use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ConfigurableRetractWorkflowOperationHandler method start.
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
notNull(workflowInstance, "workflowInstance");
final MediaPackage mp = workflowInstance.getMediaPackage();
final String channelId = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(CHANNEL_ID_KEY));
if (StringUtils.isBlank((channelId))) {
throw new WorkflowOperationException("Unable to publish this mediapackage as the configuration key " + CHANNEL_ID_KEY + " is missing. Unable to determine where to publish these elements.");
}
retract(mp, channelId);
return createResult(mp, Action.CONTINUE);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class RetractYouTubeWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(WorkflowInstance, JobContext)
*/
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
try {
logger.info("Retracting media package {} from youtube publication channel", mediaPackage);
// Wait for youtube retraction to finish
Job retractJob = publicationService.retract(mediaPackage);
if (!waitForStatus(retractJob).isSuccess())
throw new WorkflowOperationException("The youtube retract job did not complete successfully");
logger.debug("Retraction from youtube operation complete");
// Remove the retracted elements from the mediapackage
Job job = serviceRegistry.getJob(retractJob.getId());
if (job.getPayload() != null) {
logger.info("Removing youtube publication element from media package {}", mediaPackage);
Publication retractedElement = (Publication) MediaPackageElementParser.getFromXml(job.getPayload());
mediaPackage.remove(retractedElement);
logger.debug("Remove youtube publication element '{}' complete", retractedElement);
} else {
logger.info("No youtube publication found to retract in mediapackage {}!", mediaPackage);
return createResult(mediaPackage, Action.CONTINUE);
}
return createResult(mediaPackage, Action.CONTINUE);
} catch (Throwable t) {
throw new WorkflowOperationException(t);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class HttpNotificationWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Running HTTP notification workflow operation on workflow {}", workflowInstance.getId());
int maxRetry = DEFAULT_MAX_RETRY;
int timeout = DEFAULT_TIMEOUT;
// Required configuration
String urlPath = getConfig(workflowInstance, OPT_URL_PATH);
// Optional configuration
String notificationSubject = getConfig(workflowInstance, OPT_NOTIFICATION_SUBJECT, null);
String notificationMessage = getConfig(workflowInstance, OPT_NOTIFICATION_MESSAGE, null);
String method = getConfig(workflowInstance, OPT_METHOD, "post");
String maxRetryOpt = getConfig(workflowInstance, OPT_MAX_RETRY, null);
String timeoutOpt = getConfig(workflowInstance, OPT_TIMEOUT, null);
// If set, convert the timeout to milliseconds
if (timeoutOpt != null) {
timeout = Integer.parseInt(timeoutOpt) * 1000;
}
// Is there a need to retry on failure?
if (maxRetryOpt != null) {
maxRetry = Integer.parseInt(maxRetryOpt);
}
// Figure out which request method to use
HttpEntityEnclosingRequestBase request = null;
if (StringUtils.equalsIgnoreCase("post", method)) {
request = new HttpPost(urlPath);
} else if (StringUtils.equalsIgnoreCase("put", method)) {
request = new HttpPut(urlPath);
} else {
throw new WorkflowOperationException("The configuration key '" + OPT_METHOD + "' only supports 'post' and 'put'");
}
logger.debug("Request will be sent using the '{}' method", method);
// Add event parameters as form parameters
try {
List<BasicNameValuePair> params = new ArrayList<>();
// Add the subject (if specified)
if (notificationSubject != null) {
params.add(new BasicNameValuePair(HTTP_PARAM_SUBJECT, notificationSubject));
}
// Add the message (if specified)
if (notificationMessage != null) {
params.add(new BasicNameValuePair(HTTP_PARAM_MESSAGE, notificationMessage));
}
// Add the workflow instance id
params.add(new BasicNameValuePair(HTTP_PARAM_WORKFLOW, Long.toString(workflowInstance.getId())));
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new WorkflowOperationException("Error happened during the encoding of the event parameter as form parameter:", e);
}
// Execute the request
if (!executeRequest(request, maxRetry, timeout, INITIAL_SLEEP_TIME)) {
throw new WorkflowOperationException(format("Notification could not be delivered to %s", urlPath));
}
return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class HttpNotificationWorkflowOperationHandlerTest method testNotificationFailedAfterOneTry.
@Test
public void testNotificationFailedAfterOneTry() throws Exception {
client = EasyMock.createNiceMock(TrustedHttpClient.class);
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_FOUND, ""));
EasyMock.expect(client.execute((HttpPut) EasyMock.anyObject(), EasyMock.anyInt(), EasyMock.anyInt())).andReturn(response);
EasyMock.replay(client);
// set up service
operationHandler = new HttpNotificationWorkflowOperationHandler();
// operation configuration
Map<String, String> configurations = new HashMap<String, String>();
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_URL_PATH, "http://127.0.0.1:9");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_MAX_RETRY, "0");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_TIMEOUT, Integer.toString(10));
// run the operation handler
try {
getWorkflowOperationResult(mp, configurations);
Assert.fail("Operation handler should have thrown an exception!");
} catch (WorkflowOperationException e) {
Assert.assertTrue("Exception thrown as expected by the operation handler", true);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class AnalyzeAudioWorkflowOperationHandler method analyze.
private WorkflowOperationResult analyze(MediaPackage src, WorkflowOperationInstance operation) throws SoxException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException, EncoderException {
MediaPackage mediaPackage = (MediaPackage) src.clone();
// Check which tags have been configured
String sourceTagsOption = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
String sourceFlavorsOption = StringUtils.trimToNull(operation.getConfiguration("source-flavors"));
boolean forceTranscode = BooleanUtils.toBoolean(operation.getConfiguration("force-transcode"));
AbstractMediaPackageElementSelector<Track> elementSelector = new TrackSelector();
// Make sure either one of tags or flavors are provided
if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorOption) && StringUtils.isBlank(sourceFlavorsOption)) {
logger.info("No source tags or flavors have been specified, not matching anything");
return createResult(mediaPackage, Action.CONTINUE);
}
// Select the source flavors
for (String flavor : asList(sourceFlavorsOption)) {
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
// Support legacy "source-flavor" option
if (StringUtils.isNotBlank(sourceFlavorOption)) {
String flavor = StringUtils.trim(sourceFlavorOption);
try {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
}
}
// Select the source tags
for (String tag : asList(sourceTagsOption)) {
elementSelector.addTag(tag);
}
// Look for elements matching the tag
Collection<Track> elements = elementSelector.select(mediaPackage, false);
// Analyze audio for all tracks found
long totalTimeInQueue = 0;
List<URI> cleanupURIs = new ArrayList<URI>();
Map<Job, Track> analyzeJobs = new HashMap<Job, Track>();
try {
for (Track track : elements) {
TrackImpl audioTrack = (TrackImpl) track;
// Skip video only mismatches
if (!track.hasAudio()) {
logger.info("Skipping audio analysis of '{}', since it contains no audio stream", track);
continue;
} else if (track.hasVideo() || forceTranscode) {
audioTrack = (TrackImpl) extractAudioTrack(track);
audioTrack.setAudio(((TrackImpl) track).getAudio());
cleanupURIs.add(audioTrack.getURI());
}
analyzeJobs.put(soxService.analyze(audioTrack), track);
}
if (analyzeJobs.isEmpty()) {
logger.info("No matching tracks found");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait for the jobs to return
if (!waitForStatus(analyzeJobs.keySet().toArray(new Job[analyzeJobs.size()])).isSuccess())
throw new WorkflowOperationException("One of the analyze jobs did not complete successfully");
// Process the result
for (Map.Entry<Job, Track> entry : analyzeJobs.entrySet()) {
Job job = entry.getKey();
TrackImpl origTrack = (TrackImpl) entry.getValue();
// add this receipt's queue time to the total
totalTimeInQueue += job.getQueueTime();
if (job.getPayload().length() > 0) {
TrackImpl analyzed = (TrackImpl) MediaPackageElementParser.getFromXml(job.getPayload());
// Set metadata on track
origTrack.setAudio(analyzed.getAudio());
} else {
logger.warn("Analyze audio job {} for track {} has no result!", job, origTrack);
}
}
} finally {
// Clean up temporary audio files from workspace
for (URI uri : cleanupURIs) {
workspace.delete(uri);
}
}
WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
logger.debug("Analyze audio operation completed");
return result;
}
Aggregations