Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): Populating process instance id in jobs #4334

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.camunda.bpm.engine.impl.persistence.entity.JobEntity;
import org.camunda.bpm.engine.impl.persistence.entity.MessageEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.util.JsonUtil;

import static org.camunda.bpm.engine.impl.RestartProcessInstancesBatchConfigurationJsonConverter.PROCESS_DEFINITION_ID;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Not needed anymore:

Suggested change
import org.camunda.bpm.engine.impl.util.JsonUtil;
import static org.camunda.bpm.engine.impl.RestartProcessInstancesBatchConfigurationJsonConverter.PROCESS_DEFINITION_ID;


/**
*
Expand All @@ -54,6 +57,7 @@ protected void postProcessJob(RestartProcessInstancesBatchConfiguration configur
.findDeployedProcessDefinitionById(configuration.getProcessDefinitionId());
job.setDeploymentId(processDefinitionEntity.getDeploymentId());
}
job.setProcessDefinitionId(configuration.getProcessDefinitionId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ protected void createJobEntities(BatchEntity batch, T configuration, String depl
ByteArrayEntity configurationEntity = saveConfiguration(byteArrayManager, jobConfiguration);

JobEntity job = createBatchJob(batch, configurationEntity);

if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this will be required to avoid populating the ids for entities that are not process instance:

Suggested change
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1
&& !(this instanceof DecisionSetRemovalTimeJobHandler)
&& !(this instanceof DeleteHistoricDecisionInstancesJobHandler)
&& !(this instanceof SetJobRetriesJobHandler)
&& !(this instanceof SetExternalTaskRetriesJobHandler)
// && !(this instanceof BatchSetRemovalTimeJobHandler)
) {

job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

job.setDeploymentId(deploymentId);
postProcessJob(configuration, job, jobConfiguration);
jobManager.insertAndHintJobExecutor(job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ protected MessageCorrelationBatchConfiguration createJobConfiguration(MessageCor

@Override
protected void postProcessJob(MessageCorrelationBatchConfiguration configuration, JobEntity job, MessageCorrelationBatchConfiguration jobConfiguration) {
// if there is only one process instance to adjust, set its ID to the job so exclusive scheduling is possible
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public String getType() {

@Override
protected void postProcessJob(BatchConfiguration configuration, JobEntity job, BatchConfiguration jobConfiguration) {
// if there is only one process instance to adjust, set its ID to the job so exclusive scheduling is possible
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

}

protected ByteArrayEntity findByteArrayById(String byteArrayId, CommandContext commandContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.camunda.bpm.engine.batch.Batch;
import org.camunda.bpm.engine.batch.history.HistoricBatch;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.history.HistoricJobLog;
import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.cfg.multitenancy.TenantIdProvider;
Expand Down Expand Up @@ -1150,6 +1151,35 @@ public void shouldSetExecutionStartTimeInBatchAndHistory() {
Assertions.assertThat(historicBatch.getExecutionStartTime()).isEqualToIgnoringMillis(TEST_DATE);
}

@Test
public void shouldSetProcessInstanceAndDefinitionIdInHistoryJobLog() {
// given

ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS);
ProcessInstance processInstance = runtimeService.createProcessInstanceById(processDefinition.getId())
.startBeforeActivity("userTask1")
.execute();
runtimeService.deleteProcessInstance(processInstance.getId(), "test");
Batch batch = runtimeService.restartProcessInstances(processDefinition.getId())
.startAfterActivity("userTask2")
.processInstanceIds(processInstance.getId())
.executeAsync();
helper.executeSeedJob(batch);

Job executionJob = helper.getExecutionJobs(batch).get(0);
assertEquals(processInstance.getProcessDefinitionId(), executionJob.getProcessDefinitionId());
assertEquals(processInstance.getRootProcessInstanceId(), executionJob.getProcessInstanceId());

yanavasileva marked this conversation as resolved.
Show resolved Hide resolved
// when
helper.executeJob(executionJob);

// then
HistoricJobLog jobLog = historyService.createHistoricJobLogQuery().jobDefinitionType(Batch.TYPE_PROCESS_INSTANCE_RESTART).list().get(0);

assertEquals(processInstance.getProcessDefinitionId(), jobLog.getProcessDefinitionId());
assertEquals(processInstance.getRootProcessInstanceId(), jobLog.getProcessInstanceId());
Comment on lines +1179 to +1180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ The checks are performed for the historic job log, I would expect to check the jobs as well.

}

protected void assertBatchCreated(Batch batch, int processInstanceCount) {
assertNotNull(batch);
assertNotNull(batch.getId());
Expand Down