Search in sources :

Example 1 with ServiceRegistrationJpaImpl

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());
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) Date(java.util.Date)

Example 2 with ServiceRegistrationJpaImpl

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();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) JpaJob(org.opencastproject.job.jpa.JpaJob) JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) RollbackException(javax.persistence.RollbackException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 3 with ServiceRegistrationJpaImpl

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();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 4 with ServiceRegistrationJpaImpl

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());
}
Also used : ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) Job(org.opencastproject.job.api.Job) Test(org.junit.Test)

Example 5 with ServiceRegistrationJpaImpl

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());
}
Also used : ServiceRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl) Job(org.opencastproject.job.api.Job) Test(org.junit.Test)

Aggregations

ServiceRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)16 EntityManager (javax.persistence.EntityManager)9 NoResultException (javax.persistence.NoResultException)8 NotFoundException (org.opencastproject.util.NotFoundException)8 PersistenceException (javax.persistence.PersistenceException)7 RollbackException (javax.persistence.RollbackException)7 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)7 URISyntaxException (java.net.URISyntaxException)6 Job (org.opencastproject.job.api.Job)6 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 ArrayList (java.util.ArrayList)5 EntityTransaction (javax.persistence.EntityTransaction)5 Query (javax.persistence.Query)4 TypedQuery (javax.persistence.TypedQuery)4 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)3 HostRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)3