use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class WorkflowOperationWorker method resume.
/**
* Resumes a previously suspended workflow operation. Note that only workflow operation handlers that implement
* {@link ResumableWorkflowOperationHandler} can be resumed.
*
* @return the workflow operation result
* @throws WorkflowOperationException
* if executing the workflow operation handler fails
* @throws WorkflowException
* if there is a problem processing the workflow
* @throws IllegalStateException
* if the workflow operation cannot be resumed
*/
public WorkflowOperationResult resume() throws WorkflowOperationException, WorkflowException, IllegalStateException, UnauthorizedException {
WorkflowOperationInstance operation = workflow.getCurrentOperation();
// Make sure we have a (suitable) handler
if (handler == null) {
// If there is no handler for the operation, yet we are supposed to run it, we must fail
logger.warn("No handler available to resume operation '{}'", operation.getTemplate());
throw new IllegalStateException("Unable to find a workflow handler for '" + operation.getTemplate() + "'");
} else if (!(handler instanceof ResumableWorkflowOperationHandler)) {
throw new IllegalStateException("An attempt was made to resume a non-resumable operation");
}
ResumableWorkflowOperationHandler resumableHandler = (ResumableWorkflowOperationHandler) handler;
operation.setState(OperationState.RUNNING);
service.update(workflow);
try {
WorkflowOperationResult result = resumableHandler.resume(workflow, null, properties);
return result;
} catch (Exception e) {
operation.setState(OperationState.FAILED);
if (e instanceof WorkflowOperationException)
throw (WorkflowOperationException) e;
throw new WorkflowOperationException(e);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class ExecuteOnceWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
logger.debug("Running execute workflow operation with ID {}", operation.getId());
// Get operation parameters
String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
float load = 1.0f;
String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
if (StringUtils.isNotBlank(loadPropertyStr)) {
try {
load = Float.parseFloat(loadPropertyStr);
} catch (NumberFormatException e) {
String description = StringUtils.trimToEmpty(operation.getDescription());
logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
}
}
String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));
// Unmarshall target flavor
MediaPackageElementFlavor targetFlavor = null;
if (targetFlavorStr != null)
targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
// Unmarshall expected mediapackage element type
MediaPackageElement.Type expectedType = null;
if (expectedTypeStr != null) {
for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
expectedType = type;
break;
}
if (expectedType == null)
throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
}
// Process the result element
MediaPackageElement resultElement = null;
try {
Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);
WorkflowOperationResult result = null;
// Wait for all jobs to be finished
if (!waitForStatus(job).isSuccess())
throw new WorkflowOperationException("Execute operation failed");
if (StringUtils.isNotBlank(job.getPayload())) {
if (setWfProps) {
// The job payload is a file with set of properties for the workflow
resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
final Properties properties = new Properties();
File propertiesFile = workspace.get(resultElement.getURI());
try (InputStream is = new FileInputStream(propertiesFile)) {
properties.load(is);
}
logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());
Map<String, String> wfProps = new HashMap<String, String>((Map) properties);
result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
} else {
// The job payload is a new element for the MediaPackage
resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
// Have the track inspected and return the result
Job inspectionJob = null;
inspectionJob = inspectionService.inspect(resultElement.getURI());
JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
if (!barrier.waitForJobs().isSuccess()) {
throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
}
resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
}
// Store new element to mediaPackage
mediaPackage.add(resultElement);
URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename);
resultElement.setURI(uri);
// Set new flavor
if (targetFlavor != null)
resultElement.setFlavor(targetFlavor);
// Set new tags
if (targetTags != null) {
// Assume the tags starting with "-" means we want to eliminate such tags form the result element
for (String tag : asList(targetTags)) {
if (tag.startsWith("-"))
// We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
resultElement.removeTag(tag.replaceAll("^-+", ""));
else
resultElement.addTag(tag);
}
}
result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
}
} else {
// Payload is empty
result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
}
logger.debug("Execute operation {} completed", operation.getId());
return result;
} catch (ExecuteException e) {
throw new WorkflowOperationException(e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Some result element couldn't be serialized", e);
} catch (NotFoundException e) {
throw new WorkflowOperationException("Could not find mediapackage", e);
} catch (IOException e) {
throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
} catch (MediaInspectionException e) {
throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class HttpNotificationWorkflowOperationHandlerTest method testSuccessfulNotification.
@Test
@Ignore
public // todo test does not pass
void testSuccessfulNotification() throws Exception {
// operation configuration
Map<String, String> configurations = new HashMap<String, String>();
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_URL_PATH, "http://www.host-does-not-exist.com");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test message");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_MAX_RETRY, "2");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_TIMEOUT, Integer.toString(10 * 1000));
// run the operation handler
WorkflowOperationResult result = getWorkflowOperationResult(mp, configurations);
Assert.assertEquals(result.getAction(), Action.CONTINUE);
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class EmailWorkflowOperationHandlerTest method testTemplateInBody.
@Test
public void testTemplateInBody() throws Exception {
workflowInstance.setTitle("testTemplateInBody");
operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO);
operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC);
operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC);
operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT);
operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}");
WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals(DEFAULT_TO, capturedTo.getValue());
Assert.assertEquals(DEFAULT_CC, capturedCC.getValue());
Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue());
Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue());
Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue());
}
use of org.opencastproject.workflow.api.WorkflowOperationResult in project opencast by opencast.
the class EmailWorkflowOperationHandlerTest method testTemplateInFile.
@Test
public void testTemplateInFile() throws Exception {
workflowInstance.setTitle("testTemplateInFile");
operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO);
operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC);
operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC);
operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT);
operation.setConfiguration(EmailWorkflowOperationHandler.BODY_TEMPLATE_FILE_PROPERTY, "template1");
WorkflowOperationResult result = operationHandler.start(workflowInstance, null);
Assert.assertEquals(Action.CONTINUE, result.getAction());
Assert.assertEquals(DEFAULT_TO, capturedTo.getValue());
Assert.assertEquals(DEFAULT_CC, capturedCC.getValue());
Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue());
Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue());
Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue());
}
Aggregations