Search in sources :

Example 6 with WorkflowOperationHandler

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

the class WorkflowRestService method getOperationHandlers.

@GET
@Path("handlers.json")
@SuppressWarnings("unchecked")
@RestQuery(name = "handlers", description = "List all registered workflow operation handlers (implementations).", returnDescription = "A JSON representation of the registered workflow operation handlers.", reponses = { @RestResponse(responseCode = SC_OK, description = "A JSON representation of the registered workflow operation handlers") })
public Response getOperationHandlers() {
    JSONArray jsonArray = new JSONArray();
    for (HandlerRegistration reg : ((WorkflowServiceImpl) service).getRegisteredHandlers()) {
        WorkflowOperationHandler handler = reg.getHandler();
        JSONObject jsonHandler = new JSONObject();
        jsonHandler.put("id", handler.getId());
        jsonHandler.put("description", handler.getDescription());
        JSONObject jsonConfigOptions = new JSONObject();
        for (Entry<String, String> configEntry : handler.getConfigurationOptions().entrySet()) {
            jsonConfigOptions.put(configEntry.getKey(), configEntry.getValue());
        }
        jsonHandler.put("options", jsonConfigOptions);
        jsonArray.add(jsonHandler);
    }
    return Response.ok(jsonArray.toJSONString()).header("Content-Type", MediaType.APPLICATION_JSON).build();
}
Also used : HandlerRegistration(org.opencastproject.workflow.impl.WorkflowServiceImpl.HandlerRegistration) JSONObject(org.json.simple.JSONObject) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) JSONArray(org.json.simple.JSONArray) WorkflowServiceImpl(org.opencastproject.workflow.impl.WorkflowServiceImpl) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 7 with WorkflowOperationHandler

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

WorkflowOperationHandler (org.opencastproject.workflow.api.WorkflowOperationHandler)7 ResumableWorkflowOperationHandler (org.opencastproject.workflow.api.ResumableWorkflowOperationHandler)4 Collections.mkString (org.opencastproject.util.data.Collections.mkString)3 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)2 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)2 NotFoundException (org.opencastproject.util.NotFoundException)2 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)2 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)2 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)2 HandlerRegistration (org.opencastproject.workflow.impl.WorkflowServiceImpl.HandlerRegistration)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Set (java.util.Set)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 JSONArray (org.json.simple.JSONArray)1