Search in sources :

Example 1 with Repeat

use of uk.gov.gchq.gaffer.jobtracker.Repeat in project Gaffer by gchq.

the class JobControllerIT method shouldCorrectlyDoAndThenCancelScheduledJob.

@Test
public void shouldCorrectlyDoAndThenCancelScheduledJob() throws IOException, InterruptedException {
    // When
    final Repeat repeat = new Repeat(1, 2, TimeUnit.SECONDS);
    Job job = new Job(repeat, new OperationChain.Builder().first(new GetAllElements()).build());
    final ResponseEntity<JobDetail> jobSchedulingResponse = post("/graph/jobs/schedule", job, JobDetail.class);
    JobDetail jobDetailParent = jobSchedulingResponse.getBody();
    // Then
    assertEquals(201, jobSchedulingResponse.getStatusCode().value());
    String parentJobId = jobDetailParent.getJobId();
    // Wait for first scheduled to run
    Thread.sleep(1500);
    final ResponseEntity<List> getAllJobDetailsResponse = post("/graph/operations/execute", new GetAllJobDetails(), List.class);
    Iterable<JobDetail> jobDetails = deserialiseJobDetailIterable(getAllJobDetailsResponse.getBody());
    for (JobDetail jobDetail : jobDetails) {
        if (null != jobDetail.getParentJobId() && jobDetail.getParentJobId().equals(parentJobId)) {
            assertEquals(JobStatus.FINISHED, jobDetail.getStatus());
        }
        if (jobDetail.getJobId().equals(parentJobId)) {
            assertEquals(JobStatus.SCHEDULED_PARENT, jobDetail.getStatus());
        }
    }
    post("/graph/operations/execute", new CancelScheduledJob.Builder().jobId(parentJobId).build(), Set.class);
    final Iterable<JobDetail> cancelledJobDetails = deserialiseJobDetailIterable(post("/graph/operations/execute", new GetAllJobDetails(), List.class).getBody());
    for (JobDetail jobDetail : cancelledJobDetails) {
        if (parentJobId.equals(jobDetail.getJobId())) {
            assertEquals(JobStatus.CANCELLED, jobDetail.getStatus());
        }
    }
}
Also used : Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetAllJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ArrayList(java.util.ArrayList) List(java.util.List) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Job(uk.gov.gchq.gaffer.jobtracker.Job) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Test(org.junit.Test)

Example 2 with Repeat

use of uk.gov.gchq.gaffer.jobtracker.Repeat in project Gaffer by gchq.

the class GraphTest method shouldThrowExceptionOnExecuteJobUsingJobWithANullOperation.

@Test
public void shouldThrowExceptionOnExecuteJobUsingJobWithANullOperation() throws OperationException {
    // Given
    final Context context = new Context();
    final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).build()).storeProperties(StreamUtil.storeProps(getClass())).addSchemas(StreamUtil.schemas(getClass())).build();
    final Job job = new Job(new Repeat(), new OperationChain<>());
    // When / Then
    assertThatIllegalArgumentException().isThrownBy(() -> graph.executeJob(job, context)).withMessage("An operation is required");
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) Job(uk.gov.gchq.gaffer.jobtracker.Job) Test(org.junit.jupiter.api.Test)

Example 3 with Repeat

use of uk.gov.gchq.gaffer.jobtracker.Repeat in project Gaffer by gchq.

the class StoreTest method shouldRescheduleJobsCorrectlyWhenInitialisationCountIs.

private void shouldRescheduleJobsCorrectlyWhenInitialisationCountIs(final int initialisationCount) throws Exception {
    // Given
    final StoreProperties properties = mock(StoreProperties.class);
    given(properties.getJobTrackerEnabled()).willReturn(true);
    given(properties.getJobExecutorThreadCount()).willReturn(1);
    final Repeat repeat = new Repeat(0, 100, TimeUnit.SECONDS);
    final OperationChain opChain = new OperationChain.Builder().first(new DiscardOutput()).build();
    final User user = new User.Builder().userId("testUser").opAuth("opAuth").dataAuth("dataAuth").build();
    final JobDetail scheduledJobDetail = new JobDetail.Builder().jobId("jobId").user(user).opChain(opChain.toOverviewString()).serialisedOperationChain(opChain).repeat(repeat).build();
    given(jobTracker.getAllScheduledJobs()).willReturn(new WrappedCloseableIterable(singletonList(scheduledJobDetail)));
    StoreImpl2 store = new StoreImpl2();
    // When - initialise store
    for (int i = 0; i < initialisationCount; i++) {
        store.initialise("graphId", schema, properties);
    }
    ScheduledExecutorService service = store.getExecutorService();
    // Then - assert scheduled
    final ArgumentCaptor<ScheduledJobRunnable> scheduledJobRunnableCaptor = ArgumentCaptor.forClass(ScheduledJobRunnable.class);
    verify(service).scheduleAtFixedRate(scheduledJobRunnableCaptor.capture(), eq(repeat.getInitialDelay()), eq(repeat.getRepeatPeriod()), eq(repeat.getTimeUnit()));
    assertEquals(scheduledJobDetail, scheduledJobRunnableCaptor.getValue().getJobDetail());
    assertEquals(user, scheduledJobRunnableCaptor.getValue().getContext().getUser());
    assertArrayEquals(JSONSerialiser.serialise(opChain), JSONSerialiser.serialise(scheduledJobRunnableCaptor.getValue().getOperationChain()));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) User(uk.gov.gchq.gaffer.user.User) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) ScheduledJobRunnable(uk.gov.gchq.gaffer.store.Store.ScheduledJobRunnable) ValidateOperationChain(uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput)

Example 4 with Repeat

use of uk.gov.gchq.gaffer.jobtracker.Repeat in project Gaffer by gchq.

the class StoreTest method shouldCorrectlySetUpScheduledJobDetail.

@Test
public void shouldCorrectlySetUpScheduledJobDetail() throws Exception {
    // Given
    final StoreProperties properties = mock(StoreProperties.class);
    given(properties.getJobTrackerEnabled()).willReturn(true);
    given(properties.getJobExecutorThreadCount()).willReturn(1);
    StoreImpl2 store = new StoreImpl2();
    store.initialise("graphId", schema, properties);
    final Repeat repeat = new Repeat(0, 100, TimeUnit.SECONDS);
    final OperationChain opChain = new OperationChain.Builder().first(new DiscardOutput()).build();
    final Context context = new Context(user);
    final String operationChainOverviewString = opChain.toOverviewString();
    final String serialisedOperationChain = new String(JSONSerialiser.serialise(opChain), Charset.forName(CommonConstants.UTF_8));
    // When - setup job
    JobDetail parentJobDetail = store.executeJob(new Job(repeat, opChain), context);
    ScheduledExecutorService service = store.getExecutorService();
    // Then - assert scheduled
    verify(service).scheduleAtFixedRate(any(Runnable.class), eq(repeat.getInitialDelay()), eq(repeat.getRepeatPeriod()), eq(repeat.getTimeUnit()));
    // Then - assert job detail is as expected
    assertEquals(JobStatus.SCHEDULED_PARENT, parentJobDetail.getStatus());
    assertEquals(operationChainOverviewString, parentJobDetail.getOpChain());
    assertEquals(serialisedOperationChain, parentJobDetail.getSerialisedOperationChain());
    assertEquals(context.getUser(), parentJobDetail.getUser());
}
Also used : JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ValidateOperationChain(uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScheduledJobRunnable(uk.gov.gchq.gaffer.store.Store.ScheduledJobRunnable) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) Job(uk.gov.gchq.gaffer.jobtracker.Job) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Test(org.junit.jupiter.api.Test)

Example 5 with Repeat

use of uk.gov.gchq.gaffer.jobtracker.Repeat in project Gaffer by gchq.

the class JobServiceV2IT method shouldCorrectlyDoAndThenCancelScheduledJob.

@Test
public void shouldCorrectlyDoAndThenCancelScheduledJob() throws IOException, InterruptedException {
    // When
    final Repeat repeat = new Repeat(1, 2, TimeUnit.SECONDS);
    Job job = new Job(repeat, new OperationChain.Builder().first(new GetAllElements()).build());
    final Response jobSchedulingResponse = client.scheduleJob(job);
    JobDetail jobSchedulingDetail = jobSchedulingResponse.readEntity(new GenericType<JobDetail>() {
    });
    // Then
    assertEquals(201, jobSchedulingResponse.getStatus());
    String parentJobId = jobSchedulingDetail.getJobId();
    // Wait for first scheduled to run
    Thread.sleep(1500);
    final Response getAllJobDetailsResponse = client.executeOperation(new GetAllJobDetails());
    List<JobDetail> jobDetails = getAllJobDetailsResponse.readEntity(new GenericType<List<JobDetail>>() {
    });
    for (JobDetail jobDetail : jobDetails) {
        if (null != jobDetail.getParentJobId() && jobDetail.getParentJobId().equals(parentJobId)) {
            assertEquals(JobStatus.FINISHED, jobDetail.getStatus());
        }
        if (jobDetail.getJobId().equals(parentJobId)) {
            assertEquals(JobStatus.SCHEDULED_PARENT, jobDetail.getStatus());
        }
    }
    client.executeOperation(new CancelScheduledJob.Builder().jobId(parentJobId).build());
    final Response getAllJobDetailsResponseAfterCancelled = client.executeOperation(new GetAllJobDetails());
    List<JobDetail> jobDetailsAfterCancelled = getAllJobDetailsResponseAfterCancelled.readEntity(new GenericType<List<JobDetail>>() {
    });
    for (JobDetail jobDetail : jobDetailsAfterCancelled) {
        if (parentJobId.equals(jobDetail.getJobId())) {
            assertEquals(JobStatus.CANCELLED, jobDetail.getStatus());
        }
    }
}
Also used : Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) Response(javax.ws.rs.core.Response) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetAllJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) List(java.util.List) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Job(uk.gov.gchq.gaffer.jobtracker.Job) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Test(org.junit.jupiter.api.Test)

Aggregations

Repeat (uk.gov.gchq.gaffer.jobtracker.Repeat)6 Job (uk.gov.gchq.gaffer.jobtracker.Job)5 JobDetail (uk.gov.gchq.gaffer.jobtracker.JobDetail)5 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)5 Test (org.junit.jupiter.api.Test)4 CancelScheduledJob (uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob)4 List (java.util.List)3 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)3 GetAllJobDetails (uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Response (javax.ws.rs.core.Response)2 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)2 ValidateOperationChain (uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain)2 ScheduledJobRunnable (uk.gov.gchq.gaffer.store.Store.ScheduledJobRunnable)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TimeUnit (java.util.concurrent.TimeUnit)1 GenericType (javax.ws.rs.core.GenericType)1 Test (org.junit.Test)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1