use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method update.
/**
* Sets the queue and runtimes and other elements of a persistent job based on a job that's been modified in memory.
* Times on both the objects must be modified, since the in-memory job must not be stale.
*
* @param fromDb
* The job from the database
* @param jpaJob
* The in-memory job
*/
private void update(JpaJob fromDb, JpaJob jpaJob) {
final Job job = jpaJob.toJob();
final Date now = new Date();
final Status status = job.getStatus();
final Status fromDbStatus = fromDb.getStatus();
fromDb.setPayload(job.getPayload());
fromDb.setStatus(job.getStatus());
fromDb.setDispatchable(job.isDispatchable());
fromDb.setVersion(job.getVersion());
fromDb.setOperation(job.getOperation());
fromDb.setArguments(job.getArguments());
fromDb.setBlockedJobIds(job.getBlockedJobIds());
fromDb.setBlockingJobId(job.getBlockingJobId());
if (job.getDateCreated() == null) {
jpaJob.setDateCreated(now);
fromDb.setDateCreated(now);
job.setDateCreated(now);
}
if (job.getProcessingHost() != null) {
ServiceRegistrationJpaImpl processingService = (ServiceRegistrationJpaImpl) getServiceRegistration(job.getJobType(), job.getProcessingHost());
fromDb.setProcessorServiceRegistration(processingService);
}
if (Status.RUNNING.equals(status) && !Status.WAITING.equals(fromDbStatus)) {
jpaJob.setDateStarted(now);
jpaJob.setQueueTime(now.getTime() - job.getDateCreated().getTime());
fromDb.setDateStarted(now);
fromDb.setQueueTime(now.getTime() - job.getDateCreated().getTime());
job.setDateStarted(now);
job.setQueueTime(now.getTime() - job.getDateCreated().getTime());
} else if (Status.FAILED.equals(status)) {
// failed jobs may not have even started properly
fromDb.setDateCompleted(now);
jpaJob.setDateCompleted(now);
job.setDateCompleted(now);
if (job.getDateStarted() != null) {
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
} else if (Status.FINISHED.equals(status)) {
if (job.getDateStarted() == null) {
// Some services (e.g. ingest) don't use job dispatching, since they start immediately and handle their own
// lifecycle. In these cases, if the start date isn't set, use the date created as the start date
jpaJob.setDateStarted(job.getDateCreated());
job.setDateStarted(job.getDateCreated());
}
jpaJob.setDateCompleted(now);
jpaJob.setRunTime(now.getTime() - job.getDateStarted().getTime());
fromDb.setDateCompleted(now);
fromDb.setRunTime(now.getTime() - job.getDateStarted().getTime());
job.setDateCompleted(now);
job.setRunTime(now.getTime() - job.getDateStarted().getTime());
}
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistryJpaImpl method createJob.
/**
* Creates a job on a remote host.
*/
public Job createJob(String host, String serviceType, String operation, List<String> arguments, String payload, boolean dispatchable, Job parentJob, float jobLoad) throws ServiceRegistryException {
if (StringUtils.isBlank(host)) {
throw new IllegalArgumentException("Host can't be null");
}
if (StringUtils.isBlank(serviceType)) {
throw new IllegalArgumentException("Service type can't be null");
}
if (StringUtils.isBlank(operation)) {
throw new IllegalArgumentException("Operation can't be null");
}
EntityManager em = null;
EntityTransaction tx = null;
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
ServiceRegistrationJpaImpl creatingService = getServiceRegistration(em, serviceType, host);
if (creatingService == null) {
throw new ServiceRegistryException("No service registration exists for type '" + serviceType + "' on host '" + host + "'");
}
if (creatingService.getHostRegistration().isMaintenanceMode()) {
logger.warn("Creating a job from {}, which is currently in maintenance mode.", creatingService.getHost());
} else if (!creatingService.getHostRegistration().isActive()) {
logger.warn("Creating a job from {}, which is currently inactive.", creatingService.getHost());
}
User currentUser = securityService.getUser();
Organization currentOrganization = securityService.getOrganization();
JpaJob jpaJob = new JpaJob(currentUser, currentOrganization, creatingService, operation, arguments, payload, dispatchable, jobLoad);
// Bind the given parent job to the new job
if (parentJob != null) {
// Get the JPA instance of the parent job
JpaJob jpaParentJob;
try {
jpaParentJob = getJpaJob(parentJob.getId());
} catch (NotFoundException e) {
logger.error("{} not found in the persistence context", parentJob);
throw new ServiceRegistryException(e);
}
jpaJob.setParentJob(jpaParentJob);
// Get the JPA instance of the root job
JpaJob jpaRootJob = jpaParentJob;
if (parentJob.getRootJobId() != null) {
try {
jpaRootJob = getJpaJob(parentJob.getRootJobId());
} catch (NotFoundException e) {
logger.error("job with id {} not found in the persistence context", parentJob.getRootJobId());
throw new ServiceRegistryException(e);
}
}
jpaJob.setRootJob(jpaRootJob);
}
// if this job is not dispatchable, it must be handled by the host that has created it
if (dispatchable) {
jpaJob.setStatus(Status.QUEUED);
} else {
jpaJob.setProcessorServiceRegistration(creatingService);
}
em.persist(jpaJob);
tx.commit();
setJobUri(jpaJob);
Job job = jpaJob.toJob();
return job;
} catch (RollbackException e) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
throw e;
} finally {
if (em != null)
em.close();
}
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl 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.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistrationTest method testScenarioOneJobManyServices.
@Test
public void testScenarioOneJobManyServices() throws Exception {
Job jobTry1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job jobTry2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job jobTry3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job jobTry4 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
ServiceRegistrationJpaImpl updatedService1;
ServiceRegistrationJpaImpl updatedService2;
ServiceRegistrationJpaImpl updatedService3;
// 1st try, failed on localhost
jobTry1.setStatus(Status.FAILED);
jobTry1.setJobType(regType1Localhost.getServiceType());
jobTry1.setProcessingHost(regType1Localhost.getHost());
jobTry1 = serviceRegistry.updateJob(jobTry1);
updatedService1 = (ServiceRegistrationJpaImpl) serviceRegistry.getServiceRegistration(JOB_TYPE_1, regType1Localhost.getHost());
Assert.assertEquals(ServiceState.WARNING, updatedService1.getServiceState());
Assert.assertEquals(0, updatedService1.getErrorStateTrigger());
// 2nd try, failed on remotehost1
jobTry2.setStatus(Status.FAILED);
jobTry2.setJobType(regType1Remotehost1.getServiceType());
jobTry2.setProcessingHost(regType1Remotehost1.getHost());
jobTry2 = serviceRegistry.updateJob(jobTry2);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
Assert.assertEquals(ServiceState.NORMAL, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(0, updatedService2.getWarningStateTrigger());
Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
// 3rd try, failed on remotehost2
jobTry3.setStatus(Status.FAILED);
jobTry3.setJobType(regType1Remotehost2.getServiceType());
jobTry3.setProcessingHost(regType1Remotehost2.getHost());
jobTry3 = serviceRegistry.updateJob(jobTry3);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.NORMAL, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.WARNING, updatedService3.getServiceState());
Assert.assertEquals(0, updatedService2.getErrorStateTrigger());
// 4th try, finished on localhost
jobTry4.setStatus(Status.FINISHED);
jobTry4.setJobType(regType1Localhost.getServiceType());
jobTry4.setProcessingHost(regType1Localhost.getHost());
jobTry4 = serviceRegistry.updateJob(jobTry4);
updatedService1 = getUpdatedService(regType1Localhost);
updatedService2 = getUpdatedService(regType1Remotehost1);
updatedService3 = getUpdatedService(regType1Remotehost2);
Assert.assertEquals(ServiceState.NORMAL, updatedService1.getServiceState());
Assert.assertEquals(ServiceState.NORMAL, updatedService2.getServiceState());
Assert.assertEquals(ServiceState.ERROR, updatedService3.getServiceState());
}
use of org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl in project opencast by opencast.
the class ServiceRegistrationTest method testScenarioOneJobOneService.
@Test
public void testScenarioOneJobOneService() throws Exception {
Job jobTry1 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job jobTry2 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
Job jobTry3 = serviceRegistry.createJob(regType1Localhost.getHost(), regType1Localhost.getServiceType(), OPERATION_NAME_1, null, null, true, null);
ServiceRegistrationJpaImpl updatedService;
// 1st try, failed on localhost
jobTry1.setStatus(Status.FAILED);
jobTry1.setJobType(regType1Localhost.getServiceType());
jobTry1.setProcessingHost(regType1Localhost.getHost());
jobTry1 = serviceRegistry.updateJob(jobTry1);
updatedService = getUpdatedService(regType1Localhost);
Assert.assertEquals(ServiceState.WARNING, updatedService.getServiceState());
Assert.assertEquals(0, updatedService.getErrorStateTrigger());
// 2nd try, failed on localhost
jobTry2.setStatus(Status.FAILED);
jobTry2.setJobType(regType1Localhost.getServiceType());
jobTry2.setProcessingHost(regType1Localhost.getHost());
jobTry2 = serviceRegistry.updateJob(jobTry2);
updatedService = getUpdatedService(regType1Localhost);
Assert.assertEquals(ServiceState.WARNING, updatedService.getServiceState());
Assert.assertEquals(0, updatedService.getErrorStateTrigger());
// 3rd try, finished on localhost
jobTry3.setStatus(Status.FINISHED);
jobTry3.setJobType(regType1Localhost.getServiceType());
jobTry3.setProcessingHost(regType1Localhost.getHost());
jobTry3 = serviceRegistry.updateJob(jobTry3);
updatedService = getUpdatedService(regType1Localhost);
Assert.assertEquals(ServiceState.NORMAL, updatedService.getServiceState());
Assert.assertEquals(0, updatedService.getErrorStateTrigger());
}
Aggregations