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