use of org.kie.kogito.jobs.ExpirationTime in project kogito-runtimes by kiegroup.
the class DeadlineHelper method getExpirationTime.
public static ExpirationTime getExpirationTime(ScheduleInfo info) {
ExpirationTime expirationTime;
int numRepetitions = info.getNumRepetitions();
if (numRepetitions == 0) {
// not repeatable duration and not repeatable exact date
expirationTime = info.getEndDate() == null ? DurationExpirationTime.after(getDuration(info)) : ExactExpirationTime.of(info.getEndDate());
} else if (info.getStartDate() != null) {
long duration = info.getEndDate() == null ? getDuration(info) : ChronoUnit.MILLIS.between(info.getStartDate(), info.getEndDate());
// startDate/duration and startDate/endDate
expirationTime = DurationExpirationTime.repeat(Instant.now().until(info.getStartDate(), ChronoUnit.MILLIS), duration, numRepetitions);
} else if (info.getEndDate() != null) {
// duration/endDate
if (numRepetitions <= 0) {
// if number of repetitions is infinite, since there is a limit, it is not really infinite
numRepetitions = (int) (Instant.now().until(info.getEndDate(), ChronoUnit.MILLIS) / getDuration(info));
}
expirationTime = numRepetitions <= 1 ? ExactExpirationTime.of(info.getEndDate()) : DurationExpirationTime.repeat(Instant.now().until(info.getEndDate().minus(info.getDuration().multipliedBy(numRepetitions)), ChronoUnit.MILLIS), getDuration(info), numRepetitions);
} else {
// repeatable timer with duration
long duration = getDuration(info);
expirationTime = DurationExpirationTime.repeat(duration, duration, numRepetitions);
}
return expirationTime;
}
use of org.kie.kogito.jobs.ExpirationTime in project kogito-runtimes by kiegroup.
the class AsyncEventNodeInstance method internalTrigger.
@Override
public void internalTrigger(KogitoNodeInstance from, String type) {
addEventListeners();
addAsyncStatus();
final InternalProcessRuntime processRuntime = (InternalProcessRuntime) getProcessInstance().getKnowledgeRuntime().getProcessRuntime();
// Deffer the timer scheduling to the end of current UnitOfWork execution chain
processRuntime.getUnitOfWorkManager().currentUnitOfWork().intercept(new BaseWorkUnit<>(this, instance -> {
ExpirationTime expirationTime = ExactExpirationTime.of(ZonedDateTime.now().plus(1, ChronoUnit.MILLIS));
ProcessInstanceJobDescription jobDescription = ProcessInstanceJobDescription.of(new AsyncJobId(instance.getStringId()), expirationTime, instance.getProcessInstance().getStringId(), instance.getProcessInstance().getRootProcessInstanceId(), instance.getProcessInstance().getProcessId(), instance.getProcessInstance().getRootProcessId(), Optional.ofNullable(from).map(KogitoNodeInstance::getStringId).orElse(null));
JobsService jobService = processRuntime.getJobsService();
String jobId = jobService.scheduleProcessInstanceJob(jobDescription);
setJobId(jobId);
}, i -> {
}, WorkUnit.LOW_PRIORITY));
}
use of org.kie.kogito.jobs.ExpirationTime in project kogito-runtimes by kiegroup.
the class TimerNodeInstance method internalTrigger.
@Override
public void internalTrigger(KogitoNodeInstance from, String type) {
if (!Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A TimerNode only accepts default incoming connections!");
}
triggerTime = new Date();
ExpirationTime expirationTime = createTimerInstance(getTimerNode().getTimer());
if (getTimerInstances() == null) {
addTimerListener();
}
ProcessInstanceJobDescription jobDescription = ProcessInstanceJobDescription.of(new TimerJobId(getTimerNode().getTimer().getId()), expirationTime, getProcessInstance().getStringId(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getRootProcessId(), Optional.ofNullable(from).map(KogitoNodeInstance::getStringId).orElse(null));
JobsService jobService = InternalProcessRuntime.asKogitoProcessRuntime(getProcessInstance().getKnowledgeRuntime().getProcessRuntime()).getJobsService();
timerId = jobService.scheduleProcessInstanceJob(jobDescription);
}
use of org.kie.kogito.jobs.ExpirationTime in project kogito-runtimes by kiegroup.
the class DeadlineHelperTest method testRepetitionWithEndDateCornerCase.
@Test
public void testRepetitionWithEndDateCornerCase() {
ZonedDateTime future = ZonedDateTime.now().plus(Duration.ofSeconds(5));
Collection<DeadlineInfo<Map<String, Object>>> deadlines = DeadlineHelper.parseDeadlines("[subject:5secs]@[R/PT5S/" + future.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + "]");
assertEquals(1, deadlines.size());
DeadlineInfo<Map<String, Object>> deadlineInfo = deadlines.iterator().next();
assertEquals("5secs", deadlineInfo.getNotification().get("subject"));
Collection<ScheduleInfo> scheduling = deadlineInfo.getScheduleInfo();
assertEquals(1, scheduling.size());
ScheduleInfo scheduleInfo = scheduling.iterator().next();
assertEquals(Duration.ofSeconds(5), scheduleInfo.getDuration());
assertEquals(-1, scheduleInfo.getNumRepetitions());
assertEqualsDate(future, scheduleInfo.getEndDate());
assertNull(scheduleInfo.getStartDate());
ExpirationTime time = DeadlineHelper.getExpirationTime(scheduleInfo);
assertNull(time.repeatInterval());
assertEquals(0, time.repeatLimit());
assertTrue(ZonedDateTime.now().plus(Duration.ofSeconds(5)).isAfter(time.get()));
}
use of org.kie.kogito.jobs.ExpirationTime in project kogito-runtimes by kiegroup.
the class DeadlineHelperTest method testRepetitionWithStartDate.
@Test
public void testRepetitionWithStartDate() {
Collection<DeadlineInfo<Map<String, Object>>> deadlines = DeadlineHelper.parseDeadlines("[subject:5secs]@[R26/2021-03-18T18:55:01+01:00/PT2M]");
assertEquals(1, deadlines.size());
DeadlineInfo<Map<String, Object>> deadlineInfo = deadlines.iterator().next();
assertEquals("5secs", deadlineInfo.getNotification().get("subject"));
Collection<ScheduleInfo> scheduling = deadlineInfo.getScheduleInfo();
assertEquals(1, scheduling.size());
ScheduleInfo scheduleInfo = scheduling.iterator().next();
assertEquals(Duration.ofMinutes(2), scheduleInfo.getDuration());
assertEquals(26, scheduleInfo.getNumRepetitions());
assertEquals(ZonedDateTime.parse("2021-03-18T18:55:01+01:00"), scheduleInfo.getStartDate());
assertNull(scheduleInfo.getEndDate());
ExpirationTime time = DeadlineHelper.getExpirationTime(scheduleInfo);
assertEquals(Duration.ofMinutes(2).toMillis(), time.repeatInterval());
assertEquals(26, time.repeatLimit());
assertEqualsDate(ZonedDateTime.parse("2021-03-18T18:55:01+01:00"), time.get());
}
Aggregations