1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45 public abstract class AbstractJobProducerEndpoint {
46
47
48 private static final Logger logger = LoggerFactory.getLogger(AbstractJobProducerEndpoint.class);
49
50
51
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
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
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
99
100
101
102 public abstract JobProducer getService();
103
104
105
106
107 public abstract ServiceRegistry getServiceRegistry();
108
109 }