Search in sources :

Example 86 with WorkflowInstance

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

the class HoldStateTest method testHoldAndResume.

@Test
public void testHoldAndResume() throws Exception {
    // Add a listener for paused workflow instances
    WorkflowStateListener pauseListener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(pauseListener);
    Map<String, String> initialProps = new HashMap<String, String>();
    initialProps.put("testproperty", "foo");
    synchronized (pauseListener) {
        workflow = service.start(def, mp, initialProps);
        pauseListener.wait();
    }
    service.removeWorkflowListener(pauseListener);
    // The variable "testproperty" should have been replaced by "foo", but not "anotherproperty"
    Assert.assertEquals("foo", workflow.getOperations().get(0).getConfiguration("testkey"));
    Assert.assertEquals("${anotherproperty}", workflow.getOperations().get(1).getConfiguration("testkey"));
    // Simulate a user resuming and submitting new properties (this time, with a value for "anotherproperty") to the
    // workflow
    Map<String, String> resumeProps = new HashMap<String, String>();
    resumeProps.put("anotherproperty", "bar");
    WorkflowStateListener succeedListener = new WorkflowStateListener(WorkflowState.SUCCEEDED);
    service.addWorkflowListener(succeedListener);
    synchronized (succeedListener) {
        service.resume(workflow.getId(), resumeProps);
        succeedListener.wait();
    }
    service.removeWorkflowListener(succeedListener);
    Assert.assertEquals("Workflow expected to succeed", 1, succeedListener.countStateChanges(WorkflowState.SUCCEEDED));
    WorkflowInstance fromDb = service.getWorkflowById(workflow.getId());
    logger.info("checking for the existence of 'anotherproperty', which should have been replaced");
    Assert.assertEquals("foo", fromDb.getOperations().get(0).getConfiguration("testkey"));
    Assert.assertEquals("bar", fromDb.getOperations().get(1).getConfiguration("testkey"));
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) HashMap(java.util.HashMap) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 87 with WorkflowInstance

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

the class HoldStateTest method testMultipleHolds.

@Test
public void testMultipleHolds() throws Exception {
    WorkflowStateListener pauseListener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(pauseListener);
    synchronized (pauseListener) {
        workflow = service.start(def, mp);
        pauseListener.wait();
    }
    // Simulate a user resuming the workflow, but the handler still keeps the workflow in a hold state
    holdingOperationHandler.setResumeAction(Action.PAUSE);
    // Resume the workflow again. It should quickly reenter the paused state
    synchronized (pauseListener) {
        service.resume(workflow.getId());
        pauseListener.wait();
    }
    // remove the pause listener
    service.removeWorkflowListener(pauseListener);
    WorkflowInstance fromDb = service.getWorkflowById(workflow.getId());
    Assert.assertEquals(WorkflowState.PAUSED, fromDb.getState());
    // Resume the workflow again, and this time continue with the workflow
    holdingOperationHandler.setResumeAction(Action.CONTINUE);
    WorkflowStateListener succeedListener = new WorkflowStateListener(WorkflowState.SUCCEEDED, WorkflowState.FAILED);
    service.addWorkflowListener(succeedListener);
    synchronized (succeedListener) {
        service.resume(workflow.getId());
        succeedListener.wait();
    }
    service.removeWorkflowListener(succeedListener);
    Assert.assertEquals(WorkflowState.SUCCEEDED, service.getWorkflowById(workflow.getId()).getState());
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 88 with WorkflowInstance

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

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

the class WorkflowRestService method update.

@POST
@Path("update")
@RestQuery(name = "update", description = "Updates a workflow instance.", returnDescription = "No content.", restParameters = { @RestParameter(name = "workflow", isRequired = true, description = "The XML representation of the workflow instance.", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "Workflow instance updated.") })
public Response update(@FormParam("workflow") String workflowInstance) throws NotFoundException, UnauthorizedException {
    try {
        WorkflowInstance instance = WorkflowParser.parseWorkflowInstance(workflowInstance);
        service.update(instance);
        return Response.noContent().build();
    } catch (WorkflowException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 90 with WorkflowInstance

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

the class WorkflowRestService method resume.

@POST
@Path("replaceAndresume")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "replaceAndresume", description = "Replaces a suspended workflow instance with an updated version, and resumes the workflow.", returnDescription = "An XML representation of the updated and resumed workflow instance", restParameters = { @RestParameter(name = "id", isRequired = true, description = "The workflow instance identifier", type = STRING), @RestParameter(name = "mediapackage", isRequired = false, description = "The new Mediapackage", type = TEXT), @RestParameter(name = "properties", isRequired = false, description = "Properties", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the updated and resumed workflow instance."), @RestResponse(responseCode = SC_CONFLICT, description = "Can not resume workflow not in paused state"), @RestResponse(responseCode = SC_NOT_FOUND, description = "No suspended workflow instance with that identifier exists."), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to resume. Maybe you need to authenticate.") })
public Response resume(@FormParam("id") long workflowInstanceId, @FormParam("mediapackage") final String mediaPackage, @FormParam("properties") LocalHashMap properties) throws NotFoundException, UnauthorizedException {
    final Map<String, String> map;
    if (properties == null) {
        map = new HashMap<String, String>();
    } else {
        map = properties.getMap();
    }
    final Lock lock = this.lock.get(workflowInstanceId);
    lock.lock();
    try {
        WorkflowInstance workflow = service.getWorkflowById(workflowInstanceId);
        if (!WorkflowState.PAUSED.equals(workflow.getState())) {
            logger.warn("Can not resume workflow '{}', not in state paused but {}", workflow, workflow.getState());
            return Response.status(Status.CONFLICT).build();
        }
        if (mediaPackage != null) {
            MediaPackage newMp = MediaPackageParser.getFromXml(mediaPackage);
            MediaPackage oldMp = workflow.getMediaPackage();
            // Delete removed elements from workspace
            for (MediaPackageElement elem : oldMp.getElements()) {
                if (MediaPackageSupport.contains(elem.getIdentifier(), newMp))
                    continue;
                try {
                    workspace.delete(elem.getURI());
                    logger.info("Deleted removed mediapackge element {}", elem);
                } catch (NotFoundException e) {
                    logger.info("Removed mediapackage element {} is already deleted", elem);
                }
            }
            workflow.setMediaPackage(newMp);
            service.update(workflow);
        }
        workflow = service.resume(workflowInstanceId, map);
        return Response.ok(workflow).build();
    } catch (NotFoundException e) {
        return Response.status(Status.NOT_FOUND).build();
    } catch (UnauthorizedException e) {
        return Response.status(Status.UNAUTHORIZED).build();
    } catch (IllegalStateException e) {
        logger.warn(ExceptionUtils.getMessage(e));
        return Response.status(Status.CONFLICT).build();
    } catch (WorkflowException e) {
        logger.error(ExceptionUtils.getMessage(e), e);
        return Response.serverError().build();
    } catch (Exception e) {
        logger.error(ExceptionUtils.getMessage(e), e);
        return Response.serverError().build();
    } finally {
        lock.unlock();
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URISyntaxException(java.net.URISyntaxException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)94 Test (org.junit.Test)48 MediaPackage (org.opencastproject.mediapackage.MediaPackage)40 NotFoundException (org.opencastproject.util.NotFoundException)26 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)24 HashMap (java.util.HashMap)22 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)20 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)19 ArrayList (java.util.ArrayList)16 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)16 IOException (java.io.IOException)15 Organization (org.opencastproject.security.api.Organization)15 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)14 WorkflowException (org.opencastproject.workflow.api.WorkflowException)13 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)12 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)12 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)11 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)10 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)9 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)9