Search in sources :

Example 31 with WorkflowOperationInstanceImpl

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

the class DefaultsWorkflowOperationHandlerTest method getWorkflowOperationResult.

private WorkflowOperationResult getWorkflowOperationResult(MediaPackage mp, Map<String, String> workflowConfiguration, Map<String, String> operationConfiguration) throws WorkflowOperationException {
    // Add the mediapackage to a workflow instance
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowState.RUNNING);
    workflowInstance.setMediaPackage(mp);
    // Apply the workflow configuration
    for (Map.Entry<String, String> entry : workflowConfiguration.entrySet()) {
        workflowInstance.setConfiguration(entry.getKey(), entry.getValue());
    }
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl();
    operation.setTemplate("defaults");
    operation.setState(OperationState.RUNNING);
    // Apply the workflow operation configuration
    for (Map.Entry<String, String> entry : operationConfiguration.entrySet()) {
        operation.setConfiguration(entry.getKey(), entry.getValue());
    }
    List<WorkflowOperationInstance> operationsList = new ArrayList<WorkflowOperationInstance>();
    operationsList.add(operation);
    workflowInstance.setOperations(operationsList);
    // Run the media package through the operation handler, ensuring that metadata gets added
    WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
    Assert.assertEquals(result.getAction(), Action.CONTINUE);
    return result;
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult)

Example 32 with WorkflowOperationInstanceImpl

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

the class TagByDublinCoreTermWOHTest method setUp.

@Before
public void setUp() throws Exception {
    MediaPackageBuilder builder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder();
    mp = builder.loadFromXml(this.getClass().getResourceAsStream("/archive_mediapackage.xml"));
    mp.getCatalog("catalog-1").setURI(this.getClass().getResource("/dublincore.xml").toURI());
    // set up the handler
    operationHandler = new TagByDublinCoreTermWOH();
    // Initialize the workflow
    instance = new WorkflowInstanceImpl();
    operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED);
    List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>();
    ops.add(operation);
    instance.setOperations(ops);
    instance.setMediaPackage(mp);
    workspace = EasyMock.createNiceMock(Workspace.class);
    EasyMock.expect(workspace.read(EasyMock.anyObject())).andAnswer(() -> getClass().getResourceAsStream("/dublincore.xml"));
    EasyMock.replay(workspace);
    operationHandler.setWorkspace(workspace);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageBuilder(org.opencastproject.mediapackage.MediaPackageBuilder) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Example 33 with WorkflowOperationInstanceImpl

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

the class SilenceDetectionWorkflowOperationHandlerTest method getWorkflowInstance.

private WorkflowInstanceImpl getWorkflowInstance(MediaPackage mp, Map<String, String> configurations) {
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowInstance.WorkflowState.RUNNING);
    workflowInstance.setMediaPackage(mp);
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("op", WorkflowOperationInstance.OperationState.RUNNING);
    operation.setTemplate("silence");
    operation.setState(WorkflowOperationInstance.OperationState.RUNNING);
    for (String key : configurations.keySet()) {
        operation.setConfiguration(key, configurations.get(key));
    }
    List<WorkflowOperationInstance> operations = new ArrayList<WorkflowOperationInstance>(1);
    operations.add(operation);
    workflowInstance.setOperations(operations);
    return workflowInstance;
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)

Example 34 with WorkflowOperationInstanceImpl

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

Example 35 with WorkflowOperationInstanceImpl

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

the class WorkflowConfigurationTest method testConfigurationSerialization.

@Test
public void testConfigurationSerialization() throws Exception {
    WorkflowOperationInstanceImpl op = new WorkflowOperationInstanceImpl("op", OperationState.RUNNING);
    Set<WorkflowConfiguration> config = new HashSet<WorkflowConfiguration>();
    config.add(new WorkflowConfigurationImpl("this", "that"));
    op.setConfiguration(config);
    WorkflowInstanceImpl instance = new WorkflowInstanceImpl();
    List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>();
    ops.add(op);
    instance.setOperations(ops);
    String xml = WorkflowParser.toXml(instance);
    Assert.assertTrue(xml.contains("configuration key=\"this\">that</"));
}
Also used : WorkflowConfiguration(org.opencastproject.workflow.api.WorkflowConfiguration) WorkflowConfigurationImpl(org.opencastproject.workflow.api.WorkflowConfigurationImpl) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)35 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)33 ArrayList (java.util.ArrayList)32 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)31 Test (org.junit.Test)11 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)11 Before (org.junit.Before)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)7 MediaPackageBuilder (org.opencastproject.mediapackage.MediaPackageBuilder)7 URI (java.net.URI)4 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)4 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)4 Workspace (org.opencastproject.workspace.api.Workspace)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Job (org.opencastproject.job.api.Job)3 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)3 File (java.io.File)2 Date (java.util.Date)2 Incident (org.opencastproject.job.api.Incident)2