use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryInMemoryImpl method deactivate.
/**
* Shuts down this service registry, logging all jobs and their statuses.
*/
public void deactivate() {
dispatcher.shutdownNow();
Map<Status, AtomicInteger> counts = new HashMap<Job.Status, AtomicInteger>();
synchronized (jobs) {
for (String serializedJob : jobs.values()) {
Job job = null;
try {
job = JobParser.parseJob(serializedJob);
} catch (IOException e) {
throw new IllegalStateException("Error unmarshaling job", e);
}
if (counts.containsKey(job.getStatus())) {
counts.get(job.getStatus()).incrementAndGet();
} else {
counts.put(job.getStatus(), new AtomicInteger(1));
}
}
}
StringBuilder sb = new StringBuilder("Abandoned:");
for (Entry<Status, AtomicInteger> entry : counts.entrySet()) {
sb.append(" " + entry.getValue() + " " + entry.getKey() + " jobs");
}
logger.info(sb.toString());
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryJpaImpl method getDispatchableJobsWithStatus.
/**
* Gets jobs of all types that are in the given state.
*
* @param em the entity manager
* @param offset apply offset to the db query if offset > 0
* @param limit apply limit to the db query if limit > 0
* @param statuses the job status should be one from the given statuses
* @return the list of jobs waiting for dispatch
* @throws ServiceRegistryException if there is a problem communicating with the jobs database
*/
protected List<JpaJob> getDispatchableJobsWithStatus(EntityManager em, int offset, int limit, Status... statuses) throws ServiceRegistryException {
if (statuses == null)
return Collections.EMPTY_LIST;
List<Integer> statusesOrdinal = new ArrayList<>(statuses.length);
for (Status status : statuses) {
statusesOrdinal.add(status.ordinal());
}
TypedQuery<JpaJob> query = null;
try {
query = em.createNamedQuery("Job.dispatchable.status", JpaJob.class);
query.setParameter("statuses", statusesOrdinal);
if (offset > 0)
query.setFirstResult(offset);
if (limit > 0)
query.setMaxResults(limit);
return query.getResultList();
} catch (Exception e) {
throw new ServiceRegistryException(e);
}
}
use of org.opencastproject.job.api.Job.Status in project opencast by opencast.
the class ServiceRegistryJpaImpl method getServiceStatistics.
/**
* Gets performance and runtime statistics for each known service registration.
* For the statistics, only jobs created within the time interval [startDate, endDate] are being considered
*
* @param startDate
* Only jobs created after this data are considered for statistics
* @param endDate
* Only jobs created before this data are considered for statistics
* @return the service statistics
* @throws ServiceRegistryException
* if there is a problem accessing the service registry
*/
private List<ServiceStatistics> getServiceStatistics(Date startDate, Date endDate) throws ServiceRegistryException {
EntityManager em = null;
try {
em = emf.createEntityManager();
Map<Long, JaxbServiceStatistics> statsMap = new HashMap<Long, JaxbServiceStatistics>();
// Make sure we also include the services that have no processing history so far
List<ServiceRegistrationJpaImpl> services = em.createNamedQuery("ServiceRegistration.getAll").getResultList();
for (ServiceRegistrationJpaImpl s : services) {
statsMap.put(s.getId(), new JaxbServiceStatistics(s));
}
Query query = em.createNamedQuery("ServiceRegistration.statistics");
query.setParameter("minDateCreated", startDate, TemporalType.TIMESTAMP);
query.setParameter("maxDateCreated", endDate, TemporalType.TIMESTAMP);
List queryResults = query.getResultList();
for (Object result : queryResults) {
Object[] oa = (Object[]) result;
Number serviceRegistrationId = ((Number) oa[0]);
if (serviceRegistrationId == null || serviceRegistrationId.longValue() == 0)
continue;
Status status = Status.values()[((Number) oa[1]).intValue()];
Number count = (Number) oa[2];
Number meanQueueTime = (Number) oa[3];
Number meanRunTime = (Number) oa[4];
// The statistics query returns a cartesian product, so we need to iterate over them to build up the objects
JaxbServiceStatistics stats = statsMap.get(serviceRegistrationId.longValue());
if (stats == null)
continue;
// the status will be null if there are no jobs at all associated with this service registration
if (status != null) {
switch(status) {
case RUNNING:
stats.setRunningJobs(count.intValue());
break;
case QUEUED:
case DISPATCHING:
stats.setQueuedJobs(count.intValue());
break;
case FINISHED:
stats.setMeanRunTime(meanRunTime.longValue());
stats.setMeanQueueTime(meanQueueTime.longValue());
stats.setFinishedJobs(count.intValue());
break;
default:
break;
}
}
}
List<ServiceStatistics> stats = new ArrayList<ServiceStatistics>(statsMap.values());
Collections.sort(stats, new Comparator<ServiceStatistics>() {
@Override
public int compare(ServiceStatistics o1, ServiceStatistics o2) {
ServiceRegistration reg1 = o1.getServiceRegistration();
ServiceRegistration reg2 = o2.getServiceRegistration();
int typeComparison = reg1.getServiceType().compareTo(reg2.getServiceType());
return typeComparison == 0 ? reg1.getHost().compareTo(reg2.getHost()) : typeComparison;
}
});
return stats;
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
Aggregations