use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistrationTest method testScenarioManyJobsManyServices.
@Test
public void testScenarioManyJobsManyServices() throws Exception {
Job job1Try1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job job1Try2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job job1Try3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job job1Try4 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
List<String> list = new ArrayList<String>();
list.add("test");
Job job2Try1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
Job job2Try2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
Job job2Try3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_2, list, null, true, null);
serviceRegistry.maxAttemptsBeforeErrorState = 0;
ServiceRegistrationJpaImpl updatedService1;
ServiceRegistrationJpaImpl updatedService2;
ServiceRegistrationJpaImpl updatedService3;
// 1st try for job 1, failed on localhost
job1Try1.setStatus(Status.FAILED);
job1Try1.setJobType(regType1Localhost.getServiceType());
job1Try1.setProcessingHost(regType1Localhost.getHost());
job1Try1 = serviceRegistry.updateJob(job1Try1);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.WARNING, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
Assert.assertEquals(0, updatedService1.getErrorStateTrigger());
// 1st try for job 2, failed on localhost
job2Try1.setStatus(Status.FAILED);
job2Try1.setJobType(regType1Localhost.getServiceType());
job2Try1.setProcessingHost(regType1Localhost.getHost());
serviceRegistry.updateJob(job2Try1);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
// 2nd try for job 1, failed on remotehost1
job1Try2.setStatus(Status.FAILED);
job1Try2.setJobType(regType1Remotehost1.getServiceType());
job1Try2.setProcessingHost(regType1Remotehost1.getHost());
serviceRegistry.updateJob(job1Try2);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.WARNING, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
updatedService2.getWarningStateTrigger();
Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
// 2nd try for job 2, failed on remotehost1
job2Try2.setStatus(Status.FINISHED);
job2Try2.setJobType(regType1Remotehost1.getServiceType());
job2Try2.setProcessingHost(regType1Remotehost1.getHost());
serviceRegistry.updateJob(job2Try2);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
// 3rd try for job 1, failed on remotehost2
job1Try3.setStatus(Status.FINISHED);
job1Try3.setJobType(regType1Remotehost2.getServiceType());
job1Try3.setProcessingHost(regType1Remotehost2.getHost());
serviceRegistry.updateJob(job1Try3);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.ERROR, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
Assert.assertEquals(0, updatedService3.getWarningStateTrigger());
Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
// 3rd try for job2, failed on remotehost2
job2Try3.setStatus(Status.FAILED);
job2Try3.setJobType(regType1Remotehost2.getServiceType());
job2Try3.setProcessingHost(regType1Remotehost2.getHost());
serviceRegistry.updateJob(job2Try3);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.WARNING, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
// 4rd try for job1, failed on remotehost2
job1Try4.setStatus(Status.FAILED);
job1Try4.setJobType(regType1Remotehost2.getServiceType());
job1Try4.setProcessingHost(regType1Remotehost2.getHost());
serviceRegistry.updateJob(job1Try4);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.NORMAL, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService3.getServiceState());
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method getRelatedWarningServices.
/**
* Gets the services in WARNING state triggered by this job
*
* @param job
* the given job to get the related services
* @return a list of services triggered by the job
* @throws IllegalArgumentException
* if the given job was null
* @throws ServiceRegistryException
* if the there was a problem with the query
*/
private List<ServiceRegistrationJpaImpl> getRelatedWarningServices(JpaJob job) throws IllegalArgumentException, ServiceRegistryException {
if (job == null)
throw new IllegalArgumentException("job must not be null!");
Query query = null;
EntityManager em = null;
logger.debug("Finding services put in WARNING state by job {}", job.toJob().getSignature());
try {
em = emf.createEntityManager();
// TODO: modify the query to avoid to go through the list here
query = em.createNamedQuery("ServiceRegistration.relatedservices.warning");
query.setParameter("serviceType", job.getJobType());
List<ServiceRegistrationJpaImpl> jpaServices = new ArrayList<ServiceRegistrationJpaImpl>();
@SuppressWarnings("unchecked") List<ServiceRegistrationJpaImpl> jobResults = query.getResultList();
for (ServiceRegistrationJpaImpl relatedService : jobResults) {
if (relatedService.getWarningStateTrigger() == job.toJob().getSignature()) {
jpaServices.add(relatedService);
}
}
return jpaServices;
} catch (NoResultException e) {
return null;
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method updateServiceState.
/**
* Internal method to update the service registration state, throwing unwrapped JPA exceptions.
*
* @param em
* the current entity manager
* @param registration
* the service registration to update
* @return the updated service registration
* @throws PersistenceException
* if there is an exception thrown while persisting the job via JPA
* @throws IllegalArgumentException
*/
private ServiceRegistration updateServiceState(EntityManager em, ServiceRegistrationJpaImpl registration) throws PersistenceException {
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
ServiceRegistrationJpaImpl fromDb;
fromDb = em.find(ServiceRegistrationJpaImpl.class, registration.getId());
if (fromDb == null) {
throw new NoResultException();
}
fromDb.setServiceState(registration.getServiceState());
fromDb.setStateChanged(registration.getStateChanged());
fromDb.setWarningStateTrigger(registration.getWarningStateTrigger());
fromDb.setErrorStateTrigger(registration.getErrorStateTrigger());
tx.commit();
servicesStatistics.updateService(registration);
return registration;
} catch (PersistenceException e) {
if (tx.isActive()) {
tx.rollback();
}
throw e;
}
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class WorkflowServiceImplTest method setupJob.
private void setupJob(long id, String operation, ServiceRegistry mockServiceRegistry) throws ServiceRegistryException, NotFoundException {
ServiceRegistrationJpaImpl serviceRegistrationJpaImpl = EasyMock.createMock(ServiceRegistrationJpaImpl.class);
expect(serviceRegistrationJpaImpl.getHost()).andReturn("http://localhost:8080");
expect(serviceRegistrationJpaImpl.getServiceType()).andReturn(operation);
replay(serviceRegistrationJpaImpl);
List<String> arguments = new LinkedList<String>();
arguments.add(Long.toString(id));
Job job = createNiceMock(Job.class);
expect(job.getId()).andStubReturn(id);
expect(job.getCreator()).andStubReturn(securityService.getUser().getUsername());
expect(job.getOrganization()).andStubReturn(securityService.getOrganization().getId());
expect(job.getOperation()).andStubReturn("RESUME");
expect(job.getArguments()).andStubReturn(arguments);
expect(job.isDispatchable()).andStubReturn(false);
expect(job.getStatus()).andStubReturn(Job.Status.INSTANTIATED);
replay(job);
expect(mockServiceRegistry.getJob(id)).andReturn(job).anyTimes();
expect(mockServiceRegistry.updateJob(job)).andReturn(job);
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl 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();
}
}
Aggregations