use of org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method fetchHostRegistration.
/**
* Fetches a host registration from persistence.
*
* @param em
* an active entity manager
* @param host
* the host name
* @return the host registration, or null if none exists
*/
protected HostRegistrationJpaImpl fetchHostRegistration(EntityManager em, String host) {
Query query = em.createNamedQuery("HostRegistration.byHostName");
query.setParameter("host", host);
try {
return (HostRegistrationJpaImpl) query.getSingleResult();
} catch (NoResultException e) {
logger.debug("No existing host registration for {}", host);
return null;
}
}
use of org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl 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.impl.jpa.HostRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method setOnlineStatus.
/**
* Sets the online status of a service registration.
*
* @param serviceType
* The job type
* @param baseUrl
* the host URL
* @param online
* whether the service is online or off
* @param jobProducer
* whether this service produces jobs for long running operations
* @return the service registration
*/
protected ServiceRegistration setOnlineStatus(String serviceType, String baseUrl, String path, boolean online, Boolean jobProducer) throws ServiceRegistryException {
if (isBlank(serviceType) || isBlank(baseUrl)) {
throw new IllegalArgumentException("serviceType and baseUrl must not be blank");
}
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, baseUrl);
if (hostRegistration == null) {
throw new IllegalStateException("A service registration can not be updated when it has no associated host registration");
}
ServiceRegistrationJpaImpl registration = getServiceRegistration(em, serviceType, baseUrl);
if (registration == null) {
if (isBlank(path)) {
// we can not create a new registration without a path
throw new IllegalArgumentException("path must not be blank when registering new services");
}
if (jobProducer == null) {
// if we are not provided a value, consider it to be false
registration = new ServiceRegistrationJpaImpl(hostRegistration, serviceType, path, false);
} else {
registration = new ServiceRegistrationJpaImpl(hostRegistration, serviceType, path, jobProducer);
}
em.persist(registration);
} else {
if (StringUtils.isNotBlank(path))
registration.setPath(path);
registration.setOnline(online);
if (jobProducer != null) {
// if we are not provided a value, don't update the persistent value
registration.setJobProducer(jobProducer);
}
em.merge(registration);
}
tx.commit();
hostsStatistics.updateHost(hostRegistration);
servicesStatistics.updateService(registration);
return registration;
} 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.impl.jpa.HostRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method disableHost.
/**
* {@inheritDoc}
*
* @see org.opencastproject.serviceregistry.api.ServiceRegistry#disableHost(String)
*/
@Override
public void disableHost(String host) throws ServiceRegistryException, NotFoundException {
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
HostRegistrationJpaImpl hostRegistration = fetchHostRegistration(em, host);
if (hostRegistration == null) {
throw new NotFoundException("Host '" + host + "' is not currently registered, so it can not be disabled");
} else {
hostRegistration.setActive(false);
for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
ServiceRegistrationJpaImpl registration = (ServiceRegistrationJpaImpl) serviceRegistration;
registration.setActive(false);
em.merge(registration);
servicesStatistics.updateService(registration);
}
em.merge(hostRegistration);
}
logger.info("Disabling {}", host);
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.impl.jpa.HostRegistrationJpaImpl 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();
}
}
Aggregations