Search in sources :

Example 1 with JobInstanceEntity

use of io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity 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 2 with JobInstanceEntity

use of io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity 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 3 with JobInstanceEntity

use of io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity in project automatiko-engine by automatiko-io.

the class DatabaseJobService method scheduleProcessJob.

@Override
public String scheduleProcessJob(ProcessJobDescription description) {
    LOGGER.debug("ScheduleProcessJob: {}", description);
    JobInstanceEntity scheduledJob = null;
    if (description.expirationTime().repeatInterval() != null) {
        scheduledJob = new JobInstanceEntity(description.id(), description.process().id(), JobInstanceEntity.JobStatus.SCHEDULED, description.expirationTime().get().toLocalDateTime(), description.expirationTime().repeatLimit(), description.expirationTime().repeatInterval(), description.expirationTime().expression());
    } else {
        scheduledJob = new JobInstanceEntity(description.id(), description.process().id(), JobInstanceEntity.JobStatus.SCHEDULED, description.expirationTime().get().toLocalDateTime(), description.expirationTime().repeatLimit(), null, description.expirationTime().expression());
    }
    JobInstanceEntity persist = scheduledJob;
    UnitOfWorkExecutor.executeInUnitOfWork(unitOfWorkManager, () -> {
        if (JobInstanceEntity.findById(persist.id) == null) {
            JobInstanceEntity.persist(persist);
        }
        return null;
    });
    if (description.expirationTime().get().toLocalDateTime().isBefore(LocalDateTime.now().plusMinutes(interval))) {
        scheduledJobs.computeIfAbsent(description.id(), k -> {
            return scheduler.schedule(processJobByDescription(description), calculateDelay(description.expirationTime().get()), TimeUnit.MILLISECONDS);
        });
    }
    return description.id();
}
Also used : JobInstanceEntity(io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity)

Example 4 with JobInstanceEntity

use of io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity in project automatiko-engine by automatiko-io.

the class DatabaseJobService method scheduleProcessInstanceJob.

@Override
public String scheduleProcessInstanceJob(ProcessInstanceJobDescription description) {
    JobInstanceEntity scheduledJob = null;
    if (description.expirationTime().repeatInterval() != null) {
        scheduledJob = new JobInstanceEntity(description.id(), description.triggerType(), description.processId() + version(description.processVersion()), description.processInstanceId(), JobInstanceEntity.JobStatus.SCHEDULED, description.expirationTime().get().toLocalDateTime(), description.expirationTime().repeatLimit(), description.expirationTime().repeatInterval(), description.expirationTime().expression());
    } else {
        scheduledJob = new JobInstanceEntity(description.id(), description.triggerType(), description.processId() + version(description.processVersion()), description.processInstanceId(), JobInstanceEntity.JobStatus.SCHEDULED, description.expirationTime().get().toLocalDateTime(), description.expirationTime().repeatLimit(), null, description.expirationTime().expression());
    }
    JobInstanceEntity.persist(scheduledJob);
    if (description.expirationTime().get().toLocalDateTime().isBefore(LocalDateTime.now().plusMinutes(interval))) {
        scheduledJobs.computeIfAbsent(description.id(), k -> {
            return log(description.id(), scheduler.schedule(new SignalProcessInstanceOnExpiredTimer(description.id(), description.triggerType(), description.processId() + version(description.processVersion()), description.processInstanceId(), description.expirationTime().repeatLimit(), description), calculateDelay(description.expirationTime().get()), TimeUnit.MILLISECONDS));
        });
    }
    return description.id();
}
Also used : JobInstanceEntity(io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity)

Aggregations

JobInstanceEntity (io.automatiko.engine.addons.persistence.db.model.JobInstanceEntity)4 ProcessInstanceJobDescription (io.automatiko.engine.api.jobs.ProcessInstanceJobDescription)2 ProcessJobDescription (io.automatiko.engine.api.jobs.ProcessJobDescription)2 LocalDateTime (java.time.LocalDateTime)1