Search in sources :

Example 51 with WorkflowOperationResult

use of org.opencastproject.workflow.api.WorkflowOperationResult 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 52 with WorkflowOperationResult

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

the class ExecuteOnceWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.debug("Running execute workflow operation with ID {}", operation.getId());
    // Get operation parameters
    String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
    String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
    float load = 1.0f;
    String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
    if (StringUtils.isNotBlank(loadPropertyStr)) {
        try {
            load = Float.parseFloat(loadPropertyStr);
        } catch (NumberFormatException e) {
            String description = StringUtils.trimToEmpty(operation.getDescription());
            logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
        }
    }
    String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
    String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
    boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));
    // Unmarshall target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (targetFlavorStr != null)
        targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
    // Unmarshall expected mediapackage element type
    MediaPackageElement.Type expectedType = null;
    if (expectedTypeStr != null) {
        for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
            expectedType = type;
            break;
        }
        if (expectedType == null)
            throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
    }
    // Process the result element
    MediaPackageElement resultElement = null;
    try {
        Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);
        WorkflowOperationResult result = null;
        // Wait for all jobs to be finished
        if (!waitForStatus(job).isSuccess())
            throw new WorkflowOperationException("Execute operation failed");
        if (StringUtils.isNotBlank(job.getPayload())) {
            if (setWfProps) {
                // The job payload is a file with set of properties for the workflow
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                final Properties properties = new Properties();
                File propertiesFile = workspace.get(resultElement.getURI());
                try (InputStream is = new FileInputStream(propertiesFile)) {
                    properties.load(is);
                }
                logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
                workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());
                Map<String, String> wfProps = new HashMap<String, String>((Map) properties);
                result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
            } else {
                // The job payload is a new element for the MediaPackage
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
                    // Have the track inspected and return the result
                    Job inspectionJob = null;
                    inspectionJob = inspectionService.inspect(resultElement.getURI());
                    JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
                    if (!barrier.waitForJobs().isSuccess()) {
                        throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
                    }
                    resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
                }
                // Store new element to mediaPackage
                mediaPackage.add(resultElement);
                URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename);
                resultElement.setURI(uri);
                // Set new flavor
                if (targetFlavor != null)
                    resultElement.setFlavor(targetFlavor);
                // Set new tags
                if (targetTags != null) {
                    // Assume the tags starting with "-" means we want to eliminate such tags form the result element
                    for (String tag : asList(targetTags)) {
                        if (tag.startsWith("-"))
                            // We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
                            resultElement.removeTag(tag.replaceAll("^-+", ""));
                        else
                            resultElement.addTag(tag);
                    }
                }
                result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
            }
        } else {
            // Payload is empty
            result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
        }
        logger.debug("Execute operation {} completed", operation.getId());
        return result;
    } catch (ExecuteException e) {
        throw new WorkflowOperationException(e);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException("Some result element couldn't be serialized", e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException("Could not find mediapackage", e);
    } catch (IOException e) {
        throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
    } catch (MediaInspectionException e) {
        throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
    }
}
Also used : HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Properties(java.util.Properties) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) Job(org.opencastproject.job.api.Job) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) JobBarrier(org.opencastproject.job.api.JobBarrier) FileInputStream(java.io.FileInputStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) File(java.io.File)

Example 53 with WorkflowOperationResult

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

the class HttpNotificationWorkflowOperationHandlerTest method testSuccessfulNotification.

@Test
@Ignore
public // todo test does not pass
void testSuccessfulNotification() throws Exception {
    // operation configuration
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_URL_PATH, "http://www.host-does-not-exist.com");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test message");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_MAX_RETRY, "2");
    configurations.put(HttpNotificationWorkflowOperationHandler.OPT_TIMEOUT, Integer.toString(10 * 1000));
    // run the operation handler
    WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
    Assert.assertEquals(result.getAction(), Action.CONTINUE);
}
Also used : HashMap(java.util.HashMap) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 54 with WorkflowOperationResult

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

the class EmailWorkflowOperationHandlerTest method testTemplateInBody.

@Test
public void testTemplateInBody() throws Exception {
    workflowInstance.setTitle("testTemplateInBody");
    operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO);
    operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC);
    operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC);
    operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT);
    operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}");
    WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals(DEFAULT_TO, capturedTo.getValue());
    Assert.assertEquals(DEFAULT_CC, capturedCC.getValue());
    Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue());
    Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue());
    Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue());
}
Also used : WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Example 55 with WorkflowOperationResult

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

the class EmailWorkflowOperationHandlerTest method testTemplateInFile.

@Test
public void testTemplateInFile() throws Exception {
    workflowInstance.setTitle("testTemplateInFile");
    operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO);
    operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC);
    operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC);
    operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT);
    operation.setConfiguration(EmailWorkflowOperationHandler.BODY_TEMPLATE_FILE_PROPERTY, "template1");
    WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    Assert.assertEquals(DEFAULT_TO, capturedTo.getValue());
    Assert.assertEquals(DEFAULT_CC, capturedCC.getValue());
    Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue());
    Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue());
    Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue());
}
Also used : WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Aggregations

WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)101 Test (org.junit.Test)85 HashMap (java.util.HashMap)46 MediaPackage (org.opencastproject.mediapackage.MediaPackage)35 Track (org.opencastproject.mediapackage.Track)34 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)28 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)26 ArrayList (java.util.ArrayList)19 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)19 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)17 Job (org.opencastproject.job.api.Job)13 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)11 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)11 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)11 Catalog (org.opencastproject.mediapackage.Catalog)10 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)9 URI (java.net.URI)7 File (java.io.File)6 Attachment (org.opencastproject.mediapackage.Attachment)6 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)6