use of org.opencastproject.job.api.Job in project opencast by opencast.
the class IncidentServiceEndpoint method postIncident.
@POST
@Produces(MediaType.APPLICATION_XML)
@Path("/")
@RestQuery(name = "postincident", description = "Creates a new job incident and returns it as XML", returnDescription = "Returns the created job incident as XML", restParameters = { @RestParameter(name = "job", isRequired = true, description = "The job on where to create the incident", type = Type.TEXT), @RestParameter(name = "date", isRequired = true, description = "The incident creation date", type = Type.STRING), @RestParameter(name = "code", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "severity", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "details", isRequired = false, description = "The incident details", type = Type.TEXT), @RestParameter(name = "params", isRequired = false, description = "The incident parameters", type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "New job incident has been created"), @RestResponse(responseCode = SC_BAD_REQUEST, description = "Unable to parse the one of the form params"), @RestResponse(responseCode = SC_CONFLICT, description = "No job incident related job exists") })
public Response postIncident(@FormParam("job") String jobXml, @FormParam("date") String date, @FormParam("code") String code, @FormParam("severity") String severityString, @FormParam("details") String details, @FormParam("params") LocalHashMap params) {
Job job;
Date timestamp;
Severity severity;
Map<String, String> map = new HashMap<String, String>();
List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
try {
job = JobParser.parseJob(jobXml);
timestamp = new Date(DateTimeSupport.fromUTC(date));
severity = Severity.valueOf(severityString);
if (params != null)
map = params.getMap();
if (StringUtils.isNotBlank(details)) {
final JSONArray array = (JSONArray) JSONValue.parse(details);
for (int i = 0; i < array.size(); i++) {
JSONObject tuple = (JSONObject) array.get(i);
list.add(Tuple.tuple((String) tuple.get("title"), (String) tuple.get("content")));
}
}
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
try {
Incident incident = svc.storeIncident(job, timestamp, code, severity, map, list);
String uri = UrlSupport.concat(serverUrl, serviceUrl, Long.toString(incident.getId()), ".xml");
return Response.created(new URI(uri)).entity(new JaxbIncident(incident)).build();
} catch (IllegalStateException e) {
return Response.status(Status.CONFLICT).build();
} catch (Exception e) {
logger.warn("Unable to post incident for job {}: {}", job.getId(), e);
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class OsgiIncidentServiceTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
final EntityManagerFactory emf = PersistenceUtil.newTestEntityManagerFactory(AbstractIncidentService.PERSISTENCE_UNIT_NAME);
penv = PersistenceEnvs.persistenceEnvironment(emf);
// Mock up a job
Job job = createNiceMock(Job.class);
expect(job.getProcessingHost()).andStubReturn(PROCESSING_HOST);
expect(job.getJobType()).andStubReturn(JOB_TYPE);
expect(job.getCreator()).andStubReturn("creator");
expect(job.getOrganization()).andStubReturn("organization");
replay(job);
// Mock up a service registry
final ServiceRegistry serviceRegistry = createNiceMock(ServiceRegistry.class);
expect(serviceRegistry.getJob(EasyMock.anyLong())).andAnswer(new IAnswer<Job>() {
@Override
public Job answer() throws Throwable {
final Long jobId = (Long) EasyMock.getCurrentArguments()[0];
return jobs.get(jobId);
}
}).anyTimes();
replay(serviceRegistry);
// Mock up a workflow service
final WorkflowService workflowService = createNiceMock(WorkflowService.class);
replay(workflowService);
incidentService = new AbstractIncidentService() {
@Override
protected ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
@Override
protected WorkflowService getWorkflowService() {
return workflowService;
}
@Override
protected PersistenceEnv getPenv() {
return PersistenceEnvs.persistenceEnvironment(emf);
}
};
incidents = new Incidents(serviceRegistry, incidentService);
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class ServiceRegistryJpaImpl method deleteChildJobs.
private void deleteChildJobs(EntityManager em, EntityTransaction tx, long jobId) throws ServiceRegistryException {
List<Job> childJobs = getChildJobs(jobId);
if (childJobs.isEmpty()) {
logger.debug("No child jobs of job '{}' found to delete.", jobId);
return;
}
logger.debug("Start deleting child jobs of job '{}'", jobId);
try {
for (int i = childJobs.size() - 1; i >= 0; i--) {
Job job = childJobs.get(i);
JpaJob jobToDelete = em.find(JpaJob.class, job.getId());
em.remove(jobToDelete);
logger.debug("Job '{}' deleted", job.getId());
}
logger.debug("Deleted all child jobs of job '{}'", jobId);
} catch (Exception e) {
logger.error("Unable to remove child jobs from {}: {}", jobId, e);
if (tx.isActive()) {
tx.rollback();
}
throw new ServiceRegistryException(e);
}
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class ServiceRegistryJpaImpl method getActiveJobs.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#getActiveJobs()
*/
@Override
public List<Job> getActiveJobs() throws ServiceRegistryException {
List<Status> statuses = new ArrayList<Status>();
for (Status status : Status.values()) {
if (status.isActive())
statuses.add(status);
}
EntityManager em = null;
try {
em = emf.createEntityManager();
List<JpaJob> jpaJobs = getJobsByStatus(em, statuses.toArray(new Status[statuses.size()]));
List<Job> jobs = new ArrayList<Job>(jpaJobs.size());
for (JpaJob jpaJob : jpaJobs) {
jobs.add(jpaJob.toJob());
}
return jobs;
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.job.api.Job in project opencast by opencast.
the class ServiceRegistryJpaImpl method update.
/**
* Sets the queue and runtimes and other elements of a persistent job based on a job that's been modified in memory.
* Times on both the objects must be modified, since the in-memory job must not be stale.
*
* @param fromDb
* The job from the database
* @param jpaJob
* The in-memory job
*/
private void update(JpaJob fromDb, JpaJob jpaJob) {
final Job job = jpaJob.toJob();
final Date now = new Date();
final Status status = job.getStatus();
final Status fromDbStatus = fromDb.getStatus();
fromDb.setPayload(job.getPayload());
fromDb.setStatus(job.getStatus());
fromDb.setDispatchable(job.isDispatchable());
fromDb.setVersion(job.getVersion());
fromDb.setOperation(job.getOperation());
fromDb.setArguments(job.getArguments());
fromDb.setBlockedJobIds(job.getBlockedJobIds());
fromDb.setBlockingJobId(job.getBlockingJobId());
if (job.getDateCreated() == null) {
jpaJob.setDateCreated(now);
fromDb.setDateCreated(now);
job.setDateCreated(now);
}
if (job.getProcessingHost() != null) {
ServiceRegistrationJpaImpl processingService = (ServiceRegistrationJpaImpl) getServiceRegistration(job.getJobType(), job.getProcessingHost());
fromDb.setProcessorServiceRegistration(processingService);
}
if (Status.RUNNING.equals(status) && !Status.WAITING.equals(fromDbStatus)) {
jpaJob.setDateStarted(now);
jpaJob.setQueueTime(now.getTime() - job.getDateCreated().getTime());
fromDb.setDateStarted(now);
fromDb.setQueueTime(now.getTime() - job.getDateCreated().getTime());
job.setDateStarted(now);
job.setQueueTime(now.getTime() - job.getDateCreated().getTime());
} else if (Status.FAILED.equals(status)) {
// failed jobs may not have even started properly
fromDb.setDateCompleted(now);
jpaJob.setDateCompleted(now);
job.setDateCompleted(now);
if (job.getDateStarted() != null) {
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
} else if (Status.FINISHED.equals(status)) {
if (job.getDateStarted() == null) {
// Some services (e.g. ingest) don't use job dispatching, since they start immediately and handle their own
// lifecycle. In these cases, if the start date isn't set, use the date created as the start date
jpaJob.setDateStarted(job.getDateCreated());
job.setDateStarted(job.getDateCreated());
}
jpaJob.setDateCompleted(now);
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setDateCompleted(now);
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setDateCompleted(now);
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
}
Aggregations