Search in sources :

Example 6 with Job

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

the class GraphTest method shouldAddSchemaGroupsIfNotIncludedInJob.

@Test
public void shouldAddSchemaGroupsIfNotIncludedInJob() throws OperationException {
    // given
    final Job job = new Job(null, new OperationChain.Builder().first(new GetAllElements()).build());
    final Store store = mock(Store.class);
    given(store.getSchema()).willReturn(new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition()).edge(TestGroups.EDGE, new SchemaEdgeDefinition()).edge(TestGroups.EDGE_2, new SchemaEdgeDefinition()).build());
    given(store.getProperties()).willReturn(new StoreProperties());
    final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).build()).storeProperties(StreamUtil.storeProps(getClass())).store(store).build();
    final ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class);
    final ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
    given(store.executeJob(jobCaptor.capture(), contextCaptor.capture())).willReturn(new JobDetail());
    // when
    graph.executeJob(job, context);
    // then
    final GetAllElements operation = (GetAllElements) ((OperationChain) jobCaptor.getValue().getOperation()).getOperations().get(0);
    assertEquals(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition()).edge(TestGroups.EDGE, new ViewElementDefinition()).edge(TestGroups.EDGE_2, new ViewElementDefinition()).build(), operation.getView());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) TestStore(uk.gov.gchq.gaffer.integration.store.TestStore) GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Job(uk.gov.gchq.gaffer.jobtracker.Job) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 7 with Job

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

the class GraphTest method shouldThrowExceptionOnExecuteJobUsingJobWithANullJob.

@Test
public void shouldThrowExceptionOnExecuteJobUsingJobWithANullJob() 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 = null;
    // When / Then
    assertThatIllegalArgumentException().isThrownBy(() -> graph.executeJob(job, context)).withMessage("A job is required");
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Job(uk.gov.gchq.gaffer.jobtracker.Job) Test(org.junit.jupiter.api.Test)

Example 8 with Job

use of uk.gov.gchq.gaffer.jobtracker.Job 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 9 with Job

use of uk.gov.gchq.gaffer.jobtracker.Job 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)

Example 10 with Job

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

the class JobServiceV2IT method shouldNotKeepScheduledJobsRunningAfterRestartWhenUsingInMemoryCache.

@Test
public void shouldNotKeepScheduledJobsRunningAfterRestartWhenUsingInMemoryCache() throws IOException {
    // Given - schedule Job
    final Repeat repeat = new Repeat(1, 2, TimeUnit.SECONDS);
    Job job = new Job(repeat, new OperationChain.Builder().first(new GetAllElements()).build());
    final Response scheduleResponse = client.scheduleJob(job);
    String parentJobId = scheduleResponse.readEntity(new GenericType<JobDetail>() {
    }).getJobId();
    // When - get all JobDetails
    final Response allJobDetailsResponse = client.executeOperation(new GetAllJobDetails());
    List<JobDetail> allJobDetails = allJobDetailsResponse.readEntity(new GenericType<List<JobDetail>>() {
    });
    // then - assert parent is of Scheduled parent
    assertEquals(JobStatus.SCHEDULED_PARENT, allJobDetails.stream().filter(jobDetail -> jobDetail.getJobId().equals(parentJobId)).findFirst().get().getStatus());
    // Restart server to check Job still scheduled
    client.stopServer();
    client.reinitialiseGraph();
    // When - get all JobDetails
    final Response allJobDetailsResponse2 = client.executeOperation(new GetAllJobDetails());
    List<JobDetail> allJobDetails2 = allJobDetailsResponse2.readEntity(new GenericType<List<JobDetail>>() {
    });
    // Then - assert parent job id is not present
    assertTrue(allJobDetails2.stream().noneMatch(jobDetail -> jobDetail.getJobId().equals(parentJobId)));
}
Also used : Job(uk.gov.gchq.gaffer.jobtracker.Job) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) IOException(java.io.IOException) JobStatus(uk.gov.gchq.gaffer.jobtracker.JobStatus) Test(org.junit.jupiter.api.Test) GenericType(javax.ws.rs.core.GenericType) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) Response(javax.ws.rs.core.Response) ServiceConstants(uk.gov.gchq.gaffer.rest.ServiceConstants) GetAllJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) GenericType(javax.ws.rs.core.GenericType) Repeat(uk.gov.gchq.gaffer.jobtracker.Repeat) Response(javax.ws.rs.core.Response) GetAllJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) List(java.util.List) Job(uk.gov.gchq.gaffer.jobtracker.Job) CancelScheduledJob(uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob) Test(org.junit.jupiter.api.Test)

Aggregations

Job (uk.gov.gchq.gaffer.jobtracker.Job)10 Test (org.junit.jupiter.api.Test)8 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)8 JobDetail (uk.gov.gchq.gaffer.jobtracker.JobDetail)6 Repeat (uk.gov.gchq.gaffer.jobtracker.Repeat)5 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)5 CancelScheduledJob (uk.gov.gchq.gaffer.operation.impl.job.CancelScheduledJob)4 Context (uk.gov.gchq.gaffer.store.Context)4 List (java.util.List)3 Response (javax.ws.rs.core.Response)3 GetAllJobDetails (uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)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 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1