Search in sources :

Example 26 with JobDetail

use of uk.gov.gchq.gaffer.jobtracker.JobDetail in project gaffer-doc by gchq.

the class Jobs method run.

public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // / [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [job] create an operation chain to be executed as a job
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<? extends Element>> job = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build()).build();
    // ---------------------------------------------------------
    // [execute job] execute the job
    // ---------------------------------------------------------
    final JobDetail initialJobDetail = graph.executeJob(job, user);
    final String jobId = initialJobDetail.getJobId();
    // ---------------------------------------------------------
    print("JOB_DETAIL_START", initialJobDetail.toString());
    waitUntilJobHashFinished(user, graph, initialJobDetail);
    // [job details] Get the job details
    // ---------------------------------------------------------
    final JobDetail jobDetail = graph.execute(new GetJobDetails.Builder().jobId(jobId).build(), user);
    // ---------------------------------------------------------
    print("JOB_DETAIL_FINISH", jobDetail.toString());
    // [all job details] Get all job details
    // ---------------------------------------------------------
    final CloseableIterable<JobDetail> jobDetails = graph.execute(new GetAllJobDetails(), user);
    // ---------------------------------------------------------
    for (final JobDetail detail : jobDetails) {
        print("ALL_JOB_DETAILS", detail.toString());
    }
    // [get job results] Get the job results
    // ---------------------------------------------------------
    final CloseableIterable<?> jobResults = graph.execute(new GetJobResults.Builder().jobId(jobId).build(), user);
    // ---------------------------------------------------------
    for (final Object result : jobResults) {
        print("JOB_RESULTS", result.toString());
    }
    return (CloseableIterable) jobResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetJobResults(uk.gov.gchq.gaffer.operation.impl.job.GetJobResults) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetAllJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 27 with JobDetail

use of uk.gov.gchq.gaffer.jobtracker.JobDetail in project gaffer-doc by gchq.

the class GetGafferResultCacheExportExample method exportAndGetJobDetails.

public JobDetail exportAndGetJobDetails() {
    // ---------------------------------------------------------
    final OperationChain<JobDetail> opChain = new OperationChain.Builder().first(new GetAllElements()).then(new ExportToGafferResultCache<>()).then(new DiscardOutput()).then(new GetJobDetails()).build();
    // ---------------------------------------------- -----------
    jobDetail = runExample(opChain, null);
    return jobDetail;
}
Also used : JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ExportToGafferResultCache(uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput)

Example 28 with JobDetail

use of uk.gov.gchq.gaffer.jobtracker.JobDetail in project gaffer-doc by gchq.

the class GetJobDetailsExample method getJobDetailsInOperationChain.

public JobDetail getJobDetailsInOperationChain() {
    // ---------------------------------------------------------
    final OperationChain<JobDetail> opChain = new OperationChain.Builder().first(new GetAllElements()).then(new DiscardOutput()).then(new GetJobDetails()).build();
    // ---------------------------------------------------------
    final JobDetail jobDetail = runExample(opChain, null);
    jobId = jobDetail.getJobId();
    return jobDetail;
}
Also used : JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput)

Example 29 with JobDetail

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

the class GetJobDetailsHandlerTest method shouldGetJobDetailsByDelegatingToJobTrackerWithOperationJobId.

@Test
public void shouldGetJobDetailsByDelegatingToJobTrackerWithOperationJobId() throws OperationException {
    // Given
    final String jobId = "jobId";
    final GetJobDetailsHandler handler = new GetJobDetailsHandler();
    final GetJobDetails operation = new GetJobDetails.Builder().jobId(jobId).build();
    final Store store = mock(Store.class);
    final JobTracker jobTracker = mock(JobTracker.class);
    final User user = mock(User.class);
    final JobDetail jobsDetail = mock(JobDetail.class);
    final Context context = new Context(user);
    given(store.getJobTracker()).willReturn(jobTracker);
    given(jobTracker.getJob(jobId, user)).willReturn(jobsDetail);
    // When
    final JobDetail result = handler.doOperation(operation, context, store);
    // Then
    assertSame(jobsDetail, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GetJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) User(uk.gov.gchq.gaffer.user.User) JobTracker(uk.gov.gchq.gaffer.jobtracker.JobTracker) Store(uk.gov.gchq.gaffer.store.Store) Test(org.junit.jupiter.api.Test)

Example 30 with JobDetail

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

the class Graph method executeJob.

/**
 * Performs the given Job on the store.
 * This should be used for Scheduled Jobs,
 * although if no repeat is set it
 * will act as a normal Job.
 *
 * @param job     the {@link Job} to execute, which contains the {@link OperationChain} and the {@link uk.gov.gchq.gaffer.jobtracker.Repeat}
 * @param context the user context for the execution of the operation
 * @return the job detail
 * @throws OperationException thrown if the job fails to run.
 */
public JobDetail executeJob(final Job job, final Context context) throws OperationException {
    if (null == context) {
        throw new IllegalArgumentException("A context is required");
    }
    if (null == job) {
        throw new IllegalArgumentException("A job is required");
    }
    OperationChain wrappedOriginal = OperationChain.wrap(job.getOperation());
    context.setOriginalOpChain(wrappedOriginal);
    final Context clonedContext = context.shallowClone();
    final OperationChain clonedOpChain = wrappedOriginal.shallowClone();
    JobDetail result = null;
    try {
        updateOperationChainView(clonedOpChain);
        for (final GraphHook graphHook : config.getHooks()) {
            graphHook.preExecute(clonedOpChain, clonedContext);
        }
        updateOperationChainView(clonedOpChain);
        job.setOperation(clonedOpChain);
        result = store.executeJob(job, context);
        for (final GraphHook graphHook : config.getHooks()) {
            graphHook.postExecute(result, clonedOpChain, clonedContext);
        }
    } catch (final Exception e) {
        for (final GraphHook graphHook : config.getHooks()) {
            try {
                result = graphHook.onFailure(result, clonedOpChain, clonedContext, e);
            } catch (final Exception graphHookE) {
                LOGGER.warn("Error in graphHook " + graphHook.getClass().getSimpleName() + ": " + graphHookE.getMessage(), graphHookE);
            }
        }
        CloseableUtil.close(clonedOpChain);
        CloseableUtil.close(result);
        throw e;
    }
    return result;
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GraphHook(uk.gov.gchq.gaffer.graph.hook.GraphHook) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreException(uk.gov.gchq.gaffer.store.StoreException) IOException(java.io.IOException)

Aggregations

JobDetail (uk.gov.gchq.gaffer.jobtracker.JobDetail)42 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)22 Test (org.junit.jupiter.api.Test)14 GetJobDetails (uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails)14 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)11 User (uk.gov.gchq.gaffer.user.User)11 ExportToGafferResultCache (uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache)10 Context (uk.gov.gchq.gaffer.store.Context)8 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)7 Job (uk.gov.gchq.gaffer.jobtracker.Job)6 OperationException (uk.gov.gchq.gaffer.operation.OperationException)6 GetAllEdges (uk.gov.gchq.gaffer.operation.impl.get.GetAllEdges)6 GetAllJobDetails (uk.gov.gchq.gaffer.operation.impl.job.GetAllJobDetails)6 Schema (uk.gov.gchq.gaffer.store.schema.Schema)6 List (java.util.List)5 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)5 Repeat (uk.gov.gchq.gaffer.jobtracker.Repeat)5 Operation (uk.gov.gchq.gaffer.operation.Operation)5 ValidateOperationChain (uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain)5 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)5