Search in sources :

Example 1 with JobRequest

use of org.activityinfo.model.job.JobRequest in project activityinfo by bedatadriven.

the class JobResource method start.

/**
 * Starts a new long running job
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response start(String json) {
    if (user.get().isAnonymous()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    JobRequest request;
    try {
        request = JobRequest.fromJson(PARSER.parse(json));
    } catch (Exception e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    // now create the job record in the datastore
    JobEntity record = new JobEntity(user.get().getUserId());
    record.setType(request.getDescriptor().getType());
    record.setDescriptor(request.getDescriptor().toJson().toJson());
    record.setState(JobState.STARTED);
    record.setStartTime(new Date());
    record.setLocale(user.get().getUserLocale());
    Key<JobEntity> key = JobStore.ofy().save().entity(record).now();
    // And launch a task to execute the job
    TaskHandle taskHandle = queue.add(TaskOptions.Builder.withUrl(JobTaskServlet.END_POINT).retryOptions(RetryOptions.Builder.withTaskRetryLimit(4)).param(JOB_KEY_PARAM, key.toWebSafeString()));
    LOGGER.info("Starting task " + request.getDescriptor().getType());
    return Response.ok(Response.Status.CREATED).entity(buildStatus(record).toJsonObject().toJson()).build();
}
Also used : JobRequest(org.activityinfo.model.job.JobRequest) Date(java.util.Date)

Example 2 with JobRequest

use of org.activityinfo.model.job.JobRequest in project activityinfo by bedatadriven.

the class JobResourceTest method startJob.

@Test
public void startJob() {
    final org.activityinfo.json.JsonParser parser = new org.activityinfo.json.JsonParser();
    final Queue queue = QueueFactory.getDefaultQueue();
    final AuthenticatedUser user = new AuthenticatedUser("XYZ", 1, "akbertram@gmail.com");
    final JobResource resource = new JobResource(Providers.of(user), queue);
    // First request starts the job
    final String jobId = ObjectifyService.run(new Work<String>() {

        @Override
        public String run() {
            TableModel tableModel = ImmutableTableModel.builder().formId(ResourceId.valueOf("FORM1")).build();
            ExportFormJob exportForm = new ExportFormJob(tableModel);
            JobRequest request = new JobRequest(exportForm, "en");
            Response response = resource.start(request.toJsonObject().toJson());
            JsonValue resultObject = parser.parse((String) response.getEntity());
            JobStatus result = JobStatus.fromJson(resultObject);
            assertThat(result.getState(), equalTo(JobState.STARTED));
            return result.getId();
        }
    });
    // Second request retrieves status
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            Response statusResponse = resource.get(jobId);
            JsonValue statusObject = parser.parse(((String) statusResponse.getEntity()));
            JobStatus status = JobStatus.fromJson(statusObject);
            assertThat(status.getState(), equalTo(JobState.STARTED));
        }
    });
}
Also used : JsonValue(org.activityinfo.json.JsonValue) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) ExportFormJob(org.activityinfo.model.job.ExportFormJob) Response(javax.ws.rs.core.Response) JobStatus(org.activityinfo.model.job.JobStatus) JobRequest(org.activityinfo.model.job.JobRequest) VoidWork(com.googlecode.objectify.VoidWork) Queue(com.google.appengine.api.taskqueue.Queue) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) Test(org.junit.Test)

Aggregations

JobRequest (org.activityinfo.model.job.JobRequest)2 Queue (com.google.appengine.api.taskqueue.Queue)1 VoidWork (com.googlecode.objectify.VoidWork)1 Date (java.util.Date)1 Response (javax.ws.rs.core.Response)1 JsonValue (org.activityinfo.json.JsonValue)1 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)1 ImmutableTableModel (org.activityinfo.model.analysis.ImmutableTableModel)1 TableModel (org.activityinfo.model.analysis.TableModel)1 ExportFormJob (org.activityinfo.model.job.ExportFormJob)1 JobStatus (org.activityinfo.model.job.JobStatus)1 Test (org.junit.Test)1