Search in sources :

Example 91 with WorkflowOperationInstance

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

the class WorkflowServiceImpl method resume.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#resume(long, Map)
 */
@Override
public WorkflowInstance resume(long workflowInstanceId, Map<String, String> properties) throws WorkflowException, NotFoundException, IllegalStateException, UnauthorizedException {
    WorkflowInstance workflowInstance = getWorkflowById(workflowInstanceId);
    if (!WorkflowState.PAUSED.equals(workflowInstance.getState()))
        throw new IllegalStateException("Can not resume a workflow where the current state is not in paused");
    workflowInstance = updateConfiguration(workflowInstance, properties);
    update(workflowInstance);
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Is the workflow done?
    if (currentOperation == null) {
        // Let's make sure we didn't miss any failed operation, since the workflow state could have been
        // switched to paused while processing the error handling workflow extension
        workflowInstance.setState(SUCCEEDED);
        for (WorkflowOperationInstance op : workflowInstance.getOperations()) {
            if (op.getState().equals(WorkflowOperationInstance.OperationState.FAILED)) {
                if (op.isFailWorkflowOnException()) {
                    workflowInstance.setState(FAILED);
                    break;
                }
            }
        }
        // Save the resumed workflow to the database
        logger.debug("%s has %s", workflowInstance, workflowInstance.getState());
        update(workflowInstance);
        return workflowInstance;
    }
    // certain operations. In the latter case, there is no current paused operation.
    if (OperationState.INSTANTIATED.equals(currentOperation.getState())) {
        try {
            // the operation has its own job. Update that too.
            Job operationJob = serviceRegistry.createJob(JOB_TYPE, Operation.START_OPERATION.toString(), Arrays.asList(Long.toString(workflowInstanceId)), null, false, null, WORKFLOW_JOB_LOAD);
            // this method call is publicly visible, so it doesn't necessarily go through the accept method. Set the
            // workflow state manually.
            workflowInstance.setState(RUNNING);
            currentOperation.setId(operationJob.getId());
            // update the workflow and its associated job
            update(workflowInstance);
            // Now set this job to be queued so it can be dispatched
            operationJob.setStatus(Status.QUEUED);
            operationJob.setDispatchable(true);
            operationJob = serviceRegistry.updateJob(operationJob);
            return workflowInstance;
        } catch (ServiceRegistryException e) {
            throw new WorkflowDatabaseException(e);
        }
    }
    Long operationJobId = workflowInstance.getCurrentOperation().getId();
    if (operationJobId == null)
        throw new IllegalStateException("Can not resume a workflow where the current operation has no associated id");
    // Set the current operation's job to queued, so it gets picked up again
    Job workflowJob;
    try {
        workflowJob = serviceRegistry.getJob(workflowInstanceId);
        workflowJob.setStatus(Status.RUNNING);
        workflowJob.setPayload(WorkflowParser.toXml(workflowInstance));
        workflowJob = serviceRegistry.updateJob(workflowJob);
        Job operationJob = serviceRegistry.getJob(operationJobId);
        operationJob.setStatus(Status.QUEUED);
        operationJob.setDispatchable(true);
        if (properties != null) {
            Properties props = new Properties();
            props.putAll(properties);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            props.store(out, null);
            List<String> newArguments = new ArrayList<String>(operationJob.getArguments());
            newArguments.add(new String(out.toByteArray(), "UTF-8"));
            operationJob.setArguments(newArguments);
        }
        operationJob = serviceRegistry.updateJob(operationJob);
    } catch (ServiceRegistryException e) {
        throw new WorkflowDatabaseException(e);
    } catch (IOException e) {
        throw new WorkflowParsingException("Unable to parse workflow and/or workflow properties");
    }
    return workflowInstance;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collections.mkString(org.opencastproject.util.data.Collections.mkString) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Properties(java.util.Properties) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) Job(org.opencastproject.job.api.Job) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 92 with WorkflowOperationInstance

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

the class WorkflowServiceSolrIndex method createDocument.

/**
 * Adds the workflow instance to the search index.
 *
 * @param instance
 *          the instance
 * @return the solr input document
 * @throws Exception
 */
protected SolrInputDocument createDocument(WorkflowInstance instance) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(ID_KEY, instance.getId());
    doc.addField(WORKFLOW_DEFINITION_KEY, instance.getTemplate());
    doc.addField(STATE_KEY, instance.getState().toString());
    String xml = WorkflowParser.toXml(instance);
    doc.addField(XML_KEY, xml);
    // index the current operation if there is one. If the workflow is finished, there is no current operation, so use a
    // constant
    WorkflowOperationInstance op = instance.getCurrentOperation();
    if (op == null) {
        doc.addField(OPERATION_KEY, NO_OPERATION_KEY);
    } else {
        doc.addField(OPERATION_KEY, op.getTemplate());
    }
    MediaPackage mp = instance.getMediaPackage();
    doc.addField(MEDIAPACKAGE_KEY, mp.getIdentifier().toString());
    if (mp.getSeries() != null) {
        doc.addField(SERIES_ID_KEY, mp.getSeries());
    }
    if (mp.getSeriesTitle() != null) {
        doc.addField(SERIES_TITLE_KEY, mp.getSeriesTitle());
    }
    if (mp.getDate() != null) {
        doc.addField(CREATED_KEY, mp.getDate());
    }
    if (mp.getTitle() != null) {
        doc.addField(TITLE_KEY, mp.getTitle());
    }
    if (mp.getLicense() != null) {
        doc.addField(LICENSE_KEY, mp.getLicense());
    }
    if (mp.getLanguage() != null) {
        doc.addField(LANGUAGE_KEY, mp.getLanguage());
    }
    if (mp.getContributors() != null && mp.getContributors().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String contributor : mp.getContributors()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(contributor);
        }
        doc.addField(CONTRIBUTOR_KEY, buf.toString());
    }
    if (mp.getCreators() != null && mp.getCreators().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String creator : mp.getCreators()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(creator);
        }
        doc.addField(CREATOR_KEY, buf.toString());
    }
    if (mp.getSubjects() != null && mp.getSubjects().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String subject : mp.getSubjects()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(subject);
        }
        doc.addField(SUBJECT_KEY, buf.toString());
    }
    User workflowCreator = instance.getCreator();
    doc.addField(WORKFLOW_CREATOR_KEY, workflowCreator.getUsername());
    doc.addField(ORG_KEY, instance.getOrganization().getId());
    WorkflowInstance.WorkflowState state = instance.getState();
    if (!(WorkflowInstance.WorkflowState.SUCCEEDED.equals(state) || WorkflowInstance.WorkflowState.FAILED.equals(state) || WorkflowInstance.WorkflowState.STOPPED.equals(state))) {
        AccessControlList acl;
        try {
            acl = authorizationService.getActiveAcl(mp).getA();
        } catch (Error e) {
            logger.error("No security xacml found on media package {}", mp);
            throw new WorkflowException(e);
        }
        addAuthorization(doc, acl);
    }
    return doc;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) User(org.opencastproject.security.api.User) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Example 93 with WorkflowOperationInstance

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

the class SilenceDetectionWorkflowOperationHandlerTest method testStartOperation.

@Test
public void testStartOperation() throws WorkflowOperationException, SilenceDetectionFailedException, NotFoundException, ServiceRegistryException, MediaPackageException, SmilException, MalformedURLException, JAXBException, SAXException, IOException {
    Smil smil = smilService.fromXml(new File(smilURI)).getSmil();
    Job job = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(job.getPayload()).andReturn(smil.toXML()).anyTimes();
    EasyMock.expect(job.getStatus()).andReturn(Job.Status.FINISHED);
    EasyMock.expect(silenceDetectionServiceMock.detect((Track) EasyMock.anyObject(), (Track[]) EasyMock.anyObject())).andReturn(job);
    EasyMock.expect(workspaceMock.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(smilURI);
    ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    silenceDetectionOperationHandler.setServiceRegistry(serviceRegistry);
    EasyMock.expect(serviceRegistry.getJob(EasyMock.anyLong())).andReturn(job);
    EasyMock.replay(job, serviceRegistry, silenceDetectionServiceMock, workspaceMock);
    WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mp, getDefaultConfiguration());
    WorkflowOperationResult result = silenceDetectionOperationHandler.start(workflowInstance, null);
    Assert.assertNotNull("SilenceDetectionWorkflowOperationHandler workflow operation returns null " + "but should be an instantiated WorkflowOperationResult", result);
    EasyMock.verify(silenceDetectionServiceMock, workspaceMock);
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String smilFlavorSubtypeProperty = worflowOperationInstance.getConfiguration("smil-flavor-subtype");
    // test media package contains new smil catalog
    MediaPackageElementFlavor smilPartialFlavor = new MediaPackageElementFlavor("*", smilFlavorSubtypeProperty);
    Catalog[] smilCatalogs = mp.getCatalogs(smilPartialFlavor);
    Assert.assertTrue("Media package should contain a smil catalog", smilCatalogs != null && smilCatalogs.length > 0);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) Smil(org.opencastproject.smil.entity.api.Smil) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Job(org.opencastproject.job.api.Job) File(java.io.File) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Track(org.opencastproject.mediapackage.Track) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Catalog(org.opencastproject.mediapackage.Catalog) Test(org.junit.Test)

Example 94 with WorkflowOperationInstance

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

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

the class VideoEditorWorkflowOperationHandlerTest method testEditorOperationInteractiveSkip.

@Test
public void testEditorOperationInteractiveSkip() throws WorkflowOperationException {
    WorkflowInstanceImpl workflowInstance = getWorkflowInstance(mp, getDefaultConfiguration(false));
    WorkflowOperationResult result = videoEditorWorkflowOperationHandler.start(workflowInstance, null);
    Assert.assertNotNull("VideoEditor workflow operation returns null but should be an instantiated WorkflowOperationResult", result);
    // mediapackage should contain new derived track with flavor given by "target-flavor-subtype" configuration
    WorkflowOperationInstance worflowOperationInstance = workflowInstance.getCurrentOperation();
    String targetFlavorSubtypeProperty = worflowOperationInstance.getConfiguration("target-flavor-subtype");
    String skippedFlavorsProperty = worflowOperationInstance.getConfiguration("skipped-flavors");
    TrackSelector trackSelector = new TrackSelector();
    trackSelector.addFlavor(skippedFlavorsProperty);
    Collection<Track> skippedTracks = trackSelector.select(result.getMediaPackage(), false);
    Assert.assertTrue("Mediapackage does not contain any tracks matching flavor " + skippedFlavorsProperty, skippedTracks != null && !skippedTracks.isEmpty());
    for (Track skippedTrack : skippedTracks) {
        MediaPackageElementFlavor derivedTrackFlavor = MediaPackageElementFlavor.flavor(skippedTrack.getFlavor().getType(), targetFlavorSubtypeProperty);
        MediaPackageElement[] derivedElements = result.getMediaPackage().getDerived(skippedTrack, derivedTrackFlavor);
        Assert.assertTrue("Media package should contain track with flavor " + derivedTrackFlavor.toString(), derivedElements != null && derivedElements.length > 0);
    }
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) TrackSelector(org.opencastproject.mediapackage.selector.TrackSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Track(org.opencastproject.mediapackage.Track) 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