Search in sources :

Example 11 with Status

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());
}
Also used : Status(org.opencastproject.job.api.Job.Status) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) IOException(java.io.IOException) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job)

Example 12 with Status

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 &gt; 0
 * @param limit apply limit to the db query if limit &gt; 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);
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) ArrayList(java.util.ArrayList) JpaJob(org.opencastproject.job.jpa.JpaJob) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 13 with Status

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();
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) JaxbServiceStatistics(org.opencastproject.serviceregistry.api.JaxbServiceStatistics) EntityManager(javax.persistence.EntityManager) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JaxbServiceStatistics(org.opencastproject.serviceregistry.api.JaxbServiceStatistics) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Aggregations

Status (org.opencastproject.job.api.Job.Status)13 Job (org.opencastproject.job.api.Job)8 ArrayList (java.util.ArrayList)6 HttpStatus (org.apache.http.HttpStatus)6 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 NoResultException (javax.persistence.NoResultException)4 PersistenceException (javax.persistence.PersistenceException)4 RollbackException (javax.persistence.RollbackException)4 JpaJob (org.opencastproject.job.jpa.JpaJob)4 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)4 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)4 NotFoundException (org.opencastproject.util.NotFoundException)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Test (org.junit.Test)3 JaxbJob (org.opencastproject.job.api.JaxbJob)3