Search in sources :

Example 1 with ResumableWorkflowOperationHandler

use of org.opencastproject.workflow.api.ResumableWorkflowOperationHandler in project opencast by opencast.

the class WorkflowOperationWorker method resume.

/**
 * Resumes a previously suspended workflow operation. Note that only workflow operation handlers that implement
 * {@link ResumableWorkflowOperationHandler} can be resumed.
 *
 * @return the workflow operation result
 * @throws WorkflowOperationException
 *           if executing the workflow operation handler fails
 * @throws WorkflowException
 *           if there is a problem processing the workflow
 * @throws IllegalStateException
 *           if the workflow operation cannot be resumed
 */
public WorkflowOperationResult resume() throws WorkflowOperationException, WorkflowException, IllegalStateException, UnauthorizedException {
    WorkflowOperationInstance operation = workflow.getCurrentOperation();
    // Make sure we have a (suitable) handler
    if (handler == null) {
        // If there is no handler for the operation, yet we are supposed to run it, we must fail
        logger.warn("No handler available to resume operation '{}'", operation.getTemplate());
        throw new IllegalStateException("Unable to find a workflow handler for '" + operation.getTemplate() + "'");
    } else if (!(handler instanceof ResumableWorkflowOperationHandler)) {
        throw new IllegalStateException("An attempt was made to resume a non-resumable operation");
    }
    ResumableWorkflowOperationHandler resumableHandler = (ResumableWorkflowOperationHandler) handler;
    operation.setState(OperationState.RUNNING);
    service.update(workflow);
    try {
        WorkflowOperationResult result = resumableHandler.resume(workflow, null, properties);
        return result;
    } catch (Exception e) {
        operation.setState(OperationState.FAILED);
        if (e instanceof WorkflowOperationException)
            throw (WorkflowOperationException) e;
        throw new WorkflowOperationException(e);
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) JobCanceledException(org.opencastproject.util.JobCanceledException) WorkflowOperationAbortedException(org.opencastproject.workflow.api.WorkflowOperationAbortedException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 2 with ResumableWorkflowOperationHandler

use of org.opencastproject.workflow.api.ResumableWorkflowOperationHandler 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)

Example 3 with ResumableWorkflowOperationHandler

use of org.opencastproject.workflow.api.ResumableWorkflowOperationHandler in project opencast by opencast.

the class WorkflowServiceImpl method handleOperationResult.

/**
 * Callback for workflow operation handlers that executed and finished without exception. This implementation assumes
 * that the operation worker has already adjusted the current operation's state appropriately.
 *
 * @param workflow
 *          the workflow instance
 * @param result
 *          the workflow operation result
 * @return the workflow instance
 * @throws WorkflowDatabaseException
 *           if updating the workflow fails
 */
protected WorkflowInstance handleOperationResult(WorkflowInstance workflow, WorkflowOperationResult result) throws WorkflowDatabaseException {
    // Get the operation and its handler
    WorkflowOperationInstanceImpl currentOperation = (WorkflowOperationInstanceImpl) workflow.getCurrentOperation();
    WorkflowOperationHandler handler = getWorkflowOperationHandler(currentOperation.getTemplate());
    // Create an operation result for the lazy or else update the workflow's media package
    if (result == null) {
        logger.warn("Handling a null operation result for workflow %s in operation %s", workflow.getId(), currentOperation.getTemplate());
        result = new WorkflowOperationResultImpl(workflow.getMediaPackage(), null, Action.CONTINUE, 0);
    } else {
        MediaPackage mp = result.getMediaPackage();
        if (mp != null) {
            workflow.setMediaPackage(mp);
        }
    }
    // The action to take
    Action action = result.getAction();
    // Update the workflow configuration. Update the reference to the current operation as well, since the workflow has
    // been serialized and deserialized in the meantime.
    int currentOperationPosition = currentOperation.getPosition();
    workflow = updateConfiguration(workflow, result.getProperties());
    currentOperation = (WorkflowOperationInstanceImpl) workflow.getOperations().get(currentOperationPosition);
    // Adjust workflow statistics
    currentOperation.setTimeInQueue(result.getTimeInQueue());
    // Adjust the operation state
    switch(action) {
        case CONTINUE:
            currentOperation.setState(OperationState.SUCCEEDED);
            break;
        case PAUSE:
            if (!(handler instanceof ResumableWorkflowOperationHandler)) {
                throw new IllegalStateException("Operation " + currentOperation.getTemplate() + " is not resumable");
            }
            // Set abortable and continuable to default values
            currentOperation.setContinuable(result.allowsContinue());
            currentOperation.setAbortable(result.allowsAbort());
            ResumableWorkflowOperationHandler resumableHandler = (ResumableWorkflowOperationHandler) handler;
            try {
                String url = resumableHandler.getHoldStateUserInterfaceURL(workflow);
                if (url != null) {
                    String holdActionTitle = resumableHandler.getHoldActionTitle();
                    currentOperation.setHoldActionTitle(holdActionTitle);
                    currentOperation.setHoldStateUserInterfaceUrl(url);
                }
            } catch (WorkflowOperationException e) {
                logger.warn(e, "unable to replace workflow ID in the hold state URL");
            }
            workflow.setState(PAUSED);
            currentOperation.setState(OperationState.PAUSED);
            break;
        case SKIP:
            currentOperation.setState(OperationState.SKIPPED);
            break;
        default:
            throw new IllegalStateException("Unknown action '" + action + "' returned");
    }
    if (ERROR_RESOLUTION_HANDLER_ID.equals(currentOperation.getTemplate()) && result.getAction() == Action.CONTINUE) {
        Map<String, String> resultProperties = result.getProperties();
        if (resultProperties == null || StringUtils.isBlank(resultProperties.get(RETRY_STRATEGY)))
            throw new WorkflowDatabaseException("Retry strategy not present in properties!");
        RetryStrategy retryStrategy = RetryStrategy.valueOf(resultProperties.get(RETRY_STRATEGY));
        switch(retryStrategy) {
            case NONE:
                handleFailedOperation(workflow, workflow.getCurrentOperation());
                break;
            case RETRY:
                currentOperation = (WorkflowOperationInstanceImpl) workflow.getCurrentOperation();
                break;
            default:
                throw new WorkflowDatabaseException("Retry strategy not implemented yet!");
        }
    }
    return workflow;
}
Also used : Action(org.opencastproject.workflow.api.WorkflowOperationResult.Action) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) WorkflowOperationResultImpl(org.opencastproject.workflow.api.WorkflowOperationResultImpl) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) RetryStrategy(org.opencastproject.workflow.api.RetryStrategy)

Aggregations

ResumableWorkflowOperationHandler (org.opencastproject.workflow.api.ResumableWorkflowOperationHandler)3 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)2 Collections.mkString (org.opencastproject.util.data.Collections.mkString)2 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)2 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)2 WorkflowOperationHandler (org.opencastproject.workflow.api.WorkflowOperationHandler)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 UndispatchableJobException (org.opencastproject.serviceregistry.api.UndispatchableJobException)1 JobCanceledException (org.opencastproject.util.JobCanceledException)1 NotFoundException (org.opencastproject.util.NotFoundException)1 RetryStrategy (org.opencastproject.workflow.api.RetryStrategy)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1 WorkflowException (org.opencastproject.workflow.api.WorkflowException)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1 WorkflowOperationAbortedException (org.opencastproject.workflow.api.WorkflowOperationAbortedException)1 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)1 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)1 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)1 Action (org.opencastproject.workflow.api.WorkflowOperationResult.Action)1 WorkflowOperationResultImpl (org.opencastproject.workflow.api.WorkflowOperationResultImpl)1