Search in sources :

Example 16 with WorkflowDefinition

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

the class AbstractEventEndpoint method getNewProcessing.

@GET
@Path("new/processing")
@RestQuery(name = "getNewProcessing", description = "Returns all the data related to the processing tab in the new event modal as JSON", returnDescription = "All the data related to the event processing tab as JSON", restParameters = { @RestParameter(name = "tags", isRequired = false, description = "A comma separated list of tags to filter the workflow definitions", type = RestParameter.Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "Returns all the data related to the event processing tab as JSON") })
public Response getNewProcessing(@QueryParam("tags") String tagsString) {
    List<String> tags = RestUtil.splitCommaSeparatedParam(Option.option(tagsString)).value();
    List<JValue> workflows = new ArrayList<>();
    try {
        List<WorkflowDefinition> workflowsDefinitions = getWorkflowService().listAvailableWorkflowDefinitions();
        for (WorkflowDefinition wflDef : workflowsDefinitions) {
            if (wflDef.containsTag(tags)) {
                workflows.add(obj(f("id", v(wflDef.getId())), f("title", v(nul(wflDef.getTitle()).getOr(""))), f("description", v(nul(wflDef.getDescription()).getOr(""))), f("configuration_panel", v(nul(wflDef.getConfigurationPanel()).getOr("")))));
            }
        }
    } catch (WorkflowDatabaseException e) {
        logger.error("Unable to get available workflow definitions: {}", ExceptionUtils.getStackTrace(e));
        return RestUtil.R.serverError();
    }
    JValue data = obj(f("workflows", arr(workflows)), f("default_workflow_id", v(defaultWorkflowDefinionId, Jsons.NULL)));
    return okJson(data);
}
Also used : WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) JValue(com.entwinemedia.fn.data.json.JValue) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 17 with WorkflowDefinition

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

the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherRunProgressState.

@Test
public void testWorkflowDispatcherRunProgressState() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
    database.storeJobControl("mpId2", "audioTrack2", "jobId2", TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    EasyMock.expect(workspace.putInCollection(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(InputStream.class))).andReturn(new URI("http://anything"));
    EasyMock.replay(workspace);
    HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent()).andReturn(stream);
    CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
    StatusLine status = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
    EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
    EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes();
    EasyMock.replay(httpEntity, response, status);
    Capture<HttpGet> capturedGet = Capture.newInstance();
    EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response);
    EasyMock.replay(httpClient);
    // enrich(q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(q.version().isLatest())).run()).getSnapshots();
    // Mocks for query, result, etc
    Snapshot snapshot = EasyMock.createNiceMock(Snapshot.class);
    EasyMock.expect(snapshot.getOrganizationId()).andReturn(org.getId());
    ARecord aRec = EasyMock.createNiceMock(ARecord.class);
    EasyMock.expect(aRec.getSnapshot()).andReturn(Opt.some(snapshot));
    Stream<ARecord> recStream = Stream.mk(aRec);
    Predicate p = EasyMock.createNiceMock(Predicate.class);
    EasyMock.expect(p.and(p)).andReturn(p);
    AResult r = EasyMock.createNiceMock(AResult.class);
    EasyMock.expect(r.getSize()).andReturn(1L);
    EasyMock.expect(r.getRecords()).andReturn(recStream);
    Target t = EasyMock.createNiceMock(Target.class);
    ASelectQuery selectQuery = EasyMock.createNiceMock(ASelectQuery.class);
    EasyMock.expect(selectQuery.where(EasyMock.anyObject(Predicate.class))).andReturn(selectQuery);
    EasyMock.expect(selectQuery.run()).andReturn(r);
    AQueryBuilder query = EasyMock.createNiceMock(AQueryBuilder.class);
    EasyMock.expect(query.snapshot()).andReturn(t);
    EasyMock.expect(query.mediaPackageId(EasyMock.anyObject(String.class))).andReturn(p);
    EasyMock.expect(query.select(EasyMock.anyObject(Target.class))).andReturn(selectQuery);
    VersionField v = EasyMock.createNiceMock(VersionField.class);
    EasyMock.expect(v.isLatest()).andReturn(p);
    EasyMock.expect(query.version()).andReturn(v);
    EasyMock.expect(assetManager.createQuery()).andReturn(query);
    EasyMock.replay(snapshot, aRec, p, r, t, selectQuery, query, v, assetManager);
    Capture<Set<String>> capturedMpIds = Capture.newInstance();
    WorkflowDefinition wfDef = new WorkflowDefinitionImpl();
    EasyMock.expect(wfService.getWorkflowDefinitionById(IBMWatsonTranscriptionService.DEFAULT_WF_DEF)).andReturn(wfDef);
    List<WorkflowInstance> wfList = new ArrayList<WorkflowInstance>();
    wfList.add(new WorkflowInstanceImpl());
    Stream<WorkflowInstance> wfListStream = Stream.mk(wfList);
    Workflows wfs = EasyMock.createNiceMock(Workflows.class);
    EasyMock.expect(wfs.applyWorkflowToLatestVersion(EasyMock.capture(capturedMpIds), EasyMock.anyObject(ConfiguredWorkflow.class))).andReturn(wfListStream);
    service.setWfUtil(wfs);
    EasyMock.replay(wfService, wfs);
    WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
    dispatcher.run();
    // Check if it called the external service to get the results
    Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
    // Check if only one mp has a workflow created for it
    Assert.assertEquals(1, capturedMpIds.getValue().size());
    // And if it was the correct one
    Assert.assertEquals(MP_ID, capturedMpIds.getValue().iterator().next());
    // Check if status in db was updated
    TranscriptionJobControl job = database.findByJob(JOB_ID);
    Assert.assertNotNull(job);
    Assert.assertEquals(TranscriptionJobControl.Status.Closed.name(), job.getStatus());
}
Also used : Set(java.util.Set) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) WorkflowDispatcher(org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) Predicate(org.opencastproject.assetmanager.api.query.Predicate) ARecord(org.opencastproject.assetmanager.api.query.ARecord) Target(org.opencastproject.assetmanager.api.query.Target) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AResult(org.opencastproject.assetmanager.api.query.AResult) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) Workflows(org.opencastproject.assetmanager.util.Workflows) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VersionField(org.opencastproject.assetmanager.api.query.VersionField) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) ConfiguredWorkflow(org.opencastproject.workflow.api.ConfiguredWorkflow) StatusLine(org.apache.http.StatusLine) Snapshot(org.opencastproject.assetmanager.api.Snapshot) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) Test(org.junit.Test)

Example 18 with WorkflowDefinition

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

the class WorkflowServiceImpl method handleFailedOperation.

/**
 * Handles the workflow for a failing operation.
 *
 * @param workflow
 *          the workflow
 * @param currentOperation
 *          the failing workflow operation instance
 * @throws WorkflowDatabaseException
 *           If the exception handler workflow is not found
 */
private void handleFailedOperation(WorkflowInstance workflow, WorkflowOperationInstance currentOperation) throws WorkflowDatabaseException {
    String errorDefId = currentOperation.getExceptionHandlingWorkflow();
    // Adjust the workflow state according to the setting on the operation
    if (currentOperation.isFailWorkflowOnException()) {
        if (StringUtils.isBlank(errorDefId)) {
            workflow.setState(FAILED);
        } else {
            workflow.setState(FAILING);
            // Remove the rest of the original workflow
            int currentOperationPosition = workflow.getOperations().indexOf(currentOperation);
            List<WorkflowOperationInstance> operations = new ArrayList<WorkflowOperationInstance>();
            operations.addAll(workflow.getOperations().subList(0, currentOperationPosition + 1));
            workflow.setOperations(operations);
            // Determine the current workflow configuration
            Map<String, String> configuration = new HashMap<String, String>();
            for (String configKey : workflow.getConfigurationKeys()) {
                configuration.put(configKey, workflow.getConfiguration(configKey));
            }
            // Append the operations
            WorkflowDefinition errorDef = null;
            try {
                errorDef = getWorkflowDefinitionById(errorDefId);
                workflow.extend(errorDef);
                workflow.setOperations(updateConfiguration(workflow, configuration).getOperations());
            } catch (NotFoundException notFoundException) {
                throw new IllegalStateException("Unable to find the error workflow definition '" + errorDefId + "'");
            }
        }
    }
    // Fail the current operation
    currentOperation.setState(OperationState.FAILED);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString)

Example 19 with WorkflowDefinition

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

the class WorkflowServiceImpl method isReadyToAccept.

/**
 * {@inheritDoc}
 *
 * If we are already running the maximum number of workflows, don't accept another START_WORKFLOW job
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#isReadyToAccept(org.opencastproject.job.api.Job)
 */
@Override
public boolean isReadyToAccept(Job job) throws ServiceRegistryException, UndispatchableJobException {
    String operation = job.getOperation();
    // Only restrict execution of new jobs
    if (!Operation.START_WORKFLOW.toString().equals(operation))
        return true;
    // If the first operation is guaranteed to pause, run the job.
    if (job.getArguments().size() > 1 && job.getArguments().get(0) != null) {
        try {
            WorkflowDefinition workflowDef = WorkflowParser.parseWorkflowDefinition(job.getArguments().get(0));
            if (workflowDef.getOperations().size() > 0) {
                String firstOperationId = workflowDef.getOperations().get(0).getId();
                WorkflowOperationHandler handler = getWorkflowOperationHandler(firstOperationId);
                if (handler instanceof ResumableWorkflowOperationHandler) {
                    if (((ResumableWorkflowOperationHandler) handler).isAlwaysPause()) {
                        return true;
                    }
                }
            }
        } catch (WorkflowParsingException e) {
            throw new UndispatchableJobException(job + " is not a proper job to start a workflow", e);
        }
    }
    WorkflowInstance workflow = null;
    WorkflowSet workflowInstances = null;
    String mediaPackageId = null;
    // Fetch all workflows that are running with the current mediapackage
    try {
        workflow = getWorkflowById(job.getId());
        mediaPackageId = workflow.getMediaPackage().getIdentifier().toString();
        workflowInstances = getWorkflowInstances(new WorkflowQuery().withMediaPackage(workflow.getMediaPackage().getIdentifier().toString()).withState(RUNNING).withState(PAUSED).withState(FAILING));
    } catch (NotFoundException e) {
        logger.error("Trying to start workflow with id %s but no corresponding instance is available from the workflow service", job.getId());
        throw new UndispatchableJobException(e);
    } catch (UnauthorizedException e) {
        logger.error("Authorization denied while requesting to loading workflow instance %s: %s", job.getId(), e.getMessage());
        throw new UndispatchableJobException(e);
    } catch (WorkflowDatabaseException e) {
        logger.error("Error loading workflow instance %s: %s", job.getId(), e.getMessage());
        return false;
    }
    // If more than one workflow is running working on this mediapackage, then we don't start this one
    boolean toomany = workflowInstances.size() > 1;
    // Make sure we are not excluding ourselves
    toomany |= workflowInstances.size() == 1 && workflow.getId() != workflowInstances.getItems()[0].getId();
    // Avoid running multiple workflows with same media package id at the same time
    if (toomany) {
        if (!delayedWorkflows.contains(workflow.getId())) {
            logger.info("Delaying start of workflow %s, another workflow on media package %s is still running", workflow.getId(), mediaPackageId);
            delayedWorkflows.add(workflow.getId());
        }
        return false;
    }
    return true;
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 20 with WorkflowDefinition

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

the class WorkflowInstanceTest method testWorkflowDefinitionDeserialization.

@Test
public void testWorkflowDefinitionDeserialization() throws Exception {
    InputStream in = getClass().getResourceAsStream("/workflow-definition-1.xml");
    WorkflowDefinition def = WorkflowParser.parseWorkflowDefinition(in);
    IOUtils.closeQuietly(in);
    Assert.assertEquals("The First Workflow Definition", def.getTitle());
    Assert.assertEquals(2, def.getOperations().size());
    Assert.assertEquals("definition-1", def.getId());
    Assert.assertEquals("Unit testing workflow", def.getDescription());
    Assert.assertTrue(def.isPublished());
}
Also used : InputStream(java.io.InputStream) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) Test(org.junit.Test)

Aggregations

WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)22 ArrayList (java.util.ArrayList)9 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)6 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)6 Path (javax.ws.rs.Path)5 Test (org.junit.Test)5 NotFoundException (org.opencastproject.util.NotFoundException)5 RestQuery (org.opencastproject.util.doc.rest.RestQuery)5 InputStream (java.io.InputStream)4 GET (javax.ws.rs.GET)4 JValue (com.entwinemedia.fn.data.json.JValue)3 IOException (java.io.IOException)3 Set (java.util.Set)3 Before (org.junit.Before)3 Workflows (org.opencastproject.assetmanager.util.Workflows)3 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)3 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)3 File (java.io.File)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2