Search in sources :

Example 6 with ProcessInstanceJobDescription

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) JobInstanceEntity(io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 7 with ProcessInstanceJobDescription

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"));
}
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 8 with ProcessInstanceJobDescription

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));
        });
    }
}
Also used : ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) JobInstanceEntity(io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 9 with ProcessInstanceJobDescription

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));
            });
        }
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription)

Example 10 with ProcessInstanceJobDescription

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());
}
Also used : ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) Handler(io.vertx.core.Handler) Job(io.automatiko.engine.jobs.api.Job) 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