Search in sources :

Example 16 with JpaJob

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

the class ServiceRegistryJpaImpl method updateInternal.

/**
 * Internal method to update a job, throwing unwrapped JPA exceptions.
 *
 * @param em
 *          the current entity manager
 * @param job
 *          the job to update
 * @return the updated job
 * @throws PersistenceException
 *           if there is an exception thrown while persisting the job via JPA
 * @throws IllegalArgumentException
 */
protected JpaJob updateInternal(EntityManager em, JpaJob job) throws PersistenceException {
    EntityTransaction tx = em.getTransaction();
    JpaJob originalJob = null;
    JpaJob fromDb = null;
    try {
        tx.begin();
        fromDb = em.find(JpaJob.class, job.getId());
        originalJob = JpaJob.from(fromDb.toJob());
        if (fromDb == null) {
            throw new NoResultException();
        }
        update(fromDb, job);
        em.merge(fromDb);
        tx.commit();
        job.setVersion(fromDb.toJob().getVersion());
        setJobUri(job);
        return job;
    } catch (PersistenceException e) {
        dumpJobs(originalJob, fromDb);
        if (tx.isActive()) {
            tx.rollback();
        }
        throw e;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) PersistenceException(javax.persistence.PersistenceException) NoResultException(javax.persistence.NoResultException) JpaJob(org.opencastproject.job.jpa.JpaJob)

Example 17 with JpaJob

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

the class ServiceRegistrationJpaImplTest method testQueryStatistics.

@Test
public void testQueryStatistics() throws Exception {
    HostRegistrationJpaImpl host = new HostRegistrationJpaImpl("http://localhost:8081", "http://localhost:8081", 1024L, 1, 1, true, false);
    ServiceRegistrationJpaImpl serviceReg = new ServiceRegistrationJpaImpl(host, "NOP", "/nop", false);
    Date now = new Date();
    host = env.tx(Queries.persistOrUpdate(host));
    serviceReg = env.tx(Queries.persistOrUpdate(serviceReg));
    JpaJob job = env.tx(Queries.persistOrUpdate(createJob(now, serviceReg)));
    JpaJob jobYesterday = env.tx(Queries.persistOrUpdate(createJob(DateUtils.addDays(now, -1), serviceReg)));
    /* find the job created at 'now' should reveal exactly one job */
    List<Object> statistic = env.tx(Queries.named.findAll("ServiceRegistration.statistics", P.p2("minDateCreated", now), P.p2("maxDateCreated", now)));
    Object[] stats = (Object[]) statistic.get(0);
    assertEquals(1, statistic.size());
    assertEquals(5, stats.length);
    assertEquals(serviceReg.getId().longValue(), ((Number) stats[0]).longValue());
    assertEquals(job.getStatus().ordinal(), ((Number) stats[1]).intValue());
    assertEquals(1, ((Number) stats[2]).intValue());
    assertEquals(500L, ((Number) stats[3]).longValue());
    assertEquals(1000L, ((Number) stats[4]).longValue());
    /* There are no jobs in the specific time interval */
    statistic = env.tx(Queries.named.findAll("ServiceRegistration.statistics", P.p2("minDateCreated", DateUtils.addDays(now, -3)), P.p2("maxDateCreated", DateUtils.addDays(now, -2))));
    assertEquals(0, statistic.size());
}
Also used : JpaJob(org.opencastproject.job.jpa.JpaJob) Date(java.util.Date) Test(org.junit.Test)

Example 18 with JpaJob

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

the class ServiceRegistrationJpaImplTest method testToString.

@Test
public void testToString() throws Exception {
    Job newJob = new JobImpl(3L, "test", "test_org", 0L, "simple", "do", null, DISPATCHING, "localhost", "remotehost", null, null, null, 100L, 200L, "result", 3L, 1L, true, null, 1.5F, null, 4L);
    JpaJob jpaJob = JpaJob.from(newJob);
    String jobString = "Job {id:3, operation:do, status:DISPATCHING}";
    assertEquals(jpaJob.toString(), jobString);
}
Also used : JobImpl(org.opencastproject.job.api.JobImpl) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) JpaJob(org.opencastproject.job.jpa.JpaJob) Test(org.junit.Test)

Example 19 with JpaJob

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

the class ServiceRegistrationJpaImplTest method createJob.

private JpaJob createJob(Date dateCreated, ServiceRegistrationJpaImpl serviceRegistry) {
    JpaJob job = new JpaJob(user, org, serviceRegistry, "NOP", null, null, true, 1.0F);
    job.setProcessorServiceRegistration(serviceRegistry);
    job.setQueueTime(500L);
    job.setRunTime(1000L);
    job.setDateCreated(dateCreated);
    return job;
}
Also used : JpaJob(org.opencastproject.job.jpa.JpaJob)

Example 20 with JpaJob

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

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