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
63 if (!service.isReadyToAcceptJobs(jobOperation)) {
64 logger.debug("Service {} is not ready to accept jobs with operation {}", service, jobOperation);
65 return Response.status(Status.SERVICE_UNAVAILABLE).build();
66 }
67
68 Job job;
69 try {
70 job = getServiceRegistry().getJob(jobId);
71 } catch (NotFoundException e) {
72 logger.warn("Unable to find dispatched job {}", jobId);
73 return Response.status(Status.NOT_FOUND).build();
74 }
75
76
77 try {
78 if (!service.isReadyToAccept(job)) {
79 logger.debug("Service {} temporarily refused to accept job {}", service, jobId);
80 return Response.status(Status.SERVICE_UNAVAILABLE).build();
81 }
82 } catch (UndispatchableJobException e) {
83 logger.warn("Service {} permanently refused to accept job {}", service, jobId);
84 return Response.status(Status.PRECONDITION_FAILED).build();
85 }
86
87 service.acceptJob(job);
88 return Response.noContent().build();
89
90 }
91
92 @HEAD
93 @Path("/dispatch")
94 public Response checkHeartbeat() {
95 return Response.ok().build();
96 }
97
98
99
100
101
102
103 public abstract JobProducer getService();
104
105
106
107
108 public abstract ServiceRegistry getServiceRegistry();
109
110 }