Search in sources :

Example 1 with JobManager

use of org.apache.stanbol.commons.jobs.api.JobManager in project stanbol by apache.

the class JobsResource method get.

/**
     * To read a /reasoners job output.
     * 
     * @param id
     * @return
     */
@GET
@Path("/{jid}")
public Response get(@PathParam("jid") String id, @Context HttpHeaders headers) {
    log.info("Pinging job {}", id);
    // No id
    if (id == null || id.equals("")) {
        ResponseBuilder rb = Response.status(Status.BAD_REQUEST);
        return rb.build();
    }
    JobManager m = getJobManager();
    // If the job exists
    if (m.hasJob(id)) {
        log.info("Found job with id {}", id);
        Future<?> f = m.ping(id);
        if (f.isDone() && (!f.isCancelled())) {
            /**
                 * We return OK with the result
                 */
            Object o;
            try {
                o = f.get();
                if (o instanceof ReasoningServiceResult) {
                    log.debug("Is a ReasoningServiceResult");
                    ReasoningServiceResult<?> result = (ReasoningServiceResult<?>) o;
                    return new ResponseTaskBuilder(new JobResultResource(uriInfo, headers)).build(result);
                } else {
                    log.error("Job {} does not belong to reasoners", id);
                    throw new WebApplicationException(Response.Status.NOT_FOUND);
                }
            } catch (InterruptedException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            } catch (ExecutionException e) {
                log.error("Error: ", e);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            /**
                 * We return 404 with additional info
                 */
            String jobService = new StringBuilder().append(getPublicBaseUri()).append("jobs/").append(id).toString();
            this.jobLocation = jobService;
            Viewable viewable = new Viewable("404.ftl", this);
            ResponseBuilder rb = Response.status(Status.NOT_FOUND);
            rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
            rb.entity(viewable);
            return rb.build();
        }
    } else {
        log.info("No job found with id {}", id);
        ResponseBuilder rb = Response.status(Status.NOT_FOUND);
        rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ReasoningServiceResult(org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) ResponseTaskBuilder(org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExecutionException(java.util.concurrent.ExecutionException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with JobManager

use of org.apache.stanbol.commons.jobs.api.JobManager in project stanbol by apache.

the class JobsResource method test.

/**
     * Creates a new background job to be used to test the service.
     * This method is for testing the service and to provide a sample implementation
     * of a long term operation started form a rest service.
     * 
     * @return
     */
@GET
@Path("/test{jid: (/.*)?}")
public Response test(@PathParam(value = "jid") String jid) {
    log.info("Called GET (create test job)");
    // If an Id have been provided, check whether the job has finished and return the result
    if (!jid.equals("")) {
        log.info("Looking for test job {}", jid);
        JobManager m = jobManager;
        // Remove first slash from param value
        jid = jid.substring(1);
        // If the job exists
        if (m.hasJob(jid)) {
            log.info("Found job with id {}", jid);
            Future<?> f = m.ping(jid);
            if (f.isDone() && (!f.isCancelled())) {
                /**
                     * We return OK with the result
                     */
                Object o;
                try {
                    o = f.get();
                    if (o instanceof JobResult) {
                        JobResult result = (JobResult) o;
                        return Response.ok(result.getMessage()).build();
                    } else {
                        log.error("Job {} is not a test job", jid);
                        throw new WebApplicationException(Response.Status.NOT_FOUND);
                    }
                } catch (InterruptedException e) {
                    log.error("Error: ", e);
                    throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
                } catch (ExecutionException e) {
                    log.error("Error: ", e);
                    throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
                }
            } else {
                /**
                     * We return 404 with additional info (Content-Location, the related job resource)
                     * 
                     * TODO
                     * Change into json representations
                     */
                String location = getPublicBaseUri() + "jobs/" + jid;
                String info = new StringBuilder().append("Result not ready.\n").append("Job Location: ").append(location).toString();
                return Response.status(404).header("Content-Location", location).header("Content-type", "text/plain").entity(info).build();
            }
        } else {
            log.info("No job found with id {}", jid);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } else {
        // No id have been provided, we create a new test job
        JobManager m = jobManager;
        String id = m.execute(new Job() {

            @Override
            public JobResult call() throws Exception {
                for (int i = 0; i < 30; i++) {
                    try {
                        log.info("Test Process is working");
                        Thread.sleep(1000);
                    } catch (InterruptedException ie) {
                    }
                }
                return new JobResult() {

                    @Override
                    public String getMessage() {
                        return "This is a test job";
                    }

                    @Override
                    public boolean isSuccess() {
                        return true;
                    }
                };
            }

            @Override
            public String buildResultLocation(String jobId) {
                return "jobs/test/" + jobId;
            }
        });
        // This service returns 201 Created on success
        String location = getPublicBaseUri() + "jobs/" + id;
        String info = new StringBuilder().append("Job started.\n").append("Location: ").append(location).toString();
        return Response.created(URI.create(location)).header("Content-type", "text/plain").entity(info).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JobResult(org.apache.stanbol.commons.jobs.api.JobResult) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) ExecutionException(java.util.concurrent.ExecutionException) Job(org.apache.stanbol.commons.jobs.api.Job) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with JobManager

use of org.apache.stanbol.commons.jobs.api.JobManager in project stanbol by apache.

the class JobsResource method delete.

/**
     * DELETE a background job. This method will find a job, 
     * interrupt it if it is running, and removing it
     * from the {@see JobManager}.
     * 
     * @param jid
     * @return
     */
@DELETE
@Path("/{jid}")
public Response delete(@PathParam(value = "jid") String jid) {
    log.info("Called DELETE ({})", jid);
    if (!jid.equals("")) {
        log.info("Looking for test job {}", jid);
        JobManager m = jobManager;
        // If the job exists
        if (m.hasJob(jid)) {
            log.info("Deleting Job id {}", jid);
            m.remove(jid);
            return Response.ok("Job deleted.").build();
        } else {
            log.info("No job found with id {}", jid);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } else {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 4 with JobManager

use of org.apache.stanbol.commons.jobs.api.JobManager in project stanbol by apache.

the class JobsResource method delete.

/**
     * DELETE all background jobs.
     * 
     * @return
     */
@DELETE
public Response delete() {
    log.info("Called DELETE all jobs");
    JobManager manager = jobManager;
    manager.removeAll();
    return Response.ok("All jobs have been deleted.").build();
}
Also used : JobManager(org.apache.stanbol.commons.jobs.api.JobManager) DELETE(javax.ws.rs.DELETE)

Example 5 with JobManager

use of org.apache.stanbol.commons.jobs.api.JobManager in project stanbol by apache.

the class JobsResource method get.

/**
     * GET info about a Background Job
     * 
     * @param id
     * @return Response
     */
@GET
@Path("/{jid}")
public Response get(@PathParam("jid") String id) {
    log.info("Called get() with id {}", id);
    // No id
    if (id == null || id.equals("")) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    JobManager m = jobManager;
    // If the job exists
    if (m.hasJob(id)) {
        log.info("Found job with id {}", id);
        Future<?> f = m.ping(id);
        //this.info = new JobInfoImpl();
        final JobInfo info = new JobInfoImpl();
        if (f.isDone()) {
            // The job is finished
            if (f.isCancelled()) {
                // NOTE: Canceled jobs should never exist. 
                // The web service remove any deleted process from the manager
                // If a process have been canceled programmatically, it cannot be managed by the service anymore 
                // (except for DELETE)
                log.warn("Job with id {} have been canceled. Returning 404 Not found.", id);
                return Response.status(Response.Status.NOT_FOUND).build();
            } else {
                // Job is complete
                info.setFinished();
                info.addMessage("You can remove this job using DELETE");
            }
        } else {
            // the job exists but it is not complete
            info.setRunning();
            info.addMessage("You can interrupt this job using DELETE");
        }
        // Returns 200, the job exists
        info.setOutputLocation(getPublicBaseUri() + m.getResultLocation(id));
        if (isHTML()) {
            // Result as HTML
            return Response.ok(new Viewable("info", new JobsResultData(info))).build();
        } else {
            // Result as application/json, text/plain
            return Response.ok(info).build();
        }
    } else {
        log.info("No job found with id {}", id);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : JobInfo(org.apache.stanbol.commons.jobs.api.JobInfo) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) JobManager(org.apache.stanbol.commons.jobs.api.JobManager) JobInfoImpl(org.apache.stanbol.commons.jobs.impl.JobInfoImpl) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

JobManager (org.apache.stanbol.commons.jobs.api.JobManager)5 Path (javax.ws.rs.Path)4 GET (javax.ws.rs.GET)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ExecutionException (java.util.concurrent.ExecutionException)2 DELETE (javax.ws.rs.DELETE)2 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Job (org.apache.stanbol.commons.jobs.api.Job)1 JobInfo (org.apache.stanbol.commons.jobs.api.JobInfo)1 JobResult (org.apache.stanbol.commons.jobs.api.JobResult)1 JobInfoImpl (org.apache.stanbol.commons.jobs.impl.JobInfoImpl)1 ReasoningServiceResult (org.apache.stanbol.reasoners.web.utils.ReasoningServiceResult)1 ResponseTaskBuilder (org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder)1