Search in sources :

Example 11 with JpaJob

use of org.opencastproject.job.jpa.JpaJob 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 12 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method removeJobs.

@Override
public void removeJobs(List<Long> jobIds) throws NotFoundException, ServiceRegistryException {
    for (long jobId : jobIds) {
        if (jobId < 1)
            throw new NotFoundException("Job ID must be greater than zero (0)");
    }
    logger.debug("Start deleting jobs with IDs '{}'", jobIds);
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        for (long jobId : jobIds) {
            JpaJob job = em.find(JpaJob.class, jobId);
            if (job == null) {
                logger.error("Job with Id {} cannot be deleted: Not found.", jobId);
                tx.rollback();
                throw new NotFoundException("Job with ID '" + jobId + "' not found");
            }
            deleteChildJobs(em, tx, jobId);
            em.remove(job);
        }
        tx.commit();
        logger.debug("Jobs with IDs '{}' deleted", jobIds);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) JpaJob(org.opencastproject.job.jpa.JpaJob)

Example 13 with JpaJob

use of org.opencastproject.job.jpa.JpaJob 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 14 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method getJobsByStatus.

/**
 * Get the list of jobs with status from the given statuses.
 *
 * @param em
 *          the entity manager
 * @param statuses
 *          variable sized array of status values to test on jobs
 * @return list of jobs with status from statuses
 * @throws ServiceRegistryException
 *           if there is a problem communicating with the jobs database
 */
public List<JpaJob> getJobsByStatus(EntityManager em, Status... statuses) throws ServiceRegistryException {
    if (statuses == null || statuses.length < 1)
        throw new IllegalArgumentException("At least one job status must be given.");
    List<Integer> ordinalStatuses = new ArrayList<>();
    for (Status status : statuses) {
        ordinalStatuses.add(status.ordinal());
    }
    TypedQuery<JpaJob> query = null;
    try {
        query = em.createNamedQuery("Job.statuses", JpaJob.class);
        query.setParameter("statuses", ordinalStatuses);
        List<JpaJob> jpaJobs = query.getResultList();
        for (JpaJob jpaJob : jpaJobs) {
            setJobUri(jpaJob);
        }
        return jpaJobs;
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) ArrayList(java.util.ArrayList) JpaJob(org.opencastproject.job.jpa.JpaJob) 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 15 with JpaJob

use of org.opencastproject.job.jpa.JpaJob in project opencast by opencast.

the class ServiceRegistryJpaImpl method removeParentlessJobs.

@Override
public void removeParentlessJobs(int lifetime) throws ServiceRegistryException {
    EntityManager em = null;
    EntityTransaction tx = null;
    Date d = DateUtils.addDays(new Date(), -lifetime);
    int count = 0;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        TypedQuery<JpaJob> query = em.createNamedQuery("Job.withoutParent", JpaJob.class);
        List<JpaJob> jobs = query.getResultList();
        tx.begin();
        for (JpaJob jpaJob : jobs) {
            Job job = jpaJob.toJob();
            if (job.getDateCreated().after(d))
                continue;
            // DO NOT DELETE workflow instances and operations!
            if (START_OPERATION.equals(job.getOperation()) || START_WORKFLOW.equals(job.getOperation()) || RESUME.equals(job.getOperation()))
                continue;
            if (job.getStatus().isTerminated()) {
                try {
                    removeJobs(Collections.singletonList(job.getId()));
                    logger.debug("Parentless job '{}' removed", job.getId());
                    count++;
                } catch (NotFoundException e) {
                    logger.debug("Parentless job '{} ' not found in database: {}", job.getId(), e);
                }
            }
        }
        tx.commit();
        if (count > 0)
            logger.info("Successfully removed {} parentless jobs", count);
        else
            logger.info("No parentless jobs found to remove", count);
    } finally {
        if (em != null)
            em.close();
    }
    return;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) 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) Date(java.util.Date)

Aggregations

JpaJob (org.opencastproject.job.jpa.JpaJob)29 NotFoundException (org.opencastproject.util.NotFoundException)13 PersistenceException (javax.persistence.PersistenceException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)11 EntityManager (javax.persistence.EntityManager)10 NoResultException (javax.persistence.NoResultException)10 RollbackException (javax.persistence.RollbackException)10 URISyntaxException (java.net.URISyntaxException)9 Test (org.junit.Test)9 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)9 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)9 ConfigurationException (org.osgi.service.cm.ConfigurationException)9 Job (org.opencastproject.job.api.Job)7 ArrayList (java.util.ArrayList)6 EntityTransaction (javax.persistence.EntityTransaction)6 JpaJob.fnToJob (org.opencastproject.job.jpa.JpaJob.fnToJob)6 HttpStatus (org.apache.http.HttpStatus)4 Status (org.opencastproject.job.api.Job.Status)4 Date (java.util.Date)3 Query (javax.persistence.Query)2