use of org.opencastproject.execute.api.ExecuteException 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);
}
}
Aggregations