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();
}
}
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;
}
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();
}
}
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();
}
}
Aggregations