use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method countByOperation.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByOperation(java.lang.String, java.lang.String,
* Status)
*/
@Override
public long countByOperation(String serviceType, String operation, Status status) throws ServiceRegistryException {
EntityManager em = null;
try {
em = emf.createEntityManager();
Query query = em.createNamedQuery("Job.countByOperation");
query.setParameter("status", status.ordinal());
query.setParameter("serviceType", serviceType);
query.setParameter("operation", operation);
Number countResult = (Number) query.getSingleResult();
return countResult.longValue();
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class JobEndpoint method getTasksAsJSON.
/**
* Returns the list of tasks matching the given query as JSON Object
*
* @param query
* The worklfow query
* @return The list of matching tasks as JSON Object
* @throws JobEndpointException
* @throws NotFoundException
*/
public JObject getTasksAsJSON(WorkflowQuery query) throws JobEndpointException, NotFoundException {
// Get results
WorkflowSet workflowInstances = null;
long totalWithoutFilters = 0;
List<JValue> jsonList = new ArrayList<>();
try {
workflowInstances = workflowService.getWorkflowInstances(query);
totalWithoutFilters = workflowService.countWorkflowInstances();
} catch (WorkflowDatabaseException e) {
throw new JobEndpointException(String.format("Not able to get the list of job from the database: %s", e), e.getCause());
}
WorkflowInstance[] items = workflowInstances.getItems();
for (WorkflowInstance instance : items) {
long instanceId = instance.getId();
String series = instance.getMediaPackage().getSeriesTitle();
// Retrieve submission date with the workflow instance main job
Date created;
try {
created = serviceRegistry.getJob(instanceId).getDateCreated();
} catch (ServiceRegistryException e) {
throw new JobEndpointException(String.format("Error when retrieving job %s from the service registry: %s", instanceId, e), e.getCause());
}
jsonList.add(obj(f("id", v(instanceId)), f("title", v(nul(instance.getMediaPackage().getTitle()).getOr(""))), f("series", v(series, Jsons.BLANK)), f("workflow", v(instance.getTitle(), Jsons.BLANK)), f("status", v(instance.getState().toString())), f("submitted", v(created != null ? DateTimeSupport.toUTC(created.getTime()) : ""))));
}
JObject json = obj(f("results", arr(jsonList)), f("count", v(workflowInstances.getTotalCount())), f("offset", v(query.getStartPage())), f("limit", v(jsonList.size())), f("total", v(totalWithoutFilters)));
return json;
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class AnimateServiceImpl method process.
/**
* {@inheritDoc}
*
* @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
*/
@Override
protected String process(Job job) throws Exception {
logger.debug("Started processing job {}", job.getId());
if (!OPERATION.equals(job.getOperation())) {
throw new ServiceRegistryException(String.format("This service can't handle operations of type '%s'", job.getOperation()));
}
List<String> arguments = job.getArguments();
URI animation = new URI(arguments.get(0));
Gson gson = new Gson();
Map<String, String> metadata = gson.fromJson(arguments.get(1), stringMapType);
List<String> options = gson.fromJson(arguments.get(2), stringListType);
// filter animation and get new, custom input file
File input = customAnimation(job, animation, metadata);
// prepare output file
File output = new File(workspace.rootDirectory(), String.format("animate/%d/%s.%s", job.getId(), FilenameUtils.getBaseName(animation.getPath()), "mkv"));
FileUtils.forceMkdirParent(output);
// create animation process.
final List<String> command = new ArrayList<>();
command.add(synfigBinary);
command.add("-i");
command.add(input.getAbsolutePath());
command.add("-o");
command.add(output.getAbsolutePath());
command.addAll(options);
logger.info("Executing animation command: {}", command);
Process process = null;
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
process = processBuilder.start();
// print synfig (+ffmpeg) output
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
logger.debug("Synfig: {}", line);
}
}
// wait until the task is finished
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new AnimateServiceException(String.format("Synfig exited abnormally with status %d (command: %s)", exitCode, command));
}
if (!output.isFile()) {
throw new AnimateServiceException("Synfig produced no output");
}
logger.info("Animation generated successfully: {}", output);
} catch (Exception e) {
// Ensure temporary data are removed
FileUtils.deleteQuietly(output.getParentFile());
logger.debug("Removed output directory of failed animation process: {}", output.getParentFile());
throw new AnimateServiceException(e);
} finally {
IoSupport.closeQuietly(process);
FileUtils.deleteQuietly(input);
}
URI uri = workspace.putInCollection("animate-" + job.getId(), output.getName(), new FileInputStream(output));
FileUtils.deleteQuietly(new File(workspace.rootDirectory(), String.format("animate/%d", job.getId())));
return uri.toString();
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class AnimateServiceImpl method animate.
@Override
public Job animate(URI animation, Map<String, String> metadata, List<String> arguments) throws AnimateServiceException {
Gson gson = new Gson();
List<String> jobArguments = Arrays.asList(animation.toString(), gson.toJson(metadata), gson.toJson(arguments));
try {
logger.debug("Create animate service job");
return serviceRegistry.createJob(JOB_TYPE, OPERATION, jobArguments, jobLoad);
} catch (ServiceRegistryException e) {
throw new AnimateServiceException(e);
}
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class TextAnalyzerServiceImpl method process.
/**
* {@inheritDoc}
*
* @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
*/
@Override
protected String process(Job job) throws Exception {
Operation op = null;
String operation = job.getOperation();
List<String> arguments = job.getArguments();
try {
op = Operation.valueOf(operation);
switch(op) {
case Extract:
Attachment element = (Attachment) MediaPackageElementParser.getFromXml(arguments.get(0));
Catalog catalog = extract(job, element);
return MediaPackageElementParser.getAsXml(catalog);
default:
throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
}
} catch (IllegalArgumentException e) {
throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
} catch (IndexOutOfBoundsException e) {
throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
} catch (Exception e) {
throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
}
}
Aggregations