Search in sources :

Example 1 with JobProducer

use of org.opencastproject.job.api.JobProducer in project opencast by opencast.

the class AbstractJobProducerEndpoint method dispatchJob.

/**
 * @see org.opencastproject.job.api.JobProducer#acceptJob(org.opencastproject.job.api.Job)
 */
@POST
@Path("/dispatch")
public Response dispatchJob(@FormParam("id") long jobId, @FormParam("operation") String jobOperation) throws ServiceRegistryException {
    final JobProducer service = getService();
    if (service == null)
        throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);
    // See if the service is ready to accept anything
    if (!service.isReadyToAcceptJobs(jobOperation)) {
        logger.debug("Service {} is not ready to accept jobs with operation {}", service, jobOperation);
        return Response.status(Status.SERVICE_UNAVAILABLE).build();
    }
    Job job;
    try {
        job = getServiceRegistry().getJob(jobId);
    } catch (NotFoundException e) {
        logger.warn("Unable to find dispatched job {}", jobId);
        return Response.status(Status.NOT_FOUND).build();
    }
    // See if the service has strong feelings about this particular job
    try {
        if (!service.isReadyToAccept(job)) {
            logger.debug("Service {} temporarily refused to accept job {}", service, jobId);
            return Response.status(Status.SERVICE_UNAVAILABLE).build();
        }
    } catch (UndispatchableJobException e) {
        logger.warn("Service {} permanently refused to accept job {}", service, jobId);
        return Response.status(Status.PRECONDITION_FAILED).build();
    }
    service.acceptJob(job);
    return Response.noContent().build();
}
Also used : JobProducer(org.opencastproject.job.api.JobProducer) WebApplicationException(javax.ws.rs.WebApplicationException) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) NotFoundException(org.opencastproject.util.NotFoundException) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with JobProducer

use of org.opencastproject.job.api.JobProducer in project opencast by opencast.

the class ServiceRegistryInMemoryImpl method dispatchJob.

/**
 * Dispatches the job to the least loaded service or throws a <code>ServiceUnavailableException</code> if there is no
 * such service.
 *
 * @param job
 *          the job to dispatch
 * @return whether the job was dispatched
 * @throws ServiceUnavailableException
 *           if no service is available to dispatch the job
 * @throws ServiceRegistryException
 *           if the service registrations are unavailable or dispatching of the job fails
 */
protected boolean dispatchJob(Job job) throws ServiceUnavailableException, ServiceRegistryException, UndispatchableJobException {
    List<ServiceRegistration> registrations = getServiceRegistrationsByLoad(job.getJobType());
    if (registrations.size() == 0)
        throw new ServiceUnavailableException("No service is available to handle jobs of type '" + job.getJobType() + "'");
    job.setStatus(Status.DISPATCHING);
    try {
        job = updateJob(job);
    } catch (NotFoundException e) {
        throw new ServiceRegistryException("Job not found!", e);
    }
    for (ServiceRegistration registration : registrations) {
        if (registration.isJobProducer() && !registration.isInMaintenanceMode()) {
            ServiceRegistrationInMemoryImpl inMemoryRegistration = (ServiceRegistrationInMemoryImpl) registration;
            JobProducer service = inMemoryRegistration.getService();
            // Add the job to the list of jobs so that it gets counted in the load.
            // This is the same way that the JPA impl does it
            Set<Job> jobs = jobHosts.get(inMemoryRegistration);
            if (jobs == null) {
                jobs = new LinkedHashSet<Job>();
            }
            jobs.add(job);
            jobHosts.put(inMemoryRegistration, jobs);
            if (!service.isReadyToAcceptJobs(job.getOperation())) {
                jobs.remove(job);
                jobHosts.put(inMemoryRegistration, jobs);
                continue;
            }
            if (!service.isReadyToAccept(job)) {
                jobs.remove(job);
                jobHosts.put(inMemoryRegistration, jobs);
                continue;
            }
            try {
                job = updateJob(job);
            } catch (NotFoundException e) {
                jobs.remove(job);
                jobHosts.put(inMemoryRegistration, jobs);
                throw new ServiceRegistryException("Job not found!", e);
            }
            service.acceptJob(job);
            return true;
        } else if (!registration.isJobProducer()) {
            logger.warn("This implementation of the service registry doesn't support dispatching to remote services");
        // TODO: Add remote dispatching
        } else {
            logger.warn("Service " + registration + " is in maintenance mode");
        }
    }
    return false;
}
Also used : JobProducer(org.opencastproject.job.api.JobProducer) NotFoundException(org.opencastproject.util.NotFoundException) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job)

Aggregations

Job (org.opencastproject.job.api.Job)2 JobProducer (org.opencastproject.job.api.JobProducer)2 NotFoundException (org.opencastproject.util.NotFoundException)2 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JaxbJob (org.opencastproject.job.api.JaxbJob)1 UndispatchableJobException (org.opencastproject.serviceregistry.api.UndispatchableJobException)1