use of org.opencastproject.job.api.Job in project opencast by opencast.
the class InspectWorkflowOperationHandler 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 = (MediaPackage) workflowInstance.getMediaPackage().clone();
// Inspect the tracks
long totalTimeInQueue = 0;
WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
boolean rewrite = "true".equalsIgnoreCase(operation.getConfiguration(OPT_OVERWRITE));
boolean acceptNoMedia = "true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCEPT_NO_MEDIA));
final Map<String, String> options = new HashMap<String, String>();
if ("true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCURATE_FRAME_COUNT))) {
logger.info("Using accurate frame count for inspection media package {}", mediaPackage);
options.put(MediaInspectionOptions.OPTION_ACCURATE_FRAME_COUNT, Boolean.TRUE.toString());
}
// Test if there are tracks in the mediapackage
if (mediaPackage.getTracks().length == 0) {
logger.warn("Recording {} contains no media", mediaPackage);
if (!acceptNoMedia)
throw new WorkflowOperationException("Mediapackage " + mediaPackage + " contains no media");
}
for (Track track : mediaPackage.getTracks()) {
logger.info("Inspecting track '{}' of {}", track.getIdentifier(), mediaPackage);
Job inspectJob = null;
Track inspectedTrack;
if (track != null && track.getURI() != null && (track.getURI().toString().endsWith(".vtt") || track.getURI().toString().endsWith(".srt"))) {
inspectedTrack = (Track) track.clone();
inspectedTrack.setMimeType(MimeType.mimeType("text", "vtt"));
logger.info("Track '{}' of {} contains captions", track.getIdentifier(), mediaPackage);
} else {
try {
inspectJob = inspectionService.enrich(track, rewrite, options);
if (!waitForStatus(inspectJob).isSuccess()) {
throw new WorkflowOperationException("Track " + track + " could not be inspected");
}
} catch (MediaInspectionException e) {
throw new WorkflowOperationException("Error inspecting media package", e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Error parsing media package", e);
}
// add this receipt's queue and execution times to the total
long timeInQueue = inspectJob.getQueueTime() == null ? 0 : inspectJob.getQueueTime();
totalTimeInQueue += timeInQueue;
try {
inspectedTrack = (Track) MediaPackageElementParser.getFromXml(inspectJob.getPayload());
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Unable to parse track from job " + inspectJob.getId(), e);
}
if (inspectedTrack == null)
throw new WorkflowOperationException("Track " + track + " could not be inspected");
if (inspectedTrack.getStreams().length == 0)
throw new WorkflowOperationException(format("Track %s does not contain any streams", track));
}
// Replace the original track with the inspected one
try {
mediaPackage.remove(track);
mediaPackage.add(inspectedTrack);
} catch (UnsupportedElementException e) {
logger.error("Error adding {} to media package", inspectedTrack, e);
}
}
// Update dublin core with metadata
try {
updateDublinCore(mediaPackage);
} catch (Exception e) {
logger.warn("Unable to update dublin core data: {}", e.getMessage(), e);
throw new WorkflowOperationException(e.getMessage());
}
return createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandlerTest method mockDependencies.
private void mockDependencies(int numberOfCopies) throws Exception {
clonedMediaPackages = Capture.newInstance(CaptureType.ALL);
reset(workspace, assetManager, distributionService);
URI uriDc = getClass().getResource("/dublincore.xml").toURI();
for (int i = 0; i < numberOfCopies; i++) {
expect(workspace.read(eq(URI.create("dublincore.xml")))).andReturn(new FileInputStream(new File(uriDc))).times(1);
}
expect(workspace.get(anyObject())).andReturn(new File(getClass().getResource("/av.mov").toURI())).anyTimes();
expect(workspace.put(anyString(), anyString(), eq("dublincore.xml"), anyObject())).andReturn(uriDc).times(numberOfCopies);
replay(workspace);
final AResult qResult = createNiceMock(AResult.class);
expect(qResult.getRecords()).andReturn(Stream.empty()).anyTimes();
replay(qResult);
final ASelectQuery qSelect = createNiceMock(ASelectQuery.class);
expect(qSelect.where(anyObject())).andReturn(qSelect).anyTimes();
expect(qSelect.run()).andReturn(qResult).anyTimes();
replay(qSelect);
final AQueryBuilder qBuilder = createNiceMock(AQueryBuilder.class);
expect(qBuilder.select(anyObject())).andReturn(qSelect).anyTimes();
replay(qBuilder);
expect(assetManager.createQuery()).andReturn(qBuilder).anyTimes();
expect(assetManager.takeSnapshot(eq(AssetManager.DEFAULT_OWNER), capture(clonedMediaPackages))).andReturn(createNiceMock(Snapshot.class)).times(numberOfCopies);
replay(assetManager);
final Job distributionJob = createNiceMock(Job.class);
final Publication internalPub = (Publication) mp.getElementById("pub-int");
final List<MediaPackageElement> internalPubElements = new ArrayList<>();
Collections.addAll(internalPubElements, (internalPub.getAttachments()));
Collections.addAll(internalPubElements, (internalPub.getCatalogs()));
Collections.addAll(internalPubElements, (internalPub.getTracks()));
expect(distributionJob.getStatus()).andReturn(Job.Status.FINISHED).anyTimes();
for (MediaPackageElement e : internalPubElements) {
expect(distributionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(e)).times(numberOfCopies);
}
replay(distributionJob);
expect(distributionService.distribute(eq(InternalPublicationChannel.CHANNEL_ID), anyObject(), anyString())).andReturn(distributionJob).anyTimes();
replay(distributionService);
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class AbstractJobProducerEndpoint method dispatchJob.
/**
* @see org.opencastproject.job.api.JobProducer#acceptJob(org.opencastproject.job.api.Job)
*/
@POST
@Path("/dispatch")
public Response dispatchJob(@FormParam("id") long jobId, @FormParam("operation") String jobOperation) throws ServiceRegistryException {
final JobProducer service = getService();
if (service == null)
throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);
// See if the service is ready to accept anything
if (!service.isReadyToAcceptJobs(jobOperation)) {
logger.debug("Service {} is not ready to accept jobs with operation {}", service, jobOperation);
return Response.status(Status.SERVICE_UNAVAILABLE).build();
}
Job job;
try {
job = getServiceRegistry().getJob(jobId);
} catch (NotFoundException e) {
logger.warn("Unable to find dispatched job {}", jobId);
return Response.status(Status.NOT_FOUND).build();
}
// See if the service has strong feelings about this particular job
try {
if (!service.isReadyToAccept(job)) {
logger.debug("Service {} temporarily refused to accept job {}", service, jobId);
return Response.status(Status.SERVICE_UNAVAILABLE).build();
}
} catch (UndispatchableJobException e) {
logger.warn("Service {} permanently refused to accept job {}", service, jobId);
return Response.status(Status.PRECONDITION_FAILED).build();
}
service.acceptJob(job);
return Response.noContent().build();
}
use of org.opencastproject.job.api.Job 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.job.api.Job in project opencast by opencast.
the class AbstractIncidentService method getIncidentsOfJob.
@Override
public IncidentTree getIncidentsOfJob(long jobId, boolean cascade) throws NotFoundException, IncidentServiceException {
List<Incident> incidents = getIncidentsOfJob(jobId);
List<IncidentTree> childIncidents = new ArrayList<IncidentTree>();
try {
Job job = getServiceRegistry().getJob(jobId);
if (cascade && !"START_WORKFLOW".equals(job.getOperation())) {
childIncidents = getChildIncidents(jobId);
} else if (cascade && "START_WORKFLOW".equals(job.getOperation())) {
for (WorkflowOperationInstance operation : getWorkflowService().getWorkflowById(jobId).getOperations()) {
if (operation.getState().equals(OperationState.INSTANTIATED))
continue;
IncidentTree operationResult = getIncidentsOfJob(operation.getId(), true);
if (hasIncidents(Collections.list(operationResult)))
childIncidents.add(operationResult);
}
}
return new IncidentTreeImpl(incidents, childIncidents);
} catch (NotFoundException ignore) {
// Workflow deleted
return new IncidentTreeImpl(incidents, childIncidents);
} catch (Exception e) {
logger.error("Error loading child jobs of {}: {}", jobId);
throw new IncidentServiceException(e);
}
}
Aggregations