use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method getHistorySize.
/**
* Gets the failed jobs history for the given service registration
*
* @param serviceRegistration
* @return the failed jobs history size
* @throws IllegalArgumentException
* if parameter is null
* @throws ServiceRegistryException
*/
private int getHistorySize(ServiceRegistration serviceRegistration) throws IllegalArgumentException, ServiceRegistryException {
if (serviceRegistration == null)
throw new IllegalArgumentException("serviceRegistration must not be null!");
Query query = null;
EntityManager em = null;
logger.debug("Calculating count of jobs who failed due to service {}", serviceRegistration.toString());
try {
em = emf.createEntityManager();
query = em.createNamedQuery("Job.count.history.failed");
query.setParameter("serviceType", serviceRegistration.getServiceType());
query.setParameter("host", serviceRegistration.getHost());
Number number = (Number) query.getSingleResult();
return number.intValue();
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException 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.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method unregisterHost.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#unregisterHost(java.lang.String)
*/
@Override
public void unregisterHost(String host) throws ServiceRegistryException {
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
HostRegistrationJpaImpl existingHostRegistration = fetchHostRegistration(em, host);
if (existingHostRegistration == null) {
throw new IllegalArgumentException("Host '" + host + "' is not registered, so it can not be unregistered");
}
existingHostRegistration.setOnline(false);
for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
unRegisterService(serviceRegistration.getServiceType(), serviceRegistration.getHost());
}
em.merge(existingHostRegistration);
logger.info("Unregistering {}", host);
tx.commit();
logger.info("Host {} unregistered", host);
hostsStatistics.updateHost(existingHostRegistration);
} catch (Exception e) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException 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.serviceregistry.api.ServiceRegistryException 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