Search in sources :

Example 1 with UndispatchableJobException

use of org.opencastproject.serviceregistry.api.UndispatchableJobException 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 UndispatchableJobException

use of org.opencastproject.serviceregistry.api.UndispatchableJobException in project opencast by opencast.

the class WorkflowServiceImpl method isReadyToAccept.

/**
 * {@inheritDoc}
 *
 * If we are already running the maximum number of workflows, don't accept another START_WORKFLOW job
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#isReadyToAccept(org.opencastproject.job.api.Job)
 */
@Override
public boolean isReadyToAccept(Job job) throws ServiceRegistryException, UndispatchableJobException {
    String operation = job.getOperation();
    // Only restrict execution of new jobs
    if (!Operation.START_WORKFLOW.toString().equals(operation))
        return true;
    // If the first operation is guaranteed to pause, run the job.
    if (job.getArguments().size() > 1 && job.getArguments().get(0) != null) {
        try {
            WorkflowDefinition workflowDef = WorkflowParser.parseWorkflowDefinition(job.getArguments().get(0));
            if (workflowDef.getOperations().size() > 0) {
                String firstOperationId = workflowDef.getOperations().get(0).getId();
                WorkflowOperationHandler handler = getWorkflowOperationHandler(firstOperationId);
                if (handler instanceof ResumableWorkflowOperationHandler) {
                    if (((ResumableWorkflowOperationHandler) handler).isAlwaysPause()) {
                        return true;
                    }
                }
            }
        } catch (WorkflowParsingException e) {
            throw new UndispatchableJobException(job + " is not a proper job to start a workflow", e);
        }
    }
    WorkflowInstance workflow = null;
    WorkflowSet workflowInstances = null;
    String mediaPackageId = null;
    // Fetch all workflows that are running with the current mediapackage
    try {
        workflow = getWorkflowById(job.getId());
        mediaPackageId = workflow.getMediaPackage().getIdentifier().toString();
        workflowInstances = getWorkflowInstances(new WorkflowQuery().withMediaPackage(workflow.getMediaPackage().getIdentifier().toString()).withState(RUNNING).withState(PAUSED).withState(FAILING));
    } catch (NotFoundException e) {
        logger.error("Trying to start workflow with id %s but no corresponding instance is available from the workflow service", job.getId());
        throw new UndispatchableJobException(e);
    } catch (UnauthorizedException e) {
        logger.error("Authorization denied while requesting to loading workflow instance %s: %s", job.getId(), e.getMessage());
        throw new UndispatchableJobException(e);
    } catch (WorkflowDatabaseException e) {
        logger.error("Error loading workflow instance %s: %s", job.getId(), e.getMessage());
        return false;
    }
    // If more than one workflow is running working on this mediapackage, then we don't start this one
    boolean toomany = workflowInstances.size() > 1;
    // Make sure we are not excluding ourselves
    toomany |= workflowInstances.size() == 1 && workflow.getId() != workflowInstances.getItems()[0].getId();
    // Avoid running multiple workflows with same media package id at the same time
    if (toomany) {
        if (!delayedWorkflows.contains(workflow.getId())) {
            logger.info("Delaying start of workflow %s, another workflow on media package %s is still running", workflow.getId(), mediaPackageId);
            delayedWorkflows.add(workflow.getId());
        }
        return false;
    }
    return true;
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Aggregations

UndispatchableJobException (org.opencastproject.serviceregistry.api.UndispatchableJobException)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 Job (org.opencastproject.job.api.Job)1 JobProducer (org.opencastproject.job.api.JobProducer)1 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)1 Collections.mkString (org.opencastproject.util.data.Collections.mkString)1 ResumableWorkflowOperationHandler (org.opencastproject.workflow.api.ResumableWorkflowOperationHandler)1 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1 WorkflowOperationHandler (org.opencastproject.workflow.api.WorkflowOperationHandler)1 WorkflowParsingException (org.opencastproject.workflow.api.WorkflowParsingException)1 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)1 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)1