Search in sources :

Example 6 with JpaJob

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

the class ServiceRegistryJpaImpl method updateJob.

@Override
public Job updateJob(Job job) throws ServiceRegistryException {
    JpaJob jpaJob = JpaJob.from(job);
    jpaJob.setProcessorServiceRegistration((ServiceRegistrationJpaImpl) getServiceRegistration(job.getJobType(), job.getProcessingHost()));
    return updateJob(jpaJob).toJob();
}
Also used : JpaJob(org.opencastproject.job.jpa.JpaJob)

Example 7 with JpaJob

use of org.opencastproject.job.jpa.JpaJob 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);
    }
}
Also used : 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) 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 8 with JpaJob

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

the class ServiceRegistryJpaImpl method cleanUndispatchableJobs.

/**
 * Find all undispatchable jobs that were orphaned when this host was last deactivated and set them to CANCELED.
 */
private void cleanUndispatchableJobs(String hostName) {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        Query query = em.createNamedQuery("Job.undispatchable.status");
        List<Integer> statuses = new ArrayList<Integer>();
        statuses.add(Status.INSTANTIATED.ordinal());
        statuses.add(Status.RUNNING.ordinal());
        query.setParameter("statuses", statuses);
        @SuppressWarnings("unchecked") List<JpaJob> undispatchableJobs = query.getResultList();
        for (JpaJob job : undispatchableJobs) {
            // Make sure the job was processed on this host
            String jobHost = "";
            if (job.getProcessorServiceRegistration() != null) {
                jobHost = job.getProcessorServiceRegistration().getHost();
            }
            if (!jobHost.equals(hostName)) {
                logger.debug("Will not cancel undispatchable job {}, it is running on a different host", job);
            } else {
                logger.info("Cancelling the running undispatchable job {}, it was orphaned on this host", job);
                job.setStatus(Status.CANCELED);
                em.merge(job);
            }
        }
        tx.commit();
    } catch (Exception e) {
        logger.error("Unable to clean undispatchable jobs! {}", e.getMessage());
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) 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)

Example 9 with JpaJob

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

the class ServiceRegistryJpaImpl method getJobs.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getJobs(java.lang.String, Status)
 */
@Override
public List<Job> getJobs(String type, Status status) throws ServiceRegistryException {
    TypedQuery<JpaJob> query = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        if (type == null && status == null) {
            query = em.createNamedQuery("Job.all", JpaJob.class);
        } else if (type == null) {
            query = em.createNamedQuery("Job.status", JpaJob.class);
            query.setParameter("status", status.ordinal());
        } else if (status == null) {
            query = em.createNamedQuery("Job.type", JpaJob.class);
            query.setParameter("serviceType", type);
        } else {
            query = em.createNamedQuery("Job", JpaJob.class);
            query.setParameter("status", status.ordinal());
            query.setParameter("serviceType", type);
        }
        List<JpaJob> jobs = query.getResultList();
        for (JpaJob job : jobs) {
            setJobUri(job);
        }
        return $(jobs).map(fnToJob()).toList();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) 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 10 with JpaJob

use of org.opencastproject.job.jpa.JpaJob 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)

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