Skip to content

Commit

Permalink
ProcessInstanceInfo set start date when constructed
Browse files Browse the repository at this point in the history
In the `PrometheusProcessEventListener`, the field `startDate` from a `processInstance` is used to time the process instance duration metric.
https://github.com/kiegroup/droolsjbpm-integration/blob/4fb9e4c239dfc21ea99ea5b701877cafeafcbe1d/kie-server-parent/kie-server-services/kie-server-services-prometheus/src/main/java/org/kie/server/services/prometheus/PrometheusProcessEventListener.java#L96

However, the start date was always null because it is being set when `getProcessInstance` is first called.
Since `getProcessInstance` is not called before we trigger the event listener, the start date was never set.
The `ProcessInstance` start date should be set when the `ProcessInstanceInfo` object is first created.
  • Loading branch information
hmatt1 committed Jul 22, 2020
1 parent 7b9a869 commit 5971786
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.jbpm.marshalling.impl.ProtobufRuleFlowProcessInstanceMarshaller;
import org.jbpm.persistence.api.PersistentProcessInstance;
import org.jbpm.process.instance.impl.ProcessInstanceImpl;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.process.ProcessInstance;
Expand Down Expand Up @@ -103,6 +104,16 @@ public ProcessInstanceInfo(ProcessInstance processInstance) {
this.processInstance = processInstance;
this.processId = processInstance.getProcessId();
startDate = new Date();

// If we are creating a second Process Instance Info for the same process instance,
// it should not generate a new start date
if (this.processInstance != null) {
if (((WorkflowProcessInstanceImpl) this.processInstance).getStartDate() == null) {
((WorkflowProcessInstanceImpl) processInstance).internalSetStartDate(this.startDate);
} else {
startDate = ((WorkflowProcessInstanceImpl) this.processInstance).getStartDate();
}
}
}

public ProcessInstanceInfo(ProcessInstance processInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,18 @@ public void processWithNotNullStartDateTest() {
KieBase kbase = createKieBase(process);
StatefulKnowledgeSession crmPersistentSession = createSession(kbase);

RuleFlowProcessInstance processInstance = (RuleFlowProcessInstance) crmPersistentSession.startProcess( processId );
RuleFlowProcessInstance processInstance = (RuleFlowProcessInstance) crmPersistentSession.startProcess( processId );

// Ensure the Process Instance Start Date is set immediately when the processInstanceInfo is created
Assert.assertNotNull(processInstance.getStartDate());

InternalKnowledgeRuntime kruntime = processInstance.getKnowledgeRuntime();
Assert.assertEquals( ProcessInstance.STATE_ACTIVE,
processInstance.getState() );

ProcessInstanceInfo processInstanceInfo = new ProcessInstanceInfo(processInstance);
processInstance = (RuleFlowProcessInstance) processInstanceInfo.getProcessInstance(kruntime, crmPersistentSession.getEnvironment());

Assert.assertNotNull(processInstance.getStartDate());
Assert.assertEquals(processInstance.getStartDate(), processInstanceInfo.getStartDate());
}

Expand Down

0 comments on commit 5971786

Please sign in to comment.