use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ZipWorkflowOperationHandlerTest method testInvalidWorkflow.
/*
* MH-9757
*/
@Test
public void testInvalidWorkflow() throws Exception {
WorkflowInstanceImpl instance = new WorkflowInstanceImpl();
List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>();
WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED);
ops.add(operation);
instance.setOperations(ops);
operation.setConfiguration(ZipWorkflowOperationHandler.ZIP_COLLECTION_PROPERTY, "failed-zips");
operation.setConfiguration(ZipWorkflowOperationHandler.INCLUDE_FLAVORS_PROPERTY, "*/source,dublincore/*");
operation.setConfiguration(ZipWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "archive/zip");
operation.setConfiguration(ZipWorkflowOperationHandler.COMPRESS_PROPERTY, "false");
try {
WorkflowOperationResult result = operationHandler.start(null, null);
Assert.fail("A null workflow is passed so an exception should be thrown");
} catch (WorkflowOperationException e) {
// expecting exception
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class IncidentCreatorWorkflowOperationHandler method start.
@Override
public WorkflowOperationResult start(WorkflowInstance wi, JobContext ctx) throws WorkflowOperationException {
final WorkflowOperationInstance woi = wi.getCurrentOperation();
final int code = option(woi.getConfiguration(OPT_CODE)).bind(Strings.toInt).getOrElse(1);
final Severity severity = option(woi.getConfiguration(OPT_SEVERITY)).bind(parseEnum(Severity.FAILURE)).getOrElse(Severity.INFO);
final List<Tuple<String, String>> details = option(woi.getConfiguration(OPT_DETAILS)).mlist().bind(splitBy(";")).map(splitBy("=")).filter(Tuples.<String>listHasSize(2)).map(Tuples.<String>fromList()).value();
final Map<String, String> params = Immutables.map(option(woi.getConfiguration(OPT_PARAMS)).mlist().bind(splitBy(";")).map(splitBy("=")).filter(Tuples.<String>listHasSize(2)).map(Tuples.<String>fromList()).value());
log.info("Create nop job");
final Job job = nopService.nop();
log.info("Log a dummy incident with code %d", code);
serviceRegistry.incident().record(job, severity, code, params, details);
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException("Job did not complete successfully");
} else {
return createResult(WorkflowOperationResult.Action.CONTINUE);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ImageToVideoWorkflowOperationHandler method imageToVideo.
private WorkflowOperationResult imageToVideo(MediaPackage mp, WorkflowInstance wi) throws Exception {
// read cfg
final List<String> sourceTags = getCfg(wi, OPT_SOURCE_TAGS).map(asList).getOrElse(nil(String.class));
final Option<MediaPackageElementFlavor> sourceFlavor = getCfg(wi, OPT_SOURCE_FLAVOR).map(MediaPackageElementFlavor.parseFlavor);
if (sourceFlavor.isNone() && sourceTags.isEmpty()) {
logger.warn("No source tags or flavor are given to determine the image to use");
return createResult(mp, Action.SKIP);
}
final List<String> targetTags = getCfg(wi, OPT_TARGET_TAGS).map(asList).getOrElse(nil(String.class));
final Option<MediaPackageElementFlavor> targetFlavor = getCfg(wi, OPT_TARGET_FLAVOR).map(MediaPackageElementFlavor.parseFlavor);
final double duration = getCfg(wi, OPT_DURATION).bind(Strings.toDouble).getOrElse(this.<Double>cfgKeyMissing(OPT_DURATION));
final String profile = getCfg(wi, OPT_PROFILE).getOrElse(this.<String>cfgKeyMissing(OPT_PROFILE));
// run image to video jobs
final List<Job> jobs = Monadics.<MediaPackageElement>mlist(mp.getAttachments()).filter(sourceFlavor.map(Filters.matchesFlavor).getOrElse(Booleans.<MediaPackageElement>yes())).filter(Filters.hasTagAny(sourceTags)).map(Misc.<MediaPackageElement, Attachment>cast()).map(imageToVideo(profile, duration)).value();
if (JobUtil.waitForJobs(serviceRegistry, jobs).isSuccess()) {
for (final Job job : jobs) {
if (job.getPayload().length() > 0) {
Track track = (Track) MediaPackageElementParser.getFromXml(job.getPayload());
track.setURI(workspace.moveTo(track.getURI(), mp.getIdentifier().toString(), track.getIdentifier(), FilenameUtils.getName(track.getURI().toString())));
// Adjust the target tags
for (String tag : targetTags) {
track.addTag(tag);
}
// Adjust the target flavor.
for (MediaPackageElementFlavor flavor : targetFlavor) {
track.setFlavor(flavor);
}
// store new tracks to mediaPackage
mp.add(track);
logger.debug("Image to video operation completed");
} else {
logger.info("Image to video operation unsuccessful, no payload returned: {}", job);
return createResult(mp, Action.SKIP);
}
}
return createResult(mp, Action.CONTINUE, mlist(jobs).foldl(0L, new Function2<Long, Job, Long>() {
@Override
public Long apply(Long max, Job job) {
return Math.max(max, job.getQueueTime());
}
}));
} else {
throw new WorkflowOperationException("The image to video encoding jobs did not return successfully");
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class EncodeWorkflowOperationHandler method encode.
/**
* Encode tracks from MediaPackage using profiles stored in properties and updates current MediaPackage.
*
* @param src
* The source media package
* @param operation
* the current workflow operation
* @return the operation result containing the updated media package
* @throws EncoderException
* if encoding fails
* @throws WorkflowOperationException
* if errors occur during processing
* @throws IOException
* if the workspace operations fail
* @throws NotFoundException
* if the workspace doesn't contain the requested file
*/
private WorkflowOperationResult encode(MediaPackage src, WorkflowOperationInstance operation) throws EncoderException, IOException, NotFoundException, MediaPackageException, WorkflowOperationException {
MediaPackage mediaPackage = (MediaPackage) src.clone();
// Check which tags have been configured
String sourceTagsOption = StringUtils.trimToNull(operation.getConfiguration("source-tags"));
String targetTagsOption = StringUtils.trimToNull(operation.getConfiguration("target-tags"));
String sourceFlavorOption = StringUtils.trimToNull(operation.getConfiguration("source-flavor"));
String sourceFlavorsOption = StringUtils.trimToNull(operation.getConfiguration("source-flavors"));
String targetFlavorOption = StringUtils.trimToNull(operation.getConfiguration("target-flavor"));
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);
}
// Find the encoding profile
String profilesOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profiles"));
List<EncodingProfile> profiles = new ArrayList<EncodingProfile>();
for (String profileName : asList(profilesOption)) {
EncodingProfile profile = composerService.getProfile(profileName);
if (profile == null)
throw new WorkflowOperationException("Encoding profile '" + profileName + "' was not found");
profiles.add(profile);
}
// Support legacy "encoding-profile" option
String profileOption = StringUtils.trimToNull(operation.getConfiguration("encoding-profile"));
if (StringUtils.isNotBlank(profileOption)) {
String profileId = StringUtils.trim(profileOption);
EncodingProfile profile = composerService.getProfile(profileId);
if (profile == null)
throw new WorkflowOperationException("Encoding profile '" + profileId + "' was not found");
profiles.add(profile);
}
// Make sure there is at least one profile
if (profiles.isEmpty())
throw new WorkflowOperationException("No encoding profile was specified");
// Target tags
List<String> targetTags = asList(targetTagsOption);
// Target flavor
MediaPackageElementFlavor targetFlavor = null;
if (StringUtils.isNotBlank(targetFlavorOption)) {
try {
targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException("Target flavor '" + targetFlavorOption + "' is malformed");
}
}
// Look for elements matching the tag
Collection<Track> elements = elementSelector.select(mediaPackage, false);
// Encode all tracks found
long totalTimeInQueue = 0;
Map<Job, JobInformation> encodingJobs = new HashMap<Job, JobInformation>();
for (Track track : elements) {
// Encode the track with all profiles
for (EncodingProfile profile : profiles) {
// Check if the track supports the output type of the profile
MediaType outputType = profile.getOutputType();
if (outputType.equals(MediaType.Audio) && !track.hasAudio()) {
logger.info("Skipping encoding of '{}', since it lacks an audio stream", track);
continue;
} else if (outputType.equals(MediaType.Visual) && !track.hasVideo()) {
logger.info("Skipping encoding of '{}', since it lacks a video stream", track);
continue;
}
logger.info("Encoding track {} using encoding profile '{}'", track, profile);
// Start encoding and wait for the result
encodingJobs.put(composerService.parallelEncode(track, profile.getIdentifier()), new JobInformation(track, profile));
}
}
if (encodingJobs.isEmpty()) {
logger.info("No matching tracks found");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait for the jobs to return
if (!waitForStatus(encodingJobs.keySet().toArray(new Job[encodingJobs.size()])).isSuccess()) {
throw new WorkflowOperationException("One of the encoding jobs did not complete successfully");
}
// Process the result
for (Map.Entry<Job, JobInformation> entry : encodingJobs.entrySet()) {
Job job = entry.getKey();
Track track = entry.getValue().getTrack();
// add this receipt's queue time to the total
totalTimeInQueue += job.getQueueTime();
// it is allowed for compose jobs to return an empty payload. See the EncodeEngine interface
if (job.getPayload().length() > 0) {
List<Track> composedTracks = (List<Track>) MediaPackageElementParser.getArrayFromXml(job.getPayload());
// Adjust the target tags
for (Track encodedTrack : composedTracks) {
for (String tag : targetTags) {
logger.trace("Tagging composed track {} with '{}'", encodedTrack.toString(), tag);
encodedTrack.addTag(tag);
}
}
// Adjust the target flavor. Make sure to account for partial updates
if (targetFlavor != null) {
String flavorType = targetFlavor.getType();
String flavorSubtype = targetFlavor.getSubtype();
if ("*".equals(flavorType))
flavorType = track.getFlavor().getType();
if ("*".equals(flavorSubtype))
flavorSubtype = track.getFlavor().getSubtype();
for (Track encodedTrack : composedTracks) {
encodedTrack.setFlavor(new MediaPackageElementFlavor(flavorType, flavorSubtype));
logger.debug("Composed track {} has flavor '{}'", encodedTrack.toString(), encodedTrack.getFlavor());
}
}
// store new tracks to mediaPackage
for (Track encodedTrack : composedTracks) {
mediaPackage.addDerived(encodedTrack, track);
String fileName = getFileNameFromElements(track, encodedTrack);
encodedTrack.setURI(workspace.moveTo(encodedTrack.getURI(), mediaPackage.getIdentifier().toString(), encodedTrack.getIdentifier(), fileName));
}
}
}
WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
logger.debug("Parallel encode operation completed");
return result;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ImageWorkflowOperationHandler method validateTargetBaseNameFormat.
static Fx<String> validateTargetBaseNameFormat(final String formatName) {
return new Fx<String>() {
@Override
public void apply(String format) {
boolean valid;
try {
final String name = formatFileName(format, 15.11, ".png");
valid = name.contains(".") && name.contains(".png");
} catch (IllegalFormatException e) {
valid = false;
}
if (!valid) {
chuck(new WorkflowOperationException(format("%s is not a valid format string for config option %s", format, formatName)));
}
}
};
}
Aggregations