Search in sources :

Example 1 with ExpirationTime

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;
}
Also used : DurationExpirationTime(org.kie.kogito.jobs.DurationExpirationTime) ExactExpirationTime(org.kie.kogito.jobs.ExactExpirationTime) ExpirationTime(org.kie.kogito.jobs.ExpirationTime)

Example 2 with 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));
}
Also used : JobsService(org.kie.kogito.jobs.JobsService) Logger(org.slf4j.Logger) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) WorkUnit(org.kie.kogito.uow.WorkUnit) ZonedDateTime(java.time.ZonedDateTime) NodeInstance(org.kie.api.runtime.process.NodeInstance) AsyncJobId(org.kie.kogito.jobs.AsyncJobId) LoggerFactory(org.slf4j.LoggerFactory) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) BaseWorkUnit(org.kie.kogito.services.uow.BaseWorkUnit) Objects(java.util.Objects) ChronoUnit(java.time.temporal.ChronoUnit) ProcessInstance(org.kie.kogito.process.ProcessInstance) Node(org.kie.api.definition.process.Node) ASYNC_WAITING(org.jbpm.ruleflow.core.Metadata.ASYNC_WAITING) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) KogitoEventListener(org.kie.kogito.internal.process.event.KogitoEventListener) Optional(java.util.Optional) ExactExpirationTime(org.kie.kogito.jobs.ExactExpirationTime) ExpirationTime(org.kie.kogito.jobs.ExpirationTime) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) JobsService(org.kie.kogito.jobs.JobsService) AsyncJobId(org.kie.kogito.jobs.AsyncJobId) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) ExactExpirationTime(org.kie.kogito.jobs.ExactExpirationTime) ExpirationTime(org.kie.kogito.jobs.ExpirationTime)

Example 3 with ExpirationTime

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);
}
Also used : KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) JobsService(org.kie.kogito.jobs.JobsService) ExpirationTime(org.kie.kogito.jobs.ExpirationTime) TimerJobId(org.kie.kogito.jobs.TimerJobId) Date(java.util.Date)

Example 4 with ExpirationTime

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()));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ExpirationTime(org.kie.kogito.jobs.ExpirationTime) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with ExpirationTime

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());
}
Also used : ExpirationTime(org.kie.kogito.jobs.ExpirationTime) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

ExpirationTime (org.kie.kogito.jobs.ExpirationTime)11 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 ZonedDateTime (java.time.ZonedDateTime)3 KogitoNodeInstance (org.kie.kogito.internal.process.runtime.KogitoNodeInstance)2 ExactExpirationTime (org.kie.kogito.jobs.ExactExpirationTime)2 JobsService (org.kie.kogito.jobs.JobsService)2 ProcessInstanceJobDescription (org.kie.kogito.jobs.ProcessInstanceJobDescription)2 ChronoUnit (java.time.temporal.ChronoUnit)1 Date (java.util.Date)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)1 ASYNC_WAITING (org.jbpm.ruleflow.core.Metadata.ASYNC_WAITING)1 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)1 NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)1 EventNodeInstance (org.jbpm.workflow.instance.node.EventNodeInstance)1 Node (org.kie.api.definition.process.Node)1 NodeInstance (org.kie.api.runtime.process.NodeInstance)1 KogitoEventListener (org.kie.kogito.internal.process.event.KogitoEventListener)1