use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class DatabaseJobService method start.
public void start(@Observes StartupEvent event) {
loadScheduler.scheduleAtFixedRate(() -> {
UnitOfWorkExecutor.executeInUnitOfWork(unitOfWorkManager, () -> {
LocalDateTime next = LocalDateTime.now().plus(Duration.ofMinutes(interval));
List<JobInstanceEntity> jobs = JobInstanceEntity.loadJobs(next);
LOGGER.debug("Loaded jobs ({}) to be executed before {}", jobs.size(), next);
for (JobInstanceEntity job : jobs) {
if (job.ownerInstanceId == null) {
ProcessJobDescription description = ProcessJobDescription.of(build(job), null, job.ownerDefinitionId);
scheduledJobs.computeIfAbsent(job.id, k -> {
return log(job.id, scheduler.schedule(new StartProcessOnExpiredTimer(job.id, job.ownerDefinitionId, -1, description), Duration.between(LocalDateTime.now(), job.expirationTime).toMillis(), TimeUnit.MILLISECONDS));
});
} else {
ProcessInstanceJobDescription description = ProcessInstanceJobDescription.of(job.id, job.triggerType, build(job), job.ownerInstanceId, job.ownerDefinitionId, null);
scheduledJobs.computeIfAbsent(job.id, k -> {
return log(job.id, scheduler.schedule(new SignalProcessInstanceOnExpiredTimer(job.id, job.triggerType, job.ownerDefinitionId, job.ownerInstanceId, job.limit, description), Duration.between(LocalDateTime.now(), job.expirationTime).toMillis(), TimeUnit.MILLISECONDS));
});
}
}
return null;
});
}, 1, interval * 60, TimeUnit.SECONDS);
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class FileSystemBasedJobServiceTest method testScheduleJobsForProcessInstanceAndCance.
@Test
public void testScheduleJobsForProcessInstanceAndCance() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
when(application.unitOfWorkManager()).thenReturn(new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
lenient().when(processes.processById("processId_1")).then(i -> {
return process;
});
lenient().when(processes.processIds()).then(i -> {
return Collections.singletonList("processId_1");
});
lenient().when(process.instances()).then(i -> {
latch.countDown();
return Mockito.mock(ProcessInstances.class);
});
FileSystemBasedJobService jobs = new FileSystemBasedJobService("target/jobs", 1, processes, application);
ProcessInstanceJobDescription processInstanceJobDescription = ProcessInstanceJobDescription.of(123, DurationExpirationTime.after(500), "processInstanceId", "processId", "1");
jobs.scheduleProcessInstanceJob(processInstanceJobDescription);
jobs.cancelJob(processInstanceJobDescription.id());
boolean achieved = latch.await(1, TimeUnit.SECONDS);
assertThat(achieved).isFalse();
verify(processes, times(0)).processById(eq("processId"));
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class DatabaseJobService method updateRepeatableJob.
protected void updateRepeatableJob(String id) {
JobInstanceEntity job = JobInstanceEntity.findById(id);
job.limit = job.limit - 1;
job.expirationTime = job.expirationTime.plus(job.repeatInterval, ChronoUnit.MILLIS);
job.status = JobStatus.SCHEDULED;
JobInstanceEntity.persist(job);
if (job.ownerInstanceId == null) {
ProcessJobDescription description = ProcessJobDescription.of(build(job), null, job.ownerDefinitionId);
scheduledJobs.computeIfAbsent(job.id, k -> {
return log(job.id, scheduler.schedule(new StartProcessOnExpiredTimer(job.id, job.ownerDefinitionId, job.limit, description), Duration.between(LocalDateTime.now(), job.expirationTime).toMillis(), TimeUnit.MILLISECONDS));
});
} else {
ProcessInstanceJobDescription description = ProcessInstanceJobDescription.of(job.id, job.triggerType, build(job), job.ownerInstanceId, job.ownerDefinitionId, null);
scheduledJobs.computeIfAbsent(job.id, k -> {
return log(job.id, scheduler.scheduleAtFixedRate(new SignalProcessInstanceOnExpiredTimer(job.id, job.triggerType, job.ownerDefinitionId, job.ownerInstanceId, job.limit, description), Duration.between(LocalDateTime.now(), job.expirationTime).toMillis(), job.repeatInterval, TimeUnit.MILLISECONDS));
});
}
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class CassandraJobService method updateRepeatableJob.
protected void updateRepeatableJob(String id) {
Select select = selectFrom(config.keyspace().orElse("automatiko"), tableName).column(FIRE_AT_FIELD).whereColumn(INSTANCE_ID_FIELD).isEqualTo(literal(id));
ResultSet rs = cqlSession.execute(select.build());
Row job = rs.one();
if (job != null) {
Integer limit = job.getInt(FIRE_LIMIT_FIELD) - 1;
Long repeat = job.getLong(REPEAT_INTERVAL_FIELD);
ZonedDateTime fireTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(job.getLong(FIRE_AT_FIELD)), ZoneId.systemDefault());
SimpleStatement statement = QueryBuilder.update(config.keyspace().orElse("automatiko"), tableName).setColumn(STATUS_FIELD, literal("scheduled")).setColumn(FIRE_LIMIT_FIELD, literal(limit)).setColumn(FIRE_AT_FIELD, literal(fireTime.plus(repeat, ChronoUnit.MILLIS).toInstant().toEpochMilli())).whereColumn(INSTANCE_ID_FIELD).isEqualTo(literal(id)).build();
cqlSession.execute(statement);
if (job.getString(OWNER_INSTANCE_ID_FIELD) == null) {
ProcessJobDescription description = ProcessJobDescription.of(build(job.getString(EXPRESSION_FIELD)), null, job.getString(OWNER_DEF_ID_FIELD));
scheduledJobs.computeIfAbsent(job.getString(INSTANCE_ID_FIELD), k -> {
return log(job.getString(INSTANCE_ID_FIELD), scheduler.schedule(new StartProcessOnExpiredTimer(job.getString(INSTANCE_ID_FIELD), job.getString(OWNER_DEF_ID_FIELD), limit, description), Duration.between(LocalDateTime.now(), fireTime).toMillis(), TimeUnit.MILLISECONDS));
});
} else {
ProcessInstanceJobDescription description = ProcessInstanceJobDescription.of(job.getString(INSTANCE_ID_FIELD), job.getString(TRIGGER_TYPE_FIELD), build(job.getString(EXPRESSION_FIELD)), job.getString(OWNER_INSTANCE_ID_FIELD), job.getString(OWNER_DEF_ID_FIELD), null);
scheduledJobs.computeIfAbsent(job.getString(INSTANCE_ID_FIELD), k -> {
return log(job.getString(INSTANCE_ID_FIELD), scheduler.scheduleAtFixedRate(new SignalProcessInstanceOnExpiredTimer(job.getString(INSTANCE_ID_FIELD), job.getString(TRIGGER_TYPE_FIELD), job.getString(OWNER_DEF_ID_FIELD), job.getString(OWNER_INSTANCE_ID_FIELD), limit, description), Duration.between(LocalDateTime.now(), fireTime).toMillis(), repeat, TimeUnit.MILLISECONDS));
});
}
}
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class HttpBasedJobsServiceTest method testScheduleProcessInstanceJob.
@Test
void testScheduleProcessInstanceJob(@Mock HttpRequest<Buffer> request) {
when(webClient.post(anyString())).thenReturn(request);
ProcessInstanceJobDescription processInstanceJobDescription = ProcessInstanceJobDescription.of(123, ExactExpirationTime.now(), "processInstanceId", "processId", "1");
tested.scheduleProcessInstanceJob(processInstanceJobDescription);
verify(webClient).post("/jobs");
ArgumentCaptor<Job> jobArgumentCaptor = forClass(Job.class);
verify(request).sendJson(jobArgumentCaptor.capture(), any(Handler.class));
Job job = jobArgumentCaptor.getValue();
assertThat(job.getId()).isEqualTo(processInstanceJobDescription.id());
assertThat(job.getExpirationTime()).isEqualTo(processInstanceJobDescription.expirationTime().get());
assertThat(job.getProcessInstanceId()).isEqualTo(processInstanceJobDescription.processInstanceId());
assertThat(job.getProcessId()).isEqualTo(processInstanceJobDescription.processId());
}
Aggregations