Search in sources :

Example 11 with ProcessInstanceJobDescription

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));
            });
        }
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) Document(org.bson.Document) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 12 with ProcessInstanceJobDescription

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);
}
Also used : ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) Document(org.bson.Document) MongoWriteException(com.mongodb.MongoWriteException) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 13 with ProcessInstanceJobDescription

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);
    }
}
Also used : ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 14 with ProcessInstanceJobDescription

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();
}
Also used : CollectingUnitOfWorkFactory(io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) FileSystemBasedJobService(io.automatiko.engine.addons.persistence.filesystem.job.FileSystemBasedJobService) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultUnitOfWorkManager(io.automatiko.engine.services.uow.DefaultUnitOfWorkManager) Test(org.junit.jupiter.api.Test)

Example 15 with ProcessInstanceJobDescription

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();
}
Also used : CollectingUnitOfWorkFactory(io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) FileSystemBasedJobService(io.automatiko.engine.addons.persistence.filesystem.job.FileSystemBasedJobService) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultUnitOfWorkManager(io.automatiko.engine.services.uow.DefaultUnitOfWorkManager) Test(org.junit.jupiter.api.Test)

Aggregations

ProcessInstanceJobDescription (io.automatiko.engine.api.jobs.ProcessInstanceJobDescription)15 ProcessJobDescription (io.automatiko.engine.api.jobs.ProcessJobDescription)9 Test (org.junit.jupiter.api.Test)4 FileSystemBasedJobService (io.automatiko.engine.addons.persistence.filesystem.job.FileSystemBasedJobService)3 CollectingUnitOfWorkFactory (io.automatiko.engine.services.uow.CollectingUnitOfWorkFactory)3 DefaultUnitOfWorkManager (io.automatiko.engine.services.uow.DefaultUnitOfWorkManager)3 ZonedDateTime (java.time.ZonedDateTime)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 Select (com.datastax.oss.driver.api.querybuilder.select.Select)2 JobInstanceEntity (io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Document (org.bson.Document)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 QueryExecutionException (com.datastax.oss.driver.api.core.servererrors.QueryExecutionException)1 MongoWriteConcernException (com.mongodb.MongoWriteConcernException)1 MongoWriteException (com.mongodb.MongoWriteException)1