Search in sources :

Example 11 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class IncidentServiceEndpoint method postIncident.

@POST
@Produces(MediaType.APPLICATION_XML)
@Path("/")
@RestQuery(name = "postincident", description = "Creates a new job incident and returns it as XML", returnDescription = "Returns the created job incident as XML", restParameters = { @RestParameter(name = "job", isRequired = true, description = "The job on where to create the incident", type = Type.TEXT), @RestParameter(name = "date", isRequired = true, description = "The incident creation date", type = Type.STRING), @RestParameter(name = "code", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "severity", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "details", isRequired = false, description = "The incident details", type = Type.TEXT), @RestParameter(name = "params", isRequired = false, description = "The incident parameters", type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "New job incident has been created"), @RestResponse(responseCode = SC_BAD_REQUEST, description = "Unable to parse the one of the form params"), @RestResponse(responseCode = SC_CONFLICT, description = "No job incident related job exists") })
public Response postIncident(@FormParam("job") String jobXml, @FormParam("date") String date, @FormParam("code") String code, @FormParam("severity") String severityString, @FormParam("details") String details, @FormParam("params") LocalHashMap params) {
    Job job;
    Date timestamp;
    Severity severity;
    Map<String, String> map = new HashMap<String, String>();
    List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
    try {
        job = JobParser.parseJob(jobXml);
        timestamp = new Date(DateTimeSupport.fromUTC(date));
        severity = Severity.valueOf(severityString);
        if (params != null)
            map = params.getMap();
        if (StringUtils.isNotBlank(details)) {
            final JSONArray array = (JSONArray) JSONValue.parse(details);
            for (int i = 0; i < array.size(); i++) {
                JSONObject tuple = (JSONObject) array.get(i);
                list.add(Tuple.tuple((String) tuple.get("title"), (String) tuple.get("content")));
            }
        }
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
    try {
        Incident incident = svc.storeIncident(job, timestamp, code, severity, map, list);
        String uri = UrlSupport.concat(serverUrl, serviceUrl, Long.toString(incident.getId()), ".xml");
        return Response.created(new URI(uri)).entity(new JaxbIncident(incident)).build();
    } catch (IllegalStateException e) {
        return Response.status(Status.CONFLICT).build();
    } catch (Exception e) {
        logger.warn("Unable to post incident for job {}: {}", job.getId(), e);
        throw new WebApplicationException(INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) LocalHashMap(org.opencastproject.util.LocalHashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Severity(org.opencastproject.job.api.Incident.Severity) URI(java.net.URI) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) NotFoundException(org.opencastproject.util.NotFoundException) JSONObject(org.json.simple.JSONObject) JaxbIncident(org.opencastproject.job.api.JaxbIncident) JaxbIncident(org.opencastproject.job.api.JaxbIncident) Incident(org.opencastproject.job.api.Incident) Job(org.opencastproject.job.api.Job) Tuple(org.opencastproject.util.data.Tuple) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 12 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class OsgiIncidentServiceTest method setUp.

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    final EntityManagerFactory emf = PersistenceUtil.newTestEntityManagerFactory(AbstractIncidentService.PERSISTENCE_UNIT_NAME);
    penv = PersistenceEnvs.persistenceEnvironment(emf);
    // Mock up a job
    Job job = createNiceMock(Job.class);
    expect(job.getProcessingHost()).andStubReturn(PROCESSING_HOST);
    expect(job.getJobType()).andStubReturn(JOB_TYPE);
    expect(job.getCreator()).andStubReturn("creator");
    expect(job.getOrganization()).andStubReturn("organization");
    replay(job);
    // Mock up a service registry
    final ServiceRegistry serviceRegistry = createNiceMock(ServiceRegistry.class);
    expect(serviceRegistry.getJob(EasyMock.anyLong())).andAnswer(new IAnswer<Job>() {

        @Override
        public Job answer() throws Throwable {
            final Long jobId = (Long) EasyMock.getCurrentArguments()[0];
            return jobs.get(jobId);
        }
    }).anyTimes();
    replay(serviceRegistry);
    // Mock up a workflow service
    final WorkflowService workflowService = createNiceMock(WorkflowService.class);
    replay(workflowService);
    incidentService = new AbstractIncidentService() {

        @Override
        protected ServiceRegistry getServiceRegistry() {
            return serviceRegistry;
        }

        @Override
        protected WorkflowService getWorkflowService() {
            return workflowService;
        }

        @Override
        protected PersistenceEnv getPenv() {
            return PersistenceEnvs.persistenceEnvironment(emf);
        }
    };
    incidents = new Incidents(serviceRegistry, incidentService);
}
Also used : IAnswer(org.easymock.IAnswer) PersistenceEnv(org.opencastproject.util.persistence.PersistenceEnv) WorkflowService(org.opencastproject.workflow.api.WorkflowService) EntityManagerFactory(javax.persistence.EntityManagerFactory) Incidents(org.opencastproject.serviceregistry.api.Incidents) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Job(org.opencastproject.job.api.Job) Before(org.junit.Before)

Example 13 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class ServiceRegistryJpaImpl method deleteChildJobs.

private void deleteChildJobs(EntityManager em, EntityTransaction tx, long jobId) throws ServiceRegistryException {
    List<Job> childJobs = getChildJobs(jobId);
    if (childJobs.isEmpty()) {
        logger.debug("No child jobs of job '{}' found to delete.", jobId);
        return;
    }
    logger.debug("Start deleting child jobs of job '{}'", jobId);
    try {
        for (int i = childJobs.size() - 1; i >= 0; i--) {
            Job job = childJobs.get(i);
            JpaJob jobToDelete = em.find(JpaJob.class, job.getId());
            em.remove(jobToDelete);
            logger.debug("Job '{}' deleted", job.getId());
        }
        logger.debug("Deleted all child jobs of job '{}'", jobId);
    } catch (Exception e) {
        logger.error("Unable to remove child jobs from {}: {}", jobId, e);
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    }
}
Also used : JpaJob.fnToJob(org.opencastproject.job.jpa.JpaJob.fnToJob) Job(org.opencastproject.job.api.Job) JpaJob(org.opencastproject.job.jpa.JpaJob) 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 14 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class ServiceRegistryJpaImpl method getActiveJobs.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getActiveJobs()
 */
@Override
public List<Job> getActiveJobs() throws ServiceRegistryException {
    List<Status> statuses = new ArrayList<Status>();
    for (Status status : Status.values()) {
        if (status.isActive())
            statuses.add(status);
    }
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        List<JpaJob> jpaJobs = getJobsByStatus(em, statuses.toArray(new Status[statuses.size()]));
        List<Job> jobs = new ArrayList<Job>(jpaJobs.size());
        for (JpaJob jpaJob : jpaJobs) {
            jobs.add(jpaJob.toJob());
        }
        return jobs;
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : HttpStatus(org.apache.http.HttpStatus) Status(org.opencastproject.job.api.Job.Status) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) 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) 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 Job

use of org.opencastproject.job.api.Job 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)

Aggregations

Job (org.opencastproject.job.api.Job)282 MediaPackage (org.opencastproject.mediapackage.MediaPackage)89 ArrayList (java.util.ArrayList)82 Test (org.junit.Test)82 URI (java.net.URI)68 NotFoundException (org.opencastproject.util.NotFoundException)58 Track (org.opencastproject.mediapackage.Track)56 JaxbJob (org.opencastproject.job.api.JaxbJob)52 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)51 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)50 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)49 IOException (java.io.IOException)45 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)42 HttpPost (org.apache.http.client.methods.HttpPost)33 Path (javax.ws.rs.Path)31 Produces (javax.ws.rs.Produces)30 JobBarrier (org.opencastproject.job.api.JobBarrier)30 RestQuery (org.opencastproject.util.doc.rest.RestQuery)30 POST (javax.ws.rs.POST)29 HttpResponse (org.apache.http.HttpResponse)29