Search in sources :

Example 1 with JobResult

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

the class JobTaskServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (Strings.isNullOrEmpty(req.getHeader("X-AppEngine-QueueName"))) {
        LOGGER.severe("Unauthorized task request");
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String jobKey = req.getParameter(JobResource.JOB_KEY_PARAM);
    final JobEntity jobEntity = JobStore.getUserJob(jobKey).now();
    if (jobEntity == null) {
        LOGGER.severe("Job " + jobKey + " not found");
        resp.setStatus(200);
        return;
    }
    if (jobEntity.getState() != JobState.STARTED) {
        LOGGER.info("Job " + jobKey + " has state " + jobEntity.getState() + ", stopping here.");
        resp.setStatus(200);
        return;
    }
    User user = entityManager.get().find(User.class, (int) jobEntity.getUserId());
    authProvider.set(user.asAuthenticatedUser());
    Locale jobLocale = jobLocale(user, jobEntity);
    ThreadLocalLocaleProvider.pushLocale(jobLocale);
    LOGGER.info("Set authenticated user to " + user.getEmail() + " [" + jobLocale + "]");
    JobExecutor executor = executorFactory.create(jobEntity.getType());
    JobDescriptor descriptor = jobEntity.parseDescriptor();
    try {
        final JobResult result = executor.execute(descriptor);
        markCompleted(jobEntity, result);
    } catch (Exception e) {
        markFailed(jobEntity, e);
    }
}
Also used : Locale(java.util.Locale) User(org.activityinfo.server.database.hibernate.entity.User) JobDescriptor(org.activityinfo.model.job.JobDescriptor) JobResult(org.activityinfo.model.job.JobResult) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Locale (java.util.Locale)1 ServletException (javax.servlet.ServletException)1 JobDescriptor (org.activityinfo.model.job.JobDescriptor)1 JobResult (org.activityinfo.model.job.JobResult)1 User (org.activityinfo.server.database.hibernate.entity.User)1