use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowServiceImpl method process.
/**
* Processes the workflow job.
*
* @param job
* the job
* @return the job payload
* @throws Exception
* if job processing fails
*/
protected String process(Job job) throws Exception {
List<String> arguments = job.getArguments();
Operation op = null;
WorkflowInstance workflowInstance = null;
WorkflowOperationInstance wfo = null;
String operation = job.getOperation();
try {
try {
op = Operation.valueOf(operation);
switch(op) {
case START_WORKFLOW:
workflowInstance = WorkflowParser.parseWorkflowInstance(job.getPayload());
logger.debug("Starting new workflow %s", workflowInstance);
runWorkflow(workflowInstance);
break;
case RESUME:
workflowInstance = getWorkflowById(Long.parseLong(arguments.get(0)));
wfo = workflowInstance.getCurrentOperation();
Map<String, String> properties = null;
if (arguments.size() > 1) {
Properties props = new Properties();
props.load(IOUtils.toInputStream(arguments.get(arguments.size() - 1)));
properties = new HashMap<String, String>();
for (Entry<Object, Object> entry : props.entrySet()) {
properties.put(entry.getKey().toString(), entry.getValue().toString());
}
}
logger.debug("Resuming %s at %s", workflowInstance, workflowInstance.getCurrentOperation());
workflowInstance.setState(RUNNING);
update(workflowInstance);
wfo = runWorkflowOperation(workflowInstance, properties);
break;
case START_OPERATION:
workflowInstance = getWorkflowById(Long.parseLong(arguments.get(0)));
wfo = workflowInstance.getCurrentOperation();
if (OperationState.RUNNING.equals(wfo.getState()) || OperationState.PAUSED.equals(wfo.getState())) {
logger.info("Reset operation state %s %s to INSTANTIATED due to job restart", workflowInstance, wfo);
wfo.setState(OperationState.INSTANTIATED);
}
wfo.setExecutionHost(job.getProcessingHost());
logger.debug("Running %s %s", workflowInstance, wfo);
wfo = runWorkflowOperation(workflowInstance, null);
updateOperationJob(job.getId(), wfo.getState());
break;
default:
throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
}
} catch (IllegalArgumentException e) {
throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
} catch (IndexOutOfBoundsException e) {
throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
} catch (NotFoundException e) {
logger.warn(e.getMessage());
updateOperationJob(job.getId(), OperationState.FAILED);
}
return null;
} catch (Exception e) {
logger.warn(e, "Exception while accepting job " + job);
try {
if (workflowInstance != null) {
logger.warn("Marking job {} and workflow instance {} as failed", job, workflowInstance);
updateOperationJob(job.getId(), OperationState.FAILED);
workflowInstance.setState(FAILED);
update(workflowInstance);
} else {
logger.warn(e, "Unable to parse workflow instance");
}
} catch (WorkflowDatabaseException e1) {
throw new ServiceRegistryException(e1);
}
if (e instanceof ServiceRegistryException)
throw e;
throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
}
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowInstanceTest method testFlavorMarshalling.
@Test
public void testFlavorMarshalling() throws Exception {
URI uri = new URI("http://testing");
Track track = TrackImpl.fromURI(uri);
track.setFlavor(MediaPackageElements.PRESENTATION_SOURCE);
MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
mp.add(track);
WorkflowInstance workflow = new WorkflowInstanceImpl();
workflow.setMediaPackage(mp);
// Marshall the workflow to xml
String xml = WorkflowParser.toXml(workflow);
// Get it back from xml
WorkflowInstance instance2 = WorkflowParser.parseWorkflowInstance(xml);
Assert.assertEquals(workflow.getMediaPackage().getTracks()[0].getFlavor(), instance2.getMediaPackage().getTracks()[0].getFlavor());
String namespaceXml = "<workflow xmlns=\"http://workflow.opencastproject.org\" xmlns:mp=\"http://mediapackage.opencastproject.org\"><parent/><mp:mediapackage><mp:media><mp:track type=\"presentation/source\" id=\"track-1\"><mp:url>http://testing</mp:url></mp:track></mp:media></mp:mediapackage></workflow>";
WorkflowInstance instance3 = WorkflowParser.parseWorkflowInstance(namespaceXml);
Assert.assertEquals(workflow.getMediaPackage().getTracks()[0].getFlavor(), instance3.getMediaPackage().getTracks()[0].getFlavor());
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowRestEndpointTest method testGetWorkflowInstance.
@Test
public void testGetWorkflowInstance() throws Exception {
try {
restService.getWorkflowAsJson(-1);
Assert.fail("This should have thrown a not found exception");
} catch (NotFoundException e) {
// expected
}
try {
restService.getWorkflowAsXml(-1);
Assert.fail("This should have thrown a not found exception");
} catch (NotFoundException e) {
// expected
}
WorkflowInstance xmlResponse = restService.getWorkflowAsXml(1);
Assert.assertNotNull(xmlResponse);
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowServiceImplTest method testRetryStrategyHold.
@Test
public void testRetryStrategyHold() throws Exception {
WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
def.setId("workflow-definition-1");
def.setTitle("workflow-definition-1");
def.setDescription("workflow-definition-1");
def.setPublished(true);
service.registerWorkflowDefinition(def);
WorkflowOperationDefinitionImpl opDef = new WorkflowOperationDefinitionImpl("failOneTime", "fails once", null, true);
opDef.setRetryStrategy(RetryStrategy.HOLD);
opDef.setMaxAttempts(2);
def.add(opDef);
MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
WorkflowInstance workflow = startAndWait(def, mp, WorkflowState.PAUSED);
WorkflowOperationInstance errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(0);
WorkflowOperationInstance failOneTimeOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
Assert.assertTrue(failOneTimeOperation.getState() == OperationState.RETRY);
Assert.assertTrue(failOneTimeOperation.getMaxAttempts() == 2);
Assert.assertTrue(failOneTimeOperation.getFailedAttempts() == 1);
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowServiceImplTest method testRemove.
/**
* Test for {@link WorkflowServiceImpl#remove(long)}
*
* @throws Exception
* if anything fails
*/
@Test
public void testRemove() throws Exception {
WorkflowInstance wi1 = startAndWait(workingDefinition, mediapackage1, WorkflowState.SUCCEEDED);
WorkflowInstance wi2 = startAndWait(workingDefinition, mediapackage2, WorkflowState.SUCCEEDED);
// reload instances, because operations have no id before
wi1 = service.getWorkflowById(wi1.getId());
wi2 = service.getWorkflowById(wi2.getId());
service.remove(wi1.getId());
assertEquals(1, service.getWorkflowInstances(new WorkflowQuery()).size());
for (WorkflowOperationInstance op : wi1.getOperations()) {
assertEquals(0, serviceRegistry.getChildJobs(op.getId()).size());
}
service.remove(wi2.getId());
assertEquals(0, service.getWorkflowInstances(new WorkflowQuery()).size());
}
Aggregations