Repository navigation
Expand file tree
/
Copy pathJobTrigger.java
More file actions
403 lines (336 loc) · 12.5 KB
/
Copy pathJobTrigger.java
File metadata and controls
403 lines (336 loc) · 12.5 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package act.job;
import act.app.App;
import act.app.event.AppEventId;
import act.conf.AppConfig;
import act.event.AppEventListenerBase;
import fc.cron.CronExpression;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.E;
import org.osgl.util.S;
import org.rythmengine.utils.Time;
import java.util.EventObject;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static act.app.event.AppEventId.START;
import static act.app.event.AppEventId.STOP;
import static act.job.AppJobManager.appEventJobId;
abstract class JobTrigger {
protected static Logger logger = L.get(App.class);
@Override
public String toString() {
return getClass().getSimpleName();
}
final void register(_Job job, AppJobManager manager) {
job.trigger(this);
manager.addJob(job);
schedule(manager, job);
}
void scheduleFollowingCalls(AppJobManager manager, _Job job) {}
void schedule(AppJobManager manager, _Job job) {}
static JobTrigger of(AppConfig config, Cron anno) {
String v = anno.value();
if (v.startsWith("cron.")) {
v = (String) config.get(v);
} else if (v.startsWith("${") && v.endsWith("}")) {
v = v.substring(2, v.length() - 1);
v = (String) config.get(v);
}
if (S.blank(v)) {
throw E.invalidConfiguration("Cannot find configuration for cron: %s", anno.value());
}
return cron(v);
}
static JobTrigger of(AppConfig config, OnAppStart anno) {
if (anno.async()) {
return alongWith(START);
} else {
return after(START);
}
}
static JobTrigger of(AppConfig config, OnAppStop anno) {
if (anno.async()) {
return alongWith(STOP);
} else {
return before(STOP);
}
}
static JobTrigger of(AppConfig config, FixedDelay anno) {
String delay = anno.value();
if (delay.startsWith("delay.")) {
delay = (String) config.get(delay);
} else if (delay.startsWith("${") && delay.endsWith("}")) {
delay = delay.substring(2, delay.length() - 1);
delay = (String) config.get(delay);
}
if (S.blank(delay)) {
throw E.invalidConfiguration("Cannot find configuration for delay: %s", anno.value());
}
return fixedDelay(delay);
}
static JobTrigger of(AppConfig config, Every anno) {
String duration = anno.value();
if (duration.startsWith("every.")) {
duration = (String) config.get(duration);
} else if (duration.startsWith("${") && duration.endsWith("}")) {
duration = duration.substring(2, duration.length() - 1);
duration = (String) config.get(duration);
}
if (S.blank(duration)) {
throw E.invalidConfiguration("Cannot find configuration for duration: %s", anno.value());
}
return every(duration);
}
static JobTrigger of(AppConfig config, AlongWith anno) {
String id = anno.value();
E.illegalArgumentIf(S.blank(id), "associate job ID cannot be empty");
return new _AlongWith(id);
}
static JobTrigger of(AppConfig config, InvokeAfter anno) {
String id = anno.value();
E.illegalArgumentIf(S.blank(id), "associate job ID cannot be empty");
return new _After(id);
}
static JobTrigger of(AppConfig config, InvokeBefore anno) {
String id = anno.value();
E.illegalArgumentIf(S.blank(id), "associate job ID cannot be empty");
return new _Before(id);
}
static JobTrigger cron(String expression) {
return new _Cron(expression);
}
static JobTrigger fixedDelay(String duration) {
return new _FixedDelay(duration);
}
static JobTrigger fixedDelay(long seconds) {
return new _FixedDelay(seconds);
}
static JobTrigger fixedDelay(long interval, TimeUnit timeUnit) {
return new _FixedDelay(timeUnit.toSeconds(interval));
}
static JobTrigger every(String duration) {
return new _Every(duration);
}
static JobTrigger every(long seconds) {
return new _Every(seconds, TimeUnit.SECONDS);
}
static JobTrigger every(long duration, TimeUnit timeUnit) {
return new _Every(duration, timeUnit);
}
static JobTrigger onAppStart(boolean async) {
return async ? alongWith(START) : after(START);
}
static JobTrigger onAppStop(boolean async) {
return async ? alongWith(STOP) : before(STOP);
}
static JobTrigger onAppEvent(AppEventId eventId, boolean async) {
return async ? alongWith(eventId) : after(eventId);
}
static JobTrigger delayForSeconds(long seconds) {
return new _FixedDelay(seconds);
}
static JobTrigger alongWith(String jobId) {
return new _AlongWith(jobId);
}
static JobTrigger alongWith(AppEventId appEvent) {
return new _AlongWith(appEventJobId(appEvent));
}
static JobTrigger before(String jobId) {
return new _Before(jobId);
}
static JobTrigger before(AppEventId appEvent) {
return before(appEventJobId(appEvent));
}
static JobTrigger after(String jobId) {
return new _After(jobId);
}
static JobTrigger after(AppEventId appEvent) {
return after(appEventJobId(appEvent));
}
static class _Cron extends JobTrigger {
private CronExpression cronExpr;
_Cron(String expression) {
cronExpr = new CronExpression(expression);
}
@Override
public String toString() {
return S.builder("cron :").append(cronExpr.toString()).toString();
}
@Override
void schedule(final AppJobManager manager, final _Job job) {
App app = manager.app();
if (AppEventId.POST_START != app.currentState()) {
app.eventBus().bindAsync(AppEventId.POST_START, new AppEventListenerBase() {
@Override
public void on(EventObject event) throws Exception {
delayedSchedule(manager, job);
}
});
} else {
delayedSchedule(manager, job);
}
}
private void delayedSchedule(AppJobManager manager, _Job job) {
DateTime now = DateTime.now();
// add one seconds to prevent the next time be the current time (now)
DateTime next = cronExpr.nextTimeAfter(now.plusSeconds(1));
Seconds seconds = Seconds.secondsBetween(now, next);
manager.executor().schedule(job, seconds.getSeconds(), TimeUnit.SECONDS);
}
@Override
void scheduleFollowingCalls(AppJobManager manager, _Job job) {
schedule(manager, job);
}
}
private abstract static class _Periodical extends JobTrigger {
protected long seconds;
_Periodical(String duration) {
E.illegalArgumentIf(S.blank(duration), "delay duration shall not be empty");
seconds = Time.parseDuration(duration);
E.illegalArgumentIf(seconds < 1, "delay duration shall not be zero or negative number");
}
_Periodical(long seconds) {
E.illegalArgumentIf(seconds < 1, "delay duration cannot be zero or negative");
this.seconds = seconds;
}
}
private static class _FixedDelay extends _Periodical {
_FixedDelay(String duration) {
super(duration);
}
_FixedDelay(long seconds) {
super(seconds);
}
@Override
public String toString() {
return S.builder("fixed delay of ").append(seconds).append(" seconds").toString();
}
@Override
void schedule(final AppJobManager manager, final _Job job) {
App app = manager.app();
if (AppEventId.POST_START != app.currentState()) {
app.eventBus().bindAsync(AppEventId.POST_START, new AppEventListenerBase() {
@Override
public void on(EventObject event) throws Exception {
delayedSchedule(manager, job);
}
});
} else {
delayedSchedule(manager, job);
}
}
private void delayedSchedule(AppJobManager manager, _Job job) {
ScheduledThreadPoolExecutor executor = manager.executor();
executor.scheduleWithFixedDelay(job, seconds, seconds, TimeUnit.SECONDS);
}
}
private static class _Every extends _Periodical {
_Every(String duration) {
super(duration);
}
_Every(long duration, TimeUnit timeUnit) {
super(timeUnit.toSeconds(duration));
}
@Override
public String toString() {
return S.builder("every ").append(seconds).append(" seconds").toString();
}
@Override
void schedule(final AppJobManager manager, final _Job job) {
App app = manager.app();
if (AppEventId.POST_START != app.currentState()) {
app.eventBus().bindAsync(AppEventId.POST_START, new AppEventListenerBase() {
@Override
public void on(EventObject event) throws Exception {
delayedSchedule(manager, job);
}
});
} else {
delayedSchedule(manager, job);
}
}
private void delayedSchedule(AppJobManager manager, _Job job) {
ScheduledThreadPoolExecutor executor = manager.executor();
executor.scheduleAtFixedRate(job, seconds, seconds, TimeUnit.SECONDS);
}
}
private abstract static class _AssociatedTo extends JobTrigger {
protected String id;
_AssociatedTo(String id) {
E.illegalArgumentIf(S.blank(id), "associate job ID expected");
this.id = id;
}
@Override
void schedule(AppJobManager manager, _Job job) {
if (null == id) {
logger.warn("Failed to register job because target job not found: %s. Will try again after app started", id);
scheduleDelayedRegister(manager, job);
} else {
_Job associateTarget = manager.jobById(id);
associate(job, associateTarget);
}
}
private void scheduleDelayedRegister(final AppJobManager manager, final _Job job) {
final String id = delayedRegisterJobId(job);
before(START).register(new _Job(id, manager, new $.F0<Void>() {
@Override
public Void apply() throws NotAppliedException, $.Break {
_Job associateTo = manager.jobById(id);
if (null == id) {
throw E.invalidConfiguration("dependency job not found: %s", id);
}
associate(job, associateTo);
return null;
}
}), manager);
}
private String delayedRegisterJobId(_Job job) {
StringBuilder sb = S.builder("delayed_association_register-").append(job.id()).append("-to-").append(id);
return sb.toString();
}
abstract void associate(_Job theJob, _Job toJob);
}
private static class _AlongWith extends _AssociatedTo {
_AlongWith(String id) {
super(id);
}
@Override
public String toString() {
return S.builder("along with ").append(id).toString();
}
@Override
void associate(_Job theJob, _Job toJob) {
toJob.addParallelJob(theJob);
}
}
private static class _Before extends _AssociatedTo {
_Before(String id) {
super(id);
}
@Override
public String toString() {
return S.builder("before ").append(id).toString();
}
@Override
void associate(_Job theJob, _Job toJob) {
toJob.addPrecedenceJob(theJob);
}
}
private static class _After extends _AssociatedTo {
_After(String id) {
super(id);
}
@Override
public String toString() {
return S.builder("after ").append(id).toString();
}
@Override
void associate(_Job theJob, _Job toJob) {
toJob.addFollowingJob(theJob);
}
}
}