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