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);
}
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());
}
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);
}
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;
}
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());
}
Aggregations