use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method registerHost.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#registerHost(String, String, long, int, float)
*/
@Override
public void registerHost(String host, String address, long memory, int cores, float maxLoad) throws ServiceRegistryException {
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
// Find the existing registrations for this host and if it exists, update it
HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
if (hostRegistration == null) {
hostRegistration = new HostRegistrationJpaImpl(host, address, memory, cores, maxLoad, true, false);
em.persist(hostRegistration);
} else {
hostRegistration.setIpAddress(address);
hostRegistration.setMemory(memory);
hostRegistration.setCores(cores);
hostRegistration.setMaxLoad(maxLoad);
hostRegistration.setOnline(true);
em.merge(hostRegistration);
}
logger.info("Registering {} with a maximum load of {}", host, maxLoad);
tx.commit();
hostsStatistics.updateHost(hostRegistration);
} 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 enableHost.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#enableHost(String)
*/
@Override
public void enableHost(String host) throws ServiceRegistryException, NotFoundException {
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
// Find the existing registrations for this host and if it exists, update it
HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
if (hostRegistration == null) {
throw new NotFoundException("Host '" + host + "' is currently not registered, so it can not be enabled");
} else {
hostRegistration.setActive(true);
em.merge(hostRegistration);
}
logger.info("Enabling {}", host);
tx.commit();
tx.begin();
for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
registration.setActive(true);
em.merge(registration);
servicesStatistics.updateService(registration);
}
tx.commit();
hostsStatistics.updateHost(hostRegistration);
} catch (NotFoundException e) {
throw e;
} 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 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.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method getJobPayloads.
@Override
public List<String> getJobPayloads(String operation) throws ServiceRegistryException {
EntityManager em = emf.createEntityManager();
try {
TypedQuery<String> query = em.createNamedQuery("Job.payload", String.class);
query.setParameter("operation", operation);
logger.debug("Requesting job payloads using query: {}", query);
return query.getResultList();
} catch (Exception e) {
throw new ServiceRegistryException(e);
}
}
use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.
the class ServiceRegistryJpaImpl method countByHost.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#countByHost(java.lang.String, java.lang.String,
* Status)
*/
@Override
public long countByHost(String serviceType, String host, Status status) throws ServiceRegistryException {
EntityManager em = null;
try {
em = emf.createEntityManager();
Query query = em.createNamedQuery("Job.countByHost");
query.setParameter("status", status.ordinal());
query.setParameter("serviceType", serviceType);
query.setParameter("host", host);
Number countResult = (Number) query.getSingleResult();
return countResult.longValue();
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
Aggregations