Search in sources :

Example 1 with Job

use of org.kie.kogito.jobs.api.Job in project kogito-runtimes by kiegroup.

the class VertxJobsServiceTest method testGetScheduleTime.

@Test
void testGetScheduleTime(@Mock HttpRequest<Buffer> request, @Mock HttpResponse<Buffer> response) {
    when(webClient.get(anyString())).thenReturn(request);
    Job job = new Job();
    job.setId("123");
    job.setExpirationTime(ZonedDateTime.now());
    AsyncResult<HttpResponse<Buffer>> asyncResult = mock(AsyncResult.class);
    when(asyncResult.succeeded()).thenReturn(true);
    when(asyncResult.result()).thenReturn(response);
    when(response.statusCode()).thenReturn(200);
    when(response.bodyAsJson(any())).thenReturn(job);
    doAnswer(invocationOnMock -> {
        Handler<AsyncResult<HttpResponse<Buffer>>> handler = invocationOnMock.getArgument(0);
        executor.submit(() -> handler.handle(asyncResult));
        return null;
    }).when(request).send(any());
    ZonedDateTime scheduledTime = tested.getScheduledTime("123");
    assertThat(scheduledTime).isEqualTo(job.getExpirationTime());
    verify(webClient).get("/jobs/123");
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ZonedDateTime(java.time.ZonedDateTime) HttpResponse(io.vertx.ext.web.client.HttpResponse) Job(org.kie.kogito.jobs.api.Job) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 2 with Job

use of org.kie.kogito.jobs.api.Job in project kogito-runtimes by kiegroup.

the class SpringRestJobsServiceTest method testScheduleProcessInstanceJob.

@Test
void testScheduleProcessInstanceJob() {
    when(restTemplate.postForEntity(any(URI.class), any(Job.class), eq(String.class))).thenReturn(ResponseEntity.ok().build());
    ProcessInstanceJobDescription processInstanceJobDescription = ProcessInstanceJobDescription.of(new TimerJobId(123l), ExactExpirationTime.now(), "processInstanceId", "processId");
    tested.scheduleProcessInstanceJob(processInstanceJobDescription);
    ArgumentCaptor<Job> jobArgumentCaptor = forClass(Job.class);
    verify(restTemplate).postForEntity(eq(tested.getJobsServiceUri()), jobArgumentCaptor.capture(), eq(String.class));
    Job job = jobArgumentCaptor.getValue();
    assertThat(job.getId()).isEqualTo(processInstanceJobDescription.id());
}
Also used : ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Job(org.kie.kogito.jobs.api.Job) TimerJobId(org.kie.kogito.jobs.TimerJobId) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with Job

use of org.kie.kogito.jobs.api.Job in project kogito-runtimes by kiegroup.

the class CreateProcessInstanceJobRequestEventTest method assertFields.

@Override
void assertFields(CreateProcessInstanceJobRequestEvent event) {
    super.assertFields(event);
    Job job = event.getData();
    assertThat(job).isNotNull();
    assertThat(job.getId()).isEqualTo(JOB_ID);
    assertThat(job.getProcessInstanceId()).isEqualTo(PROCESS_INSTANCE_ID);
    assertThat(job.getRootProcessInstanceId()).isEqualTo(ROOT_PROCESS_INSTANCE_ID);
    assertThat(job.getProcessId()).isEqualTo(PROCESS_ID);
    assertThat(job.getRootProcessId()).isEqualTo(ROOT_PROCESS_ID);
    assertThat(job.getNodeInstanceId()).isEqualTo(NODE_INSTANCE_ID);
    assertThat(job.getExpirationTime()).isEqualTo(EXPIRATION_TIME);
    assertThat(job.getCallbackEndpoint()).isEqualTo(CALLBACK_ENDPOINT);
    assertThat(job.getRepeatInterval()).isEqualTo(REPEAT_INTERVAL);
    assertThat(job.getRepeatLimit()).isEqualTo(REPEAT_LIMIT);
    assertThat(job.getPriority()).isEqualTo(PRIORITY);
}
Also used : Job(org.kie.kogito.jobs.api.Job)

Example 4 with Job

use of org.kie.kogito.jobs.api.Job in project kogito-runtimes by kiegroup.

the class JobCloudEventDeserializer method deserialize.

public JobCloudEvent<?> deserialize(byte[] data) {
    try {
        CloudEvent cloudEvent = objectMapper.readValue(data, CloudEvent.class);
        CloudEventData cloudEventData = Objects.requireNonNull(cloudEvent.getData(), "JobCloudEvent data field must not be null");
        if (cloudEvent.getType().equals(CreateProcessInstanceJobRequestEvent.CREATE_PROCESS_INSTANCE_JOB_REQUEST)) {
            Job job = objectMapper.readValue(cloudEventData.toBytes(), Job.class);
            return CreateProcessInstanceJobRequestEvent.builder().withValuesFrom(cloudEvent).withContextFrom(cloudEvent).job(job).build();
        } else if (cloudEvent.getType().equals(CancelJobRequestEvent.CANCEL_JOB_REQUEST)) {
            CancelJobRequestEvent.JobId jobId = objectMapper.readValue(cloudEventData.toBytes(), CancelJobRequestEvent.JobId.class);
            return CancelJobRequestEvent.builder().withValuesFrom(cloudEvent).withContextFrom(cloudEvent).jobId(jobId.getId()).build();
        }
        throw new DeserializationException("Unknown JobCloudEvent event type: " + cloudEvent.getType());
    } catch (IOException e) {
        throw new DeserializationException("An error was produced during a JobCloudEvent deserialization: " + e.getMessage(), e);
    }
}
Also used : CloudEventData(io.cloudevents.CloudEventData) IOException(java.io.IOException) Job(org.kie.kogito.jobs.api.Job) CloudEvent(io.cloudevents.CloudEvent) JobCloudEvent(org.kie.kogito.jobs.api.event.JobCloudEvent)

Example 5 with Job

use of org.kie.kogito.jobs.api.Job in project kogito-apps by kiegroup.

the class BaseMessagingApiIT method cancelJob.

@Test
@Timeout(value = 10, unit = TimeUnit.MINUTES)
protected void cancelJob() {
    assertCallbackResource();
    // create a job service request event and send it to the jobs service.
    ZonedDateTime expiration = ZonedDateTime.now().plusDays(1);
    String callback = buildCallbackEndpoint(getCallbackResourceURL(), PROCESS_ID, PROCESS_INSTANCE_ID, NODE_INSTANCE_ID_2);
    CreateProcessInstanceJobRequestEvent createJobEvent = CreateProcessInstanceJobRequestEvent.builder().source(URI.create(TEST_SOURCE)).job(new Job(JOB_ID_2, expiration, PRIORITY, callback, PROCESS_INSTANCE_ID, ROOT_PROCESS_INSTANCE_ID, PROCESS_ID, ROOT_PROCESS_ID, REPEAT_INTERVAL, REPEAT_LIMIT, NODE_INSTANCE_ID_2)).processInstanceId(PROCESS_INSTANCE_ID).processId(PROCESS_ID).rootProcessInstanceId(ROOT_PROCESS_INSTANCE_ID).rootProcessId(ROOT_PROCESS_ID).kogitoAddons(KOGITO_ADDONS).build();
    String createJobEventJson = serializer.serialize(createJobEvent);
    jobEventsEmitter.send(createJobEventJson);
    // wait until the job is created or fail if the CALLBACK_EXECUTIONS_QUERY_TIMOUT_IN_SECONDS elapsed.
    String jobUrl = "/jobs/" + JOB_ID_2;
    waitUntilResult(() -> getJob(jobUrl), response -> expectedJobExists(JOB_ID_2, response), CALLBACK_EXECUTIONS_QUERY_TIMOUT_IN_SECONDS, CALLBACK_EXECUTIONS_QUERY_POLL_INTERVAL_IN_MILLISECONDS);
    // create a job service cancel event and send it to the jobs service.
    CancelJobRequestEvent cancelJobEvent = CancelJobRequestEvent.builder().source(URI.create(TEST_SOURCE)).jobId(JOB_ID_2).build();
    String cancelJobEventJson = serializer.serialize(cancelJobEvent);
    jobEventsEmitter.send(cancelJobEventJson);
    // wait until the job was canceled or fail if CALLBACK_EXECUTIONS_QUERY_TIMOUT_IN_SECONDS elapsed.
    waitUntilResult(() -> getJob(jobUrl), response -> expectedJobDontExists(JOB_ID_2, response), CALLBACK_EXECUTIONS_QUERY_TIMOUT_IN_SECONDS, CALLBACK_EXECUTIONS_QUERY_POLL_INTERVAL_IN_MILLISECONDS);
}
Also used : CreateProcessInstanceJobRequestEvent(org.kie.kogito.jobs.api.event.CreateProcessInstanceJobRequestEvent) ZonedDateTime(java.time.ZonedDateTime) CancelJobRequestEvent(org.kie.kogito.jobs.api.event.CancelJobRequestEvent) Job(org.kie.kogito.jobs.api.Job) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Job (org.kie.kogito.jobs.api.Job)28 Test (org.junit.jupiter.api.Test)23 ScheduledJob (org.kie.kogito.jobs.service.model.ScheduledJob)13 CreateProcessInstanceJobRequestEvent (org.kie.kogito.jobs.api.event.CreateProcessInstanceJobRequestEvent)7 ZonedDateTime (java.time.ZonedDateTime)4 JobDetails (org.kie.kogito.jobs.service.model.job.JobDetails)3 Timeout (org.junit.jupiter.api.Timeout)2 ProcessInstanceJobDescription (org.kie.kogito.jobs.ProcessInstanceJobDescription)2 TimerJobId (org.kie.kogito.jobs.TimerJobId)2 JobServiceException (org.kie.kogito.jobs.service.exception.JobServiceException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CloudEvent (io.cloudevents.CloudEvent)1 CloudEventData (io.cloudevents.CloudEventData)1 AsyncResult (io.vertx.core.AsyncResult)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 IOException (java.io.IOException)1 URI (java.net.URI)1 CancelJobRequestEvent (org.kie.kogito.jobs.api.event.CancelJobRequestEvent)1