use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class MongodbJobService method updateRepeatableJob.
protected void updateRepeatableJob(String id) {
Document job = collection().find(eq(INSTANCE_ID_FIELD, id)).projection(Projections.fields(Projections.include(INSTANCE_ID_FIELD, FIRE_AT_FIELD))).first();
if (job != null) {
Integer limit = job.getInteger(FIRE_LIMIT_FIELD) - 1;
Long repeat = job.getLong(REPEAT_INTERVAL_FIELD);
ZonedDateTime fireTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(job.getLong(FIRE_AT_FIELD)), ZoneId.systemDefault());
job.put(STATUS_FIELD, "scheduled");
job.put(FIRE_LIMIT_FIELD, limit);
job.put(FIRE_AT_FIELD, fireTime.plus(repeat, ChronoUnit.MILLIS).toInstant().toEpochMilli());
collection().updateOne(eq(INSTANCE_ID_FIELD, id), job);
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 MongodbJobService method start.
public void start(@Observes StartupEvent event) {
collection().createIndex(Indexes.ascending(INSTANCE_ID_FIELD));
collection().createIndex(Indexes.descending(FIRE_AT_FIELD));
loadScheduler.scheduleAtFixedRate(() -> {
try {
long next = LocalDateTime.now().plus(Duration.ofMinutes(config.interval().orElse(10L))).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
FindIterable<Document> jobs = collection().find(lt(FIRE_AT_FIELD, next));
for (Document job : jobs) {
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), -1, description), Duration.between(LocalDateTime.now(), ZonedDateTime.ofInstant(Instant.ofEpochMilli(job.getLong(FIRE_AT_FIELD)), ZoneId.systemDefault())).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.schedule(new SignalProcessInstanceOnExpiredTimer(job.getString(INSTANCE_ID_FIELD), job.getString(TRIGGER_TYPE_FIELD), job.getString(OWNER_DEF_ID_FIELD), job.getString(OWNER_INSTANCE_ID_FIELD), job.getInteger(FIRE_LIMIT_FIELD), description), Duration.between(LocalDateTime.now(), ZonedDateTime.ofInstant(Instant.ofEpochMilli(job.getLong(FIRE_AT_FIELD)), ZoneId.systemDefault())).toMillis(), TimeUnit.MILLISECONDS));
});
}
}
} catch (Exception e) {
LOGGER.error("Error while loading jobs from cassandra", e);
}
}, 1, config.interval().orElse(10L) * 60, TimeUnit.SECONDS);
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class FileSystemBasedJobService method loadAndSchedule.
protected void loadAndSchedule(Path path) {
try {
if (Files.isDirectory(path) || Files.isHidden(path)) {
return;
}
ScheduledJob job = mapper.readValue(Files.readAllBytes(path), ScheduledJob.class);
if (scheduledJobs.containsKey(job.getId())) {
LOGGER.debug("Already scheduled: {}", job);
return;
}
ScheduledFuture<?> future = null;
if (job.getProcessInstanceId() != null) {
ProcessInstanceJobDescription description = ProcessInstanceJobDescription.of(job.getId(), job.getTriggerType(), build(job), job.getProcessInstanceId(), job.getProcessId(), null);
if (job.getReapeatInterval() != null) {
future = scheduler.scheduleAtFixedRate(new SignalProcessInstanceOnExpiredTimer(job.getId(), job.getTriggerType(), job.getProcessId(), job.getProcessInstanceId(), false, job.getLimit(), description), calculateDelay(job.getFireTimeAsDateTime()), job.getReapeatInterval(), TimeUnit.MILLISECONDS);
} else {
future = scheduler.schedule(new SignalProcessInstanceOnExpiredTimer(job.getId(), job.getTriggerType(), job.getProcessId(), job.getProcessInstanceId(), true, description.expirationTime().repeatLimit(), description), calculateDelay(job.getFireTimeAsDateTime()), TimeUnit.MILLISECONDS);
}
} else {
ProcessJobDescription description = ProcessJobDescription.of(build(job), job.getProcessId(), null);
if (job.getReapeatInterval() != null) {
future = scheduler.scheduleAtFixedRate(new StartProcessOnExpiredTimer(job.getId(), job.getProcessId(), false, job.getLimit(), description), calculateDelay(job.getFireTimeAsDateTime()), job.getReapeatInterval(), TimeUnit.MILLISECONDS);
} else {
future = scheduler.schedule(new StartProcessOnExpiredTimer(job.getId(), job.getProcessId(), true, -1, description), calculateDelay(job.getFireTimeAsDateTime()), TimeUnit.MILLISECONDS);
}
}
scheduledJobs.put(job.getId(), future);
} catch (IOException e) {
LOGGER.warn("Unable to load stored scheduled job with id {}", path.getFileName().toString(), e);
}
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class FileSystemBasedJobServiceTest method testScheduleJobsForProcessInstanceReload.
@Test
public void testScheduleJobsForProcessInstanceReload() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
when(application.unitOfWorkManager()).thenReturn(new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
when(processes.processById("processId_1")).then(i -> {
return process;
});
when(processes.processIds()).then(i -> {
return Collections.singletonList("processId_1");
});
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(100), "processInstanceId", "processId", "1");
jobs.scheduleProcessInstanceJob(processInstanceJobDescription);
jobs.shutown(null);
jobs = new FileSystemBasedJobService("target/jobs", 1, processes, application);
jobs.scheduleOnLoad(null);
boolean achieved = latch.await(2, TimeUnit.SECONDS);
assertThat(achieved).isTrue();
}
use of io.automatiko.engine.api.jobs.ProcessInstanceJobDescription in project automatiko-engine by automatiko-io.
the class FileSystemBasedJobServiceTest method testScheduleJobsForProcessInstance.
@Test
public void testScheduleJobsForProcessInstance() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
when(application.unitOfWorkManager()).thenReturn(new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
when(processes.processById("processId_1")).then(i -> {
return process;
});
when(processes.processIds()).then(i -> {
return Collections.singletonList("processId_1");
});
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);
boolean achieved = latch.await(2, TimeUnit.SECONDS);
assertThat(achieved).isTrue();
verify(process, times(1)).instances();
}
Aggregations