Search in sources :

Example 26 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method cleanRunningJobs.

/**
 * Find all running jobs on this service and set them to RESET or CANCELED.
 *
 * @param serviceType
 *          the service type
 * @param baseUrl
 *          the base url
 * @throws ServiceRegistryException
 *           if there is a problem communicating with the jobs database
 */
private void cleanRunningJobs(String serviceType, String baseUrl) throws ServiceRegistryException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        TypedQuery<JpaJob> query = em.createNamedQuery("Job.processinghost.status", JpaJob.class);
        query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
        List<Integer> statuses = new ArrayList<Integer>();
        statuses.add(Status.RUNNING.ordinal());
        statuses.add(Status.DISPATCHING.ordinal());
        statuses.add(Status.WAITING.ordinal());
        query.setParameter("statuses", statuses);
        query.setParameter("host", baseUrl);
        query.setParameter("serviceType", serviceType);
        List<JpaJob> unregisteredJobs = query.getResultList();
        for (JpaJob job : unregisteredJobs) {
            if (job.isDispatchable()) {
                em.refresh(job);
                // If this job has already been treated
                if (Status.CANCELED.equals(job.getStatus()) || Status.RESTART.equals(job.getStatus()))
                    continue;
                if (job.getRootJob() != null && Status.PAUSED.equals(job.getRootJob().getStatus())) {
                    JpaJob rootJob = job.getRootJob();
                    cancelAllChildren(rootJob, em);
                    rootJob.setStatus(Status.RESTART);
                    rootJob.setOperation(START_OPERATION);
                    em.merge(rootJob);
                    continue;
                }
                logger.info("Marking child jobs from job {} as canceled", job);
                cancelAllChildren(job, em);
                logger.info("Rescheduling lost job {}", job);
                job.setStatus(Status.RESTART);
                job.setProcessorServiceRegistration(null);
            } else {
                logger.info("Marking lost job {} as failed", job);
                job.setStatus(Status.FAILED);
            }
            em.merge(job);
        }
        tx.commit();
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) 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 27 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method getChildren.

@SuppressWarnings("unchecked")
private List<JpaJob> getChildren(EntityManager em, long id) throws Exception {
    Query query = em.createNamedQuery("Job.children");
    query.setParameter("id", id);
    List<JpaJob> childJobs = query.getResultList();
    List<JpaJob> resultJobs = new ArrayList<>(childJobs);
    for (JpaJob childJob : childJobs) {
        resultJobs.addAll(getChildren(em, childJob.getId()));
    }
    return resultJobs;
}
Also used : Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) JpaJob(org.opencastproject.job.jpa.JpaJob)

Example 28 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method updateJob.

private JpaJob updateJob(JpaJob job) throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Job oldJob = getJob(job.getId());
        JpaJob jpaJob = updateInternal(em, job);
        if (!TYPE_WORKFLOW.equals(job.getJobType()) && job.getJobLoad() > 0.0f && job.getProcessorServiceRegistration() != null && job.getProcessorServiceRegistration().equals(getRegistryHostname())) {
            processCachedLoadChange(job);
        }
        // All WorkflowService Jobs will be ignored
        if (oldJob.getStatus() != job.getStatus() && !TYPE_WORKFLOW.equals(job.getJobType())) {
            updateServiceForFailover(job);
        }
        return jpaJob;
    } catch (PersistenceException e) {
        throw new ServiceRegistryException(e);
    } catch (NotFoundException e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) NotFoundException(org.opencastproject.util.NotFoundException) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) JpaJob(org.opencastproject.job.jpa.JpaJob) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 29 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method getJpaJob.

private JpaJob getJpaJob(long id) throws NotFoundException, ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        JpaJob jpaJob = em.find(JpaJob.class, id);
        if (jpaJob == null) {
            throw new NotFoundException("Job " + id + " not found");
        }
        // JPA's caches can be out of date if external changes (e.g. another node in the cluster) have been made to
        // this row in the database
        em.refresh(jpaJob);
        setJobUri(jpaJob);
        return jpaJob;
    } catch (Exception e) {
        if (e instanceof NotFoundException) {
            throw (NotFoundException) e;
        } else {
            throw new ServiceRegistryException(e);
        }
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) 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)

Aggregations

JpaJob (org.opencastproject.job.jpa.JpaJob)29 NotFoundException (org.opencastproject.util.NotFoundException)13 PersistenceException (javax.persistence.PersistenceException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)11 EntityManager (javax.persistence.EntityManager)10 NoResultException (javax.persistence.NoResultException)10 RollbackException (javax.persistence.RollbackException)10 URISyntaxException (java.net.URISyntaxException)9 Test (org.junit.Test)9 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)9 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)9 ConfigurationException (org.osgi.service.cm.ConfigurationException)9 Job (org.opencastproject.job.api.Job)7 ArrayList (java.util.ArrayList)6 EntityTransaction (javax.persistence.EntityTransaction)6 JpaJob.fnToJob (org.opencastproject.job.jpa.JpaJob.fnToJob)6 HttpStatus (org.apache.http.HttpStatus)4 Status (org.opencastproject.job.api.Job.Status)4 Date (java.util.Date)3 Query (javax.persistence.Query)2