View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.rest;
23  
24  import org.opencastproject.job.api.Job;
25  import org.opencastproject.job.api.JobProducer;
26  import org.opencastproject.serviceregistry.api.ServiceRegistry;
27  import org.opencastproject.serviceregistry.api.ServiceRegistryException;
28  import org.opencastproject.serviceregistry.api.UndispatchableJobException;
29  import org.opencastproject.util.NotFoundException;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.ws.rs.FormParam;
35  import javax.ws.rs.HEAD;
36  import javax.ws.rs.POST;
37  import javax.ws.rs.Path;
38  import javax.ws.rs.WebApplicationException;
39  import javax.ws.rs.core.Response;
40  import javax.ws.rs.core.Response.Status;
41  
42  /**
43   * Base implementation for job producer REST endpoints.
44   */
45  public abstract class AbstractJobProducerEndpoint {
46  
47    /** The logger */
48    private static final Logger logger = LoggerFactory.getLogger(AbstractJobProducerEndpoint.class);
49  
50    /**
51     * @see org.opencastproject.job.api.JobProducer#acceptJob(org.opencastproject.job.api.Job)
52     */
53    @POST
54    @Path("/dispatch")
55    public Response dispatchJob(@FormParam("id") long jobId, @FormParam("operation") String jobOperation)
56            throws ServiceRegistryException {
57      final JobProducer service = getService();
58      if (service == null)
59        throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);
60  
61      // See if the service is ready to accept anything
62      if (!service.isReadyToAcceptJobs(jobOperation)) {
63        logger.debug("Service {} is not ready to accept jobs with operation {}", service, jobOperation);
64        return Response.status(Status.SERVICE_UNAVAILABLE).build();
65      }
66  
67      Job job;
68      try {
69        job = getServiceRegistry().getJob(jobId);
70      } catch (NotFoundException e) {
71        logger.warn("Unable to find dispatched job {}", jobId);
72        return Response.status(Status.NOT_FOUND).build();
73      }
74  
75      // See if the service has strong feelings about this particular job
76      try {
77        if (!service.isReadyToAccept(job)) {
78          logger.debug("Service {} temporarily refused to accept job {}", service, jobId);
79          return Response.status(Status.SERVICE_UNAVAILABLE).build();
80        }
81      } catch (UndispatchableJobException e) {
82        logger.warn("Service {} permanently refused to accept job {}", service, jobId);
83        return Response.status(Status.PRECONDITION_FAILED).build();
84      }
85  
86      service.acceptJob(job);
87      return Response.noContent().build();
88  
89    }
90  
91    @HEAD
92    @Path("/dispatch")
93    public Response checkHeartbeat() {
94      return Response.ok().build();
95    }
96  
97    /**
98     * Returns the job producer that is backing this REST endpoint.
99     *
100    * @return the job producer
101    */
102   public abstract JobProducer getService();
103 
104   /**
105    * Return the service registry.
106    */
107   public abstract ServiceRegistry getServiceRegistry();
108 
109 }