Search in sources :

Example 46 with WorkflowInstanceImpl

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

the class WorkflowServiceImpl method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#start(org.opencastproject.workflow.api.WorkflowDefinition,
 *      org.opencastproject.mediapackage.MediaPackage, Long, java.util.Map)
 */
@Override
public WorkflowInstance start(WorkflowDefinition workflowDefinition, MediaPackage sourceMediaPackage, Long parentWorkflowId, Map<String, String> properties) throws WorkflowDatabaseException, WorkflowParsingException, NotFoundException {
    // We have to synchronize per media package to avoid starting multiple simultaneous workflows for one media package.
    final Lock lock = mediaPackageLocks.get(sourceMediaPackage.getIdentifier().toString());
    lock.lock();
    try {
        logger.startUnitOfWork();
        if (workflowDefinition == null)
            throw new IllegalArgumentException("workflow definition must not be null");
        if (sourceMediaPackage == null)
            throw new IllegalArgumentException("mediapackage must not be null");
        for (List<String> errors : MediaPackageSupport.sanityCheck(sourceMediaPackage)) {
            throw new IllegalArgumentException("Insane media package cannot be processed: " + mkString(errors, "; "));
        }
        if (parentWorkflowId != null) {
            try {
                // Let NotFoundException bubble up
                getWorkflowById(parentWorkflowId);
            } catch (UnauthorizedException e) {
                throw new IllegalArgumentException("Parent workflow " + parentWorkflowId + " not visible to this user");
            }
        } else {
            WorkflowQuery wfq = new WorkflowQuery().withMediaPackage(sourceMediaPackage.getIdentifier().compact());
            WorkflowSet mpWorkflowInstances = getWorkflowInstances(wfq);
            if (mpWorkflowInstances.size() > 0) {
                for (WorkflowInstance wfInstance : mpWorkflowInstances.getItems()) {
                    if (wfInstance.isActive())
                        throw new IllegalStateException(String.format("Can't start workflow '%s' for media package '%s' because another workflow is currently active.", workflowDefinition.getTitle(), sourceMediaPackage.getIdentifier().compact()));
                }
            }
        }
        // Get the current user
        User currentUser = securityService.getUser();
        if (currentUser == null)
            throw new SecurityException("Current user is unknown");
        // Get the current organization
        Organization organization = securityService.getOrganization();
        if (organization == null)
            throw new SecurityException("Current organization is unknown");
        WorkflowInstance workflowInstance = new WorkflowInstanceImpl(workflowDefinition, sourceMediaPackage, parentWorkflowId, currentUser, organization, properties);
        workflowInstance = updateConfiguration(workflowInstance, properties);
        // Create and configure the workflow instance
        try {
            // Create a new job for this workflow instance
            String workflowDefinitionXml = WorkflowParser.toXml(workflowDefinition);
            String workflowInstanceXml = WorkflowParser.toXml(workflowInstance);
            String mediaPackageXml = MediaPackageParser.getAsXml(sourceMediaPackage);
            List<String> arguments = new ArrayList<String>();
            arguments.add(workflowDefinitionXml);
            arguments.add(mediaPackageXml);
            if (parentWorkflowId != null || properties != null) {
                String parentWorkflowIdString = (parentWorkflowId != null) ? parentWorkflowId.toString() : NULL_PARENT_ID;
                arguments.add(parentWorkflowIdString);
            }
            if (properties != null) {
                arguments.add(mapToString(properties));
            }
            Job job = serviceRegistry.createJob(JOB_TYPE, Operation.START_WORKFLOW.toString(), arguments, workflowInstanceXml, false, null, WORKFLOW_JOB_LOAD);
            // Have the workflow take on the job's identity
            workflowInstance.setId(job.getId());
            // Add the workflow to the search index and have the job enqueued for dispatch.
            // Update also sets ACL and mediapackage metadata
            update(workflowInstance);
            return workflowInstance;
        } catch (Throwable t) {
            try {
                workflowInstance.setState(FAILED);
                update(workflowInstance);
            } catch (Exception failureToFail) {
                logger.warn(failureToFail, "Unable to update workflow to failed state");
            }
            throw new WorkflowDatabaseException(t);
        }
    } finally {
        logger.endUnitOfWork();
        lock.unlock();
    }
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) Lock(java.util.concurrent.locks.Lock) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Job(org.opencastproject.job.api.Job)

Example 47 with WorkflowInstanceImpl

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

the class WorkflowInstanceTest method testMediaPackageSerializationInWorkflowInstance.

@Test
public void testMediaPackageSerializationInWorkflowInstance() throws Exception {
    WorkflowInstanceImpl workflow = new WorkflowInstanceImpl();
    MediaPackage src = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    Track track = (Track) MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(new URI("http://sample"), Track.TYPE, MediaPackageElements.PRESENTER_SOURCE);
    src.add(track);
    MediaPackage deserialized = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().loadFromXml(MediaPackageParser.getAsXml(src));
    workflow.setMediaPackage(deserialized);
    Assert.assertEquals(1, workflow.getMediaPackage().getTracks().length);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) URI(java.net.URI) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 48 with WorkflowInstanceImpl

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

the class WorkflowInstanceTest method testWorkflowFields.

@Test
public void testWorkflowFields() throws Exception {
    // Workflows should obtain information from the definition used in the constructor
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.setId("123");
    def.setPublished(true);
    Map<String, String> props = new HashMap<String, String>();
    props.put("key1", "value1");
    WorkflowInstance instance = new WorkflowInstanceImpl(def, null, null, null, null, props);
    Assert.assertEquals(def.getId(), instance.getTemplate());
    Assert.assertEquals("value1", instance.getConfiguration("key1"));
    def.setTitle("a title");
    instance = new WorkflowInstanceImpl(def, null, null, null, null, null);
    Assert.assertEquals(def.getTitle(), instance.getTitle());
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) HashMap(java.util.HashMap) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 49 with WorkflowInstanceImpl

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

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

Aggregations

WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)57 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)37 ArrayList (java.util.ArrayList)34 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)31 Test (org.junit.Test)27 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)17 MediaPackage (org.opencastproject.mediapackage.MediaPackage)12 Job (org.opencastproject.job.api.Job)9 Track (org.opencastproject.mediapackage.Track)9 URI (java.net.URI)8 Before (org.junit.Before)8 HashMap (java.util.HashMap)7 NotFoundException (org.opencastproject.util.NotFoundException)7 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)7 MediaPackageBuilder (org.opencastproject.mediapackage.MediaPackageBuilder)6 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)6 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)6 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)5 ServiceRegistry (org.opencastproject.serviceregistry.api.ServiceRegistry)5 File (java.io.File)4