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;
}
}
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());
}
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);
}
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;
}
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 > 0
* @param limit apply limit to the db query if limit > 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);
}
}
Aggregations