Search in sources :

Example 1 with Status

use of org.opencastproject.job.api.Job.Status in project opencast by opencast.

the class ServiceRegistryJpaImpl method getHostLoads.

/**
 * Gets a map of hosts to the number of jobs currently loading that host
 *
 * @param em
 *          the entity manager
 *
 * @return the map of hosts to job counts
 */
SystemLoad getHostLoads(EntityManager em) {
    final SystemLoad systemLoad = new SystemLoad();
    // Find all jobs that are currently running on any given host, or get all of them
    Query q = em.createNamedQuery("ServiceRegistration.hostloads");
    List<Integer> statuses = new LinkedList<Integer>();
    for (Status status : JOB_STATUSES_INFLUENCING_LOAD_BALANCING) {
        statuses.add(status.ordinal());
    }
    q.setParameter("statuses", statuses);
    // Note: This is used in the query to filter out workflow jobs.
    // These jobs are load balanced by the workflow service directly.
    q.setParameter("workflow_type", TYPE_WORKFLOW);
    // Accumulate the numbers for relevant job statuses per host
    for (Object result : q.getResultList()) {
        Object[] resultArray = (Object[]) result;
        String host = String.valueOf(resultArray[0]);
        Status status = Status.values()[(int) resultArray[1]];
        float load = ((Number) resultArray[2]).floatValue();
        // Only queued, and running jobs are adding to the load, so every other status is discarded
        if (status == null || !JOB_STATUSES_INFLUENCING_LOAD_BALANCING.contains(status)) {
            load = 0.0f;
        }
        // Add the service registration
        NodeLoad serviceLoad;
        if (systemLoad.containsHost(host)) {
            serviceLoad = systemLoad.get(host);
            serviceLoad.setLoadFactor(serviceLoad.getLoadFactor() + load);
        } else {
            serviceLoad = new NodeLoad(host, load);
        }
        systemLoad.addNodeLoad(serviceLoad);
    }
    // This is important, otherwise services which have no current load are not listed in the output!
    for (HostRegistration h : getHostRegistrations(em)) {
        if (!systemLoad.containsHost(h.getBaseUrl())) {
            systemLoad.addNodeLoad(new NodeLoad(h.getBaseUrl(), 0.0f));
        }
    }
    return systemLoad;
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) SystemLoad(org.opencastproject.serviceregistry.api.SystemLoad) LinkedList(java.util.LinkedList) NodeLoad(org.opencastproject.serviceregistry.api.SystemLoad.NodeLoad) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration)

Example 2 with Status

use of org.opencastproject.job.api.Job.Status 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();
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) JpaJob(org.opencastproject.job.jpa.JpaJob) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) 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 3 with Status

use of org.opencastproject.job.api.Job.Status 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());
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) Date(java.util.Date)

Example 4 with Status

use of org.opencastproject.job.api.Job.Status in project opencast by opencast.

the class SearchServiceImplTest method testPopulateIndex.

@SuppressWarnings("unchecked")
@Test
public void testPopulateIndex() throws Exception {
    // This service registry must return a list of jobs
    List<String> args = new ArrayList<String>();
    args.add(new DefaultOrganization().getId());
    List<Job> jobs = new ArrayList<Job>();
    for (long i = 0; i < 10; i++) {
        MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
        mediaPackage.setIdentifier(IdBuilderFactory.newInstance().newIdBuilder().createNew());
        searchDatabase.storeMediaPackage(mediaPackage, acl, new Date());
        String payload = MediaPackageParser.getAsXml(mediaPackage);
        Job job = new JobImpl(i);
        job.setArguments(args);
        job.setPayload(payload);
        job.setStatus(Status.FINISHED);
        jobs.add(job);
    }
    ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(serviceRegistry.createJob((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (List<String>) EasyMock.anyObject(), (String) EasyMock.anyObject(), EasyMock.anyBoolean())).andReturn(new JobImpl()).anyTimes();
    EasyMock.expect(serviceRegistry.updateJob((Job) EasyMock.anyObject())).andReturn(new JobImpl()).anyTimes();
    EasyMock.expect(serviceRegistry.getJobs((String) EasyMock.anyObject(), (Status) EasyMock.anyObject())).andReturn(jobs).anyTimes();
    EasyMock.replay(serviceRegistry);
    service.setServiceRegistry(serviceRegistry);
    OrganizationDirectoryService orgDirectory = EasyMock.createNiceMock(OrganizationDirectoryService.class);
    EasyMock.expect(orgDirectory.getOrganization((String) EasyMock.anyObject())).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.replay(orgDirectory);
    service.setOrganizationDirectoryService(orgDirectory);
    // We should have nothing in the search index
    assertEquals(0, service.getByQuery(new SearchQuery()).size());
    service.populateIndex("System Admin");
    // This time we should have 10 results
    assertEquals(10, service.getByQuery(new SearchQuery()).size());
}
Also used : Status(org.opencastproject.job.api.Job.Status) SearchQuery(org.opencastproject.search.api.SearchQuery) JobImpl(org.opencastproject.job.api.JobImpl) ArrayList(java.util.ArrayList) Date(java.util.Date) MediaPackage(org.opencastproject.mediapackage.MediaPackage) List(java.util.List) ArrayList(java.util.ArrayList) AccessControlList(org.opencastproject.security.api.AccessControlList) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Job(org.opencastproject.job.api.Job) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) Test(org.junit.Test)

Example 5 with Status

use of org.opencastproject.job.api.Job.Status in project opencast by opencast.

the class ServiceRegistryInMemoryImpl method updateInternal.

private Job updateInternal(Job job) {
    Date now = new Date();
    Status status = job.getStatus();
    if (job.getDateCreated() == null) {
        job.setDateCreated(now);
    }
    if (Status.RUNNING.equals(status)) {
        job.setDateStarted(now);
        job.setQueueTime(now.getTime() - job.getDateCreated().getTime());
    } else if (Status.FAILED.equals(status)) {
        // failed jobs may not have even started properly
        job.setDateCompleted(now);
        if (job.getDateStarted() != null) {
            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
            job.setDateStarted(job.getDateCreated());
        }
        job.setDateCompleted(now);
        job.setRunTime(now.getTime() - job.getDateStarted().getTime());
        // Cleanup local list of jobs assigned to a specific service
        for (Entry<String, List<ServiceRegistrationInMemoryImpl>> service : services.entrySet()) {
            for (ServiceRegistrationInMemoryImpl srv : service.getValue()) {
                Set<Job> jobs = jobHosts.get(srv);
                if (jobs != null) {
                    Set<Job> updatedJobs = new HashSet<>();
                    for (Job savedJob : jobs) {
                        if (savedJob.getId() != job.getId())
                            updatedJobs.add(savedJob);
                    }
                    jobHosts.put(srv, updatedJobs);
                }
            }
        }
    }
    return job;
}
Also used : Status(org.opencastproject.job.api.Job.Status) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) Date(java.util.Date) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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