-
Notifications
You must be signed in to change notification settings - Fork 682
/
JobsPlugin.java
280 lines (260 loc) · 11.6 KB
/
JobsPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package play.jobs;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import play.Logger;
import play.Play;
import play.PlayPlugin;
import play.exceptions.PlayException;
import play.exceptions.UnexpectedException;
import play.libs.CronExpression;
import play.libs.Expression;
import play.libs.Time;
import play.mvc.Http.Request;
import play.utils.Java;
import play.utils.PThreadFactory;
public class JobsPlugin extends PlayPlugin {
public static ScheduledThreadPoolExecutor executor;
public static List<Job> scheduledJobs = new ArrayList<>();
private static final ThreadLocal<List<Callable<?>>> afterInvocationActions = new ThreadLocal<>();
@Override
public String getStatus() {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
if (executor == null) {
out.println("Jobs execution pool:");
out.println("~~~~~~~~~~~~~~~~~~~");
out.println("(not yet started)");
return sw.toString();
}
out.println("Jobs execution pool:");
out.println("~~~~~~~~~~~~~~~~~~~");
out.println("Pool size: " + executor.getPoolSize());
out.println("Active count: " + executor.getActiveCount());
out.println("Scheduled task count: " + executor.getTaskCount());
out.println("Queue size: " + executor.getQueue().size());
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
if (!scheduledJobs.isEmpty()) {
out.println();
out.println("Scheduled jobs (" + scheduledJobs.size() + "):");
out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
for (Job job : scheduledJobs) {
out.print(job);
if (job.getClass().isAnnotationPresent(OnApplicationStart.class)
&& !(job.getClass().isAnnotationPresent(On.class) || job.getClass().isAnnotationPresent(Every.class))) {
OnApplicationStart appStartAnnotation = job.getClass().getAnnotation(OnApplicationStart.class);
out.print(" run at application start" + (appStartAnnotation.async() ? " (async)" : "") + ".");
}
if (job.getClass().isAnnotationPresent(On.class)) {
String cron = job.getClass().getAnnotation(On.class).value();
if (cron != null && cron.startsWith("cron.")) {
cron = Play.configuration.getProperty(cron);
}
out.print(" run with cron expression " + cron + ".");
}
if (job.getClass().isAnnotationPresent(Every.class)) {
out.print(" run every " + job.getClass().getAnnotation(Every.class).value() + ".");
}
if (job.lastRun > 0) {
out.print(" (last run at " + df.format(new Date(job.lastRun)));
if (job.wasError) {
out.print(" with error)");
} else {
out.print(")");
}
} else {
out.print(" (has never run)");
}
out.println();
}
}
if (!executor.getQueue().isEmpty()) {
out.println();
out.println("Waiting jobs:");
out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
ScheduledFuture[] q = executor.getQueue().toArray(new ScheduledFuture[executor.getQueue().size()]);
for (ScheduledFuture task : q) {
out.println(Java.extractUnderlyingCallable((FutureTask<?>) task) + " will run in " + task.getDelay(TimeUnit.SECONDS)
+ " seconds");
}
}
return sw.toString();
}
@Override
public void afterApplicationStart() {
List<Class<?>> jobs = new ArrayList<>();
for (Class clazz : Play.classloader.getAllClasses()) {
if (Job.class.isAssignableFrom(clazz)) {
jobs.add(clazz);
}
}
for (Class<?> clazz : jobs) {
// @OnApplicationStart
if (clazz.isAnnotationPresent(OnApplicationStart.class)) {
// check if we're going to run the job sync or async
OnApplicationStart appStartAnnotation = clazz.getAnnotation(OnApplicationStart.class);
if (!appStartAnnotation.async()) {
// run job sync
try {
Job<?> job = createJob(clazz);
job.run();
if (job.wasError) {
if (job.lastException != null) {
throw job.lastException;
}
throw new RuntimeException("@OnApplicationStart Job has failed");
}
} catch (InstantiationException | IllegalAccessException e) {
throw new UnexpectedException("Job could not be instantiated", e);
} catch (Throwable ex) {
if (ex instanceof PlayException) {
throw (PlayException) ex;
}
throw new UnexpectedException(ex);
}
} else {
// run job async
try {
Job<?> job = createJob(clazz);
// start running job now in the background
@SuppressWarnings("unchecked")
Callable<Job> callable = (Callable<Job>) job;
executor.submit(callable);
} catch (InstantiationException | IllegalAccessException ex) {
throw new UnexpectedException("Cannot instantiate Job " + clazz.getName(), ex);
}
}
}
// @On
if (clazz.isAnnotationPresent(On.class)) {
try {
Job<?> job = createJob(clazz);
scheduleForCRON(job);
} catch (InstantiationException | IllegalAccessException ex) {
throw new UnexpectedException("Cannot instantiate Job " + clazz.getName(), ex);
}
}
// @Every
if (clazz.isAnnotationPresent(Every.class)) {
try {
Job job = createJob(clazz);
String value = job.getClass().getAnnotation(Every.class).value();
if (value.startsWith("cron.")) {
value = Play.configuration.getProperty(value);
}
value = Expression.evaluate(value, value).toString();
if (!"never".equalsIgnoreCase(value)) {
executor.scheduleWithFixedDelay(job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
}
} catch (InstantiationException | IllegalAccessException ex) {
throw new UnexpectedException("Cannot instantiate Job " + clazz.getName(), ex);
}
}
}
}
private Job<?> createJob(Class<?> clazz) throws InstantiationException, IllegalAccessException {
Job<?> job = (Job<?>) clazz.newInstance();
scheduledJobs.add(job);
return job;
}
@Override
public void onApplicationStart() {
int core = Integer.parseInt(Play.configuration.getProperty("play.jobs.pool", "10"));
executor = new ScheduledThreadPoolExecutor(core, new PThreadFactory("jobs"), new ThreadPoolExecutor.AbortPolicy());
scheduledJobs.clear();
}
public static <V> void scheduleForCRON(Job<V> job) {
if (!job.getClass().isAnnotationPresent(On.class)) {
return;
}
String cron = job.getClass().getAnnotation(On.class).value();
if (cron.startsWith("cron.")) {
cron = Play.configuration.getProperty(cron);
}
cron = Expression.evaluate(cron, cron).toString();
if (cron == null || cron.isEmpty() || "never".equalsIgnoreCase(cron)) {
Logger.info("Skipping job %s, cron expression is not defined", job.getClass().getName());
return;
}
try {
Date now = new Date();
cron = Expression.evaluate(cron, cron).toString();
CronExpression cronExp = new CronExpression(cron);
Date nextDate = cronExp.getNextValidTimeAfter(now);
if (nextDate == null) {
Logger.warn("The cron expression for job %s doesn't have any match in the future, will never be executed",
job.getClass().getName());
return;
}
if (nextDate.equals(job.nextPlannedExecution)) {
// Bug #13: avoid running the job twice for the same time
// (happens when we end up running the job a few minutes before
// the planned time)
Date nextInvalid = cronExp.getNextInvalidTimeAfter(nextDate);
nextDate = cronExp.getNextValidTimeAfter(nextInvalid);
}
job.nextPlannedExecution = nextDate;
executor.schedule((Callable<V>) job, nextDate.getTime() - now.getTime(), TimeUnit.MILLISECONDS);
job.executor = executor;
} catch (Exception ex) {
throw new UnexpectedException(ex);
}
}
@Override
public void onApplicationStop() {
List<Class> jobs = Play.classloader.getAssignableClasses(Job.class);
for (Class clazz : jobs) {
// @OnApplicationStop
if (clazz.isAnnotationPresent(OnApplicationStop.class)) {
try {
Job<?> job = createJob(clazz);
job.run();
if (job.wasError) {
if (job.lastException != null) {
throw job.lastException;
}
throw new RuntimeException("@OnApplicationStop Job has failed");
}
} catch (InstantiationException | IllegalAccessException e) {
throw new UnexpectedException("Job could not be instantiated", e);
} catch (Throwable ex) {
if (ex instanceof PlayException) {
throw (PlayException) ex;
}
throw new UnexpectedException(ex);
}
}
}
executor.shutdownNow();
executor.getQueue().clear();
}
@Override
public void beforeInvocation() {
afterInvocationActions.set(new LinkedList<Callable<?>>());
}
@Override
public void afterInvocation() {
List<Callable<?>> currentActions = afterInvocationActions.get();
afterInvocationActions.set(null);
for (Callable<?> callable : currentActions) {
executor.submit(callable);
}
}
// default visibility, because we want to use this only from Job.java
static void addAfterRequestAction(Callable<?> c) {
if (Request.current() == null) {
throw new IllegalStateException("After request actions can be added only from threads that serve requests!");
}
afterInvocationActions.get().add(c);
}
}