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();
}
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));
}
});
}
Aggregations