public static JobDto fromJob(Job job) { JobDto dto = new JobDto(); dto.id = job.getId(); dto.jobDefinitionId = job.getJobDefinitionId(); dto.processInstanceId = job.getProcessInstanceId(); dto.processDefinitionId = job.getProcessDefinitionId(); dto.processDefinitionKey = job.getProcessDefinitionKey(); dto.executionId = job.getExecutionId(); dto.exceptionMessage = job.getExceptionMessage(); dto.retries = job.getRetries(); dto.dueDate = job.getDuedate(); dto.suspended = job.isSuspended(); dto.priority = job.getPriority(); dto.tenantId = job.getTenantId(); dto.createTime = job.getCreateTime(); return dto; }
public boolean areJobsAvailable() { List<Job> list = managementService.createJobQuery().list(); for (Job job : list) { if (!job.isSuspended() && job.getRetries() > 0 && (job.getDuedate() == null || ClockUtil.getCurrentTime().after(job.getDuedate()))) { return true; } } return false; }
protected List<String> getAllJobIds() { ArrayList<String> result = new ArrayList<String>(); for (Job job : managementService.createJobQuery().list()) { if (job.getProcessInstanceId() != null) { result.add(job.getId()); } } return result; }
@Deployment(resources = { "org/camunda/bpm/engine/test/bpmn/event/signal/SignalEventTest.signalStartEvent.bpmn20.xml", "org/camunda/bpm/engine/test/bpmn/event/signal/SignalEventTests.throwAlertSignalAsync.bpmn20.xml"}) @Test public void testAsyncSignalStartEventJobProperties() { processEngineConfiguration.setEnsureJobDueDateNotNull(false); ProcessDefinition catchingProcessDefinition = repositoryService .createProcessDefinitionQuery() .processDefinitionKey("startBySignal") .singleResult(); // given a process instance that throws a signal asynchronously runtimeService.startProcessInstanceByKey("throwSignalAsync"); // where the throwing instance ends immediately // then there is not yet a catching process instance assertEquals(0, runtimeService.createProcessInstanceQuery().count()); // but there is a job for the asynchronous continuation Job asyncJob = managementService.createJobQuery().singleResult(); assertEquals(catchingProcessDefinition.getId(), asyncJob.getProcessDefinitionId()); assertEquals(catchingProcessDefinition.getKey(), asyncJob.getProcessDefinitionKey()); assertNull(asyncJob.getExceptionMessage()); assertNull(asyncJob.getExecutionId()); assertNull(asyncJob.getJobDefinitionId()); assertEquals(0, asyncJob.getPriority()); assertNull(asyncJob.getProcessInstanceId()); assertEquals(3, asyncJob.getRetries()); assertNull(asyncJob.getDuedate()); assertNull(asyncJob.getDeploymentId()); }
@Test public void testTransactionRollbackInServiceTaskWithCustomRetryCycle() throws Exception { runtimeService.startProcessInstanceByKey("txRollbackServiceTaskWithCustomRetryCycle"); waitForJobExecutorToProcessAllJobs(10000); Job job = managementService.createJobQuery().singleResult(); assertNotNull(job); assertEquals(0, job.getRetries()); assertNotNull(job.getExceptionMessage()); assertNotNull(managementService.getJobExceptionStacktrace(job.getId())); }
private int executeJob(String processInstanceId) { Job job = fetchJob(processInstanceId); try { managementService.executeJob(job.getId()); } catch (Exception e) { // ignore } job = fetchJob(processInstanceId); return job.getRetries(); }
public void assertJobMigrated(Job jobBefore, String activityIdAfter, Date dueDateAfter) { Job jobAfter = snapshotAfterMigration.getJobById(jobBefore.getId()); assertNotNull("Expected that a job with id '" + jobBefore.getId() + "' exists after migration", jobAfter); JobDefinition jobDefinitionAfter = snapshotAfterMigration.getJobDefinitionForActivityIdAndType(activityIdAfter, ((JobEntity) jobBefore).getJobHandlerType()); assertNotNull("Expected that a job definition for activity '" + activityIdAfter + "' exists after migration", jobDefinitionAfter); assertEquals(jobBefore.getId(), jobAfter.getId()); assertEquals("Expected that job is assigned to job definition '" + jobDefinitionAfter.getId() + "' after migration", jobDefinitionAfter.getId(), jobAfter.getJobDefinitionId()); assertEquals("Expected that job is assigned to deployment '" + snapshotAfterMigration.getDeploymentId() + "' after migration", snapshotAfterMigration.getDeploymentId(), jobAfter.getDeploymentId()); assertEquals(dueDateAfter, jobAfter.getDuedate()); assertEquals(((JobEntity) jobBefore).getType(), ((JobEntity) jobAfter).getType()); assertEquals(jobBefore.getPriority(), jobAfter.getPriority()); assertEquals(jobDefinitionAfter.getProcessDefinitionId(), jobAfter.getProcessDefinitionId()); assertEquals(jobDefinitionAfter.getProcessDefinitionKey(), jobAfter.getProcessDefinitionKey()); }
@Deployment(resources = {"org/camunda/bpm/engine/test/history/HistoricJobLogTest.testStartTimerEvent.bpmn20.xml"}) public void testStartTimerEventJobHandlerType() { Job job = managementService .createJobQuery() .singleResult(); HistoricJobLog historicJob = historyService .createHistoricJobLogQuery() .jobId(job.getId()) .singleResult(); assertNotNull(historicJob); assertEquals(job.getId(), historicJob.getJobId()); assertEquals(job.getJobDefinitionId(), historicJob.getJobDefinitionId()); assertEquals("theStart", historicJob.getActivityId()); assertEquals(TimerStartEventJobHandler.TYPE, historicJob.getJobDefinitionType()); assertEquals("CYCLE: 0 0/5 * * * ?", historicJob.getJobDefinitionConfiguration()); assertEquals(job.getDuedate(), historicJob.getJobDueDate()); }
public void testSetJobDueDateWithUpdateInstancePermissionOnAnyProcessDefinition() { // given String processInstanceId = startProcessInstanceByKey(TIMER_BOUNDARY_PROCESS_KEY).getId(); createGrantAuthorization(PROCESS_DEFINITION, ANY, userId, UPDATE_INSTANCE); String jobId = selectJobByProcessInstanceId(processInstanceId).getId(); // when managementService.setJobDuedate(jobId, null); // then Job job = selectJobById(jobId); assertNull(job.getDuedate()); }
@Test public void createModificationJobs() { ProcessDefinition processDefinition = testRule.deployAndGetDefinition(instance); rule.getProcessEngineConfiguration().setBatchJobsPerSeed(10); Batch batch = helper.startAfterAsync("process1", 20, "user1", processDefinition.getId()); JobDefinition seedJobDefinition = helper.getSeedJobDefinition(batch); JobDefinition modificationJobDefinition = helper.getExecutionJobDefinition(batch);; helper.executeSeedJob(batch); List<Job> modificationJobs = helper.getJobsForDefinition(modificationJobDefinition); assertEquals(10, modificationJobs.size()); for (Job modificationJob : modificationJobs) { assertEquals(modificationJobDefinition.getId(), modificationJob.getJobDefinitionId()); assertEquals(currentTime, modificationJob.getDuedate()); assertNull(modificationJob.getProcessDefinitionId()); assertNull(modificationJob.getProcessDefinitionKey()); assertNull(modificationJob.getProcessInstanceId()); assertNull(modificationJob.getExecutionId()); } // and the seed job still exists Job seedJob = helper.getJobForDefinition(seedJobDefinition); assertNotNull(seedJob); }
@Test public void testQueryByExecutionId() { Job job = managementService.createJobQuery().processInstanceId(processInstanceIdOne).singleResult(); JobQuery query = managementService.createJobQuery().executionId(job.getExecutionId()); assertEquals(query.singleResult().getId(), job.getId()); verifyQueryResults(query, 1); }
@Deployment(resources = { "org/camunda/bpm/engine/test/bpmn/async/FoxJobRetryCmdTest.testFailedServiceTask.bpmn20.xml" }) public void testOverwritingPropertyWithBpmnExtension() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("failedServiceTask"); assertNotNull(pi); Job job = managementService.createJobQuery().processInstanceId(pi.getProcessInstanceId()).singleResult(); assertNotNull(job); assertEquals(pi.getProcessInstanceId(), job.getProcessInstanceId()); try { managementService.executeJob(job.getId()); fail("Exception expected!"); } catch(Exception e) { // expected } job = managementService.createJobQuery().jobId(job.getId()).singleResult(); assertEquals(4, job.getRetries()); } }
public void testSuspendJobByIdWihtUpdatePermissionOnProcessInstance() { // given String processInstanceId = startProcessInstanceByKey(TIMER_BOUNDARY_PROCESS_KEY).getId(); String jobId = selectJobByProcessInstanceId(processInstanceId).getId(); createGrantAuthorization(PROCESS_INSTANCE, processInstanceId, userId, UPDATE); // when managementService.suspendJobById(jobId); // then Job job = selectJobByProcessInstanceId(processInstanceId); assertNotNull(job); assertTrue(job.isSuspended()); }
public void setRetries(Batch batch, int count, int retries) { List<Job> jobs = getExecutionJobs(batch); assertTrue(jobs.size() >= count); ManagementService managementService = getManagementService(); for (int i = 0; i < count; i++) { managementService.setJobRetries(jobs.get(i).getId(), retries); } }
@Deployment(resources = {"org/camunda/bpm/engine/test/history/HistoricJobLogTest.testAsyncContinuation.bpmn20.xml"}) public void testAsyncBeforeJobHandlerType() { processEngineConfiguration.setEnsureJobDueDateNotNull(false); runtimeService.startProcessInstanceByKey("process"); Job job = managementService .createJobQuery() .singleResult(); HistoricJobLog historicJob = historyService .createHistoricJobLogQuery() .jobId(job.getId()) .singleResult(); assertNotNull(historicJob); assertNull(historicJob.getJobDueDate()); assertEquals(job.getJobDefinitionId(), historicJob.getJobDefinitionId()); assertEquals("serviceTask", historicJob.getActivityId()); assertEquals(AsyncContinuationJobHandler.TYPE, historicJob.getJobDefinitionType()); assertEquals(MessageJobDeclaration.ASYNC_BEFORE, historicJob.getJobDefinitionConfiguration()); }
@Test public void testScheduleJobForBatchWindow() throws ParseException { ClockUtil.setCurrentTime(currentDate); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(startTime); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(endTime); processEngineConfiguration.initHistoryCleanup(); Job job = historyService.cleanUpHistoryAsync(); assertFalse(startDateForCheck.after(job.getDuedate())); // job due date is not before start date assertTrue(endDateForCheck.after(job.getDuedate())); } }
@Deployment public void testGetJobExceptionStacktrace() { ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("exceptionInJobExecution"); // The execution is waiting in the first usertask. This contains a boundry // timer event which we will execute manual for testing purposes. Job timerJob = managementService.createJobQuery() .processInstanceId(processInstance.getId()) .singleResult(); assertNotNull("No job found for process instance", timerJob); try { managementService.executeJob(timerJob.getId()); fail("RuntimeException from within the script task expected"); } catch (RuntimeException re) { assertTextPresent("This is an exception thrown from scriptTask", re.getMessage()); } // Fetch the task to see that the exception that occurred is persisted timerJob = managementService.createJobQuery() .processInstanceId(processInstance.getId()) .singleResult(); Assert.assertNotNull(timerJob); Assert.assertNotNull(timerJob.getExceptionMessage()); assertTextPresent("This is an exception thrown from scriptTask", timerJob.getExceptionMessage()); // Get the full stacktrace using the managementService String exceptionStack = managementService.getJobExceptionStacktrace(timerJob.getId()); Assert.assertNotNull(exceptionStack); assertTextPresent("This is an exception thrown from scriptTask", exceptionStack); }
@Test @OperateOnDeployment("app") public void testInvokeProcessApplicationWithContextOnAsyncExecution() { runtimeService.startProcessInstanceByKey("timerProcess"); ProcessApplicationWithInvocationContext.clearInvocationContext(); Job timer = managementService.createJobQuery().timers().singleResult(); assertThat(timer, is(notNullValue())); long dueDate = timer.getDuedate().getTime(); Date afterDueDate = new Date(dueDate + 1000 * 60); ClockUtil.setCurrentTime(afterDueDate); waitForJobExecutorToProcessAllJobs(); InvocationContext invocationContext = ProcessApplicationWithInvocationContext.getInvocationContext(); assertThat(invocationContext, is(notNullValue())); assertThat(invocationContext.getExecution(), is(notNullValue())); assertThat(invocationContext.getExecution().getId(), is(timer.getExecutionId())); }
@Test public void shouldCreateActivatedMonitorJob() { // given Batch batch = helper.migrateProcessInstancesAsync(1); // when helper.executeSeedJob(batch); // then Job monitorJob = helper.getMonitorJob(batch); assertFalse(monitorJob.isSuspended()); }
@Test public void testBatchExecutionFailureWithMissingProcessInstance() { Batch batch = helper.migrateProcessInstancesAsync(2); helper.executeSeedJob(batch); List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list(); String deletedProcessInstanceId = processInstances.get(0).getId(); // when runtimeService.deleteProcessInstance(deletedProcessInstanceId, "test"); helper.executeJobs(batch); // then the remaining process instance was migrated assertEquals(0, helper.countSourceProcessInstances()); assertEquals(1, helper.countTargetProcessInstances()); // and one batch job failed and has 2 retries left List<Job> migrationJobs = helper.getExecutionJobs(batch); assertEquals(1, migrationJobs.size()); Job failedJob = migrationJobs.get(0); assertEquals(2, failedJob.getRetries()); assertThat(failedJob.getExceptionMessage(), startsWith("ENGINE-23003")); assertThat(failedJob.getExceptionMessage(), containsString("Process instance '" + deletedProcessInstanceId + "' cannot be migrated")); }