Search in sources :

Example 76 with WorkflowOperationInstance

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

the class WorkflowServiceImplTest method testRetryStrategyHoldFailureByUser.

@Test
public void testRetryStrategyHoldFailureByUser() throws Exception {
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.setId("workflow-definition-1");
    def.setTitle("workflow-definition-1");
    def.setDescription("workflow-definition-1");
    def.setPublished(true);
    service.registerWorkflowDefinition(def);
    WorkflowOperationDefinitionImpl opDef = new WorkflowOperationDefinitionImpl("failTwice", "fails twice", null, true);
    opDef.setRetryStrategy(RetryStrategy.HOLD);
    opDef.setMaxAttempts(3);
    opDef.setFailWorkflowOnException(true);
    def.add(opDef);
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    WorkflowInstance workflow = startAndWait(def, mp, WorkflowState.PAUSED);
    WorkflowOperationInstance errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(0);
    WorkflowOperationInstance failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
    Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
    Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
    Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.RETRY);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 1);
    // Try operation a second time, simulate user selecting RETRY
    retryAndWait(service.getWorkflowById(workflow.getId()), "RETRY", WorkflowState.PAUSED);
    errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
    failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(2);
    Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
    Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
    Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.RETRY);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 2);
    // Simulate user selecting 'fail'
    retryAndWait(service.getWorkflowById(workflow.getId()), "NONE", WorkflowState.FAILED);
    failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(2);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.FAILED);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 2);
}
Also used : WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 77 with WorkflowOperationInstance

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

the class CloneWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running clone workflow operation on workflow {}", workflowInstance.getId());
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_TAGS));
    String sourceFlavorOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_FLAVOR));
    String targetFlavorOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_FLAVOR));
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector = new SimpleElementSelector();
    // Make sure either one of tags or flavors are provided
    if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorOption)) {
        logger.info("No source tags or flavors have been specified, not matching anything. Operation will be skipped.");
        return createResult(mediaPackage, Action.SKIP);
    }
    // if no source-favor is specified, all flavors will be checked for given tags
    if (sourceFlavorOption == null) {
        sourceFlavorOption = "*/*";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("Parameters passed to clone workflow operation:");
    sb.append("\n source-tags: ").append(sourceTagsOption);
    sb.append("\n source-flavor: ").append(sourceFlavorOption);
    sb.append("\n target-flavor: ").append(targetFlavorOption);
    logger.debug(sb.toString());
    // Select the source flavors
    MediaPackageElementFlavor sourceFlavor = MediaPackageElementFlavor.parseFlavor(sourceFlavorOption);
    elementSelector.addFlavor(sourceFlavor);
    // Select the source tags
    for (String tag : asList(sourceTagsOption)) {
        elementSelector.addTag(tag);
    }
    // Look for elements matching the tags and the flavor
    Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
    // Check the the number of element returned
    if (elements.size() == 0) {
        // If no one found, we skip the operation
        logger.debug("No matching elements found, skipping operation.");
        return createResult(workflowInstance.getMediaPackage(), Action.SKIP);
    } else {
        logger.debug("Copy " + elements.size() + " elements to new flavor: {}", targetFlavorOption);
        MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
        for (MediaPackageElement element : elements) {
            MediaPackageElementFlavor flavor;
            // Take subtype either from option or from element (if option is *)
            String subtype;
            if ("*".equals(targetFlavor.getSubtype()))
                subtype = element.getFlavor().getSubtype();
            else
                subtype = targetFlavor.getSubtype();
            // Take type either from option or from element (if option is *)
            if ("*".equals(targetFlavor.getType()))
                flavor = new MediaPackageElementFlavor(element.getFlavor().getType(), subtype);
            else
                flavor = new MediaPackageElementFlavor(targetFlavor.getType(), subtype);
            // Copy element and set new flavor
            MediaPackageElement newElement = copyElement(element);
            newElement.setFlavor(flavor);
            mediaPackage.add(newElement);
        }
    }
    return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 78 with WorkflowOperationInstance

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

the class CopyWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running copy workflow operation on workflow {}", workflowInstance.getId());
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_TAGS));
    String sourceFlavorsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_FLAVORS));
    String targetDirectoryOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_DIRECTORY));
    Option<String> targetFilenameOption = Option.option(StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_FILENAME)));
    StringBuilder sb = new StringBuilder();
    sb.append("Parameters passed to copy workflow operation:");
    sb.append("\n source-tags: ").append(sourceTagsOption);
    sb.append("\n source-flavors: ").append(sourceFlavorsOption);
    sb.append("\n target-directory: ").append(targetDirectoryOption);
    sb.append("\n target-filename: ").append(targetFilenameOption);
    logger.debug(sb.toString());
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector = new SimpleElementSelector();
    // Make sure either one of tags or flavors are provided
    if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorsOption)) {
        logger.info("No source tags or flavors have been specified, not matching anything");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Make the target filename and directory are provided
    if (StringUtils.isBlank(targetDirectoryOption))
        throw new WorkflowOperationException("No target directory has been set for the copy operation!");
    // Select the source flavors
    for (String flavor : asList(sourceFlavorsOption)) {
        try {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
        }
    }
    // Select the source tags
    for (String tag : asList(sourceTagsOption)) {
        elementSelector.addTag(tag);
    }
    // Look for elements matching the tag
    Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
    // Check the the number of element returned
    if (elements.size() == 0) {
        // If no one found, we skip the operation
        return createResult(workflowInstance.getMediaPackage(), Action.SKIP);
    } else if (elements.size() == 1) {
        for (MediaPackageElement element : elements) {
            logger.debug("Copy single element to: {}", targetDirectoryOption);
            final String fileName;
            if (targetFilenameOption.isSome()) {
                fileName = targetFilenameOption.get();
            } else {
                fileName = FilenameUtils.getBaseName(element.getURI().toString());
            }
            String ext = FilenameUtils.getExtension(element.getURI().toString());
            ext = ext.length() > 0 ? ".".concat(ext) : "";
            File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName.concat(ext)));
            copyElement(element, targetFile);
        }
    } else {
        logger.debug("Copy multiple elements to: {}", targetDirectoryOption);
        int i = 1;
        for (MediaPackageElement element : elements) {
            final String fileName;
            if (targetFilenameOption.isSome()) {
                fileName = String.format(targetFilenameOption.get(), i);
            } else {
                fileName = FilenameUtils.getBaseName(element.getURI().toString());
            }
            String ext = FilenameUtils.getExtension(element.getURI().toString());
            ext = ext.length() > 0 ? ".".concat(ext) : "";
            File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName + ext));
            copyElement(element, targetFile);
            i++;
        }
    }
    return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) File(java.io.File)

Example 79 with WorkflowOperationInstance

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

the class ImportWorkflowPropertiesWOHTest method testStartOp.

@Test
public void testStartOp() throws Exception {
    final WorkflowOperationInstance woi = createMock(WorkflowOperationInstance.class);
    expect(woi.getConfiguration("source-flavor")).andStubReturn(FLAVOR);
    expect(woi.getConfiguration("keys")).andStubReturn("chapter, presenter_position, cover_marker_in_s");
    replay(woi);
    final Attachment att = new AttachmentImpl();
    att.setURI(new URI(WF_PROPS_ATT_URI));
    att.setFlavor(MediaPackageElementFlavor.parseFlavor(FLAVOR));
    final MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    mp.add(att);
    WorkflowInstance wi = createMock(WorkflowInstance.class);
    expect(wi.getCurrentOperation()).andStubReturn(woi);
    expect(wi.getMediaPackage()).andStubReturn(mp);
    replay(wi);
    try (InputStream is = ImportWorkflowPropertiesWOHTest.class.getResourceAsStream("/workflow-properties.xml")) {
        Files.copy(is, tmpPropsFile, StandardCopyOption.REPLACE_EXISTING);
    }
    final Workspace workspace = createNiceMock(Workspace.class);
    expect(workspace.get(new URI(WF_PROPS_ATT_URI))).andStubReturn(tmpPropsFile.toFile());
    replay(workspace);
    final ImportWorkflowPropertiesWOH woh = new ImportWorkflowPropertiesWOH();
    woh.setWorkspace(workspace);
    WorkflowOperationResult result = woh.start(wi, null);
    Map<String, String> properties = result.getProperties();
    Assert.assertTrue(properties.containsKey("chapter"));
    Assert.assertEquals("true", properties.get("chapter"));
    Assert.assertTrue(properties.containsKey("presenter_position"));
    Assert.assertEquals("left", properties.get("presenter_position"));
    Assert.assertTrue(properties.containsKey("cover_marker_in_s"));
    Assert.assertEquals("30.674", properties.get("cover_marker_in_s"));
    verify(wi);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) InputStream(java.io.InputStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Attachment(org.opencastproject.mediapackage.Attachment) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Workspace(org.opencastproject.workspace.api.Workspace) Test(org.junit.Test)

Example 80 with WorkflowOperationInstance

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

the class SeriesWorkflowOperationHandlerTest method testAclOnly.

@Test
public void testAclOnly() throws Exception {
    WorkflowInstanceImpl instance = new WorkflowInstanceImpl();
    List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>();
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED);
    ops.add(operation);
    instance.setOperations(ops);
    instance.setMediaPackage(mp);
    operation.setConfiguration(SeriesWorkflowOperationHandler.SERIES_PROPERTY, "series1");
    operation.setConfiguration(SeriesWorkflowOperationHandler.ATTACH_PROPERTY, "");
    operation.setConfiguration(SeriesWorkflowOperationHandler.APPLY_ACL_PROPERTY, "true");
    WorkflowOperationResult result = operationHandler.start(instance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Aggregations

WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)104 ArrayList (java.util.ArrayList)51 MediaPackage (org.opencastproject.mediapackage.MediaPackage)48 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)37 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)33 Test (org.junit.Test)32 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)31 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)28 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)24 Job (org.opencastproject.job.api.Job)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)19 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)19 URI (java.net.URI)18 NotFoundException (org.opencastproject.util.NotFoundException)16 Track (org.opencastproject.mediapackage.Track)14 IOException (java.io.IOException)13 File (java.io.File)12 HashMap (java.util.HashMap)12 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10