Repository navigation
Expand file tree
/
Copy path_Job.java
More file actions
266 lines (229 loc) · 7.33 KB
/
Copy path_Job.java
File metadata and controls
266 lines (229 loc) · 7.33 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
package act.job;
import act.Act;
import act.app.App;
import act.app.event.AppEventId;
import act.event.AppEventListenerBase;
import act.route.DuplicateRouteMappingException;
import act.util.DestroyableBase;
import org.osgl.$;
import org.osgl.Osgl;
import org.osgl.exception.ConfigurationException;
import org.osgl.exception.NotAppliedException;
import org.osgl.exception.UnexpectedException;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.EventObject;
import java.util.List;
import java.util.UUID;
class _Job extends DestroyableBase implements Runnable {
private static final Logger logger = LogManager.get(_Job.class);
static final String BRIEF_VIEW = "id,oneTime,executed,trigger";
static final String DETAIL_VIEW = "id,oneTime,executed,trigger,worker";
private static final C.Set<Class<? extends UnexpectedException>> FATAL_EXCEPTIONS = C.set(
DuplicateRouteMappingException.class,
ConfigurationException.class
);
private String id;
private boolean oneTime;
private boolean executed;
private AppJobManager manager;
private JobTrigger trigger;
private $.Func0<?> worker;
private List<_Job> parallelJobs = C.newList();
private List<_Job> followingJobs = C.newList();
private List<_Job> precedenceJobs = C.newList();
_Job(AppJobManager manager) {
this(Act.cuid(), manager);
}
_Job(String id, AppJobManager manager) {
this(id, manager, null);
}
_Job(String id, AppJobManager manager, $.Func0<?> worker) {
this(id, manager, worker, true);
}
_Job(String id, AppJobManager manager, $.Func0<?> worker, boolean oneTime) {
this.id = id;
this.manager = $.NPE(manager);
this.worker = worker;
this.oneTime = oneTime;
}
@Override
protected void releaseResources() {
worker = null;
manager = null;
parallelJobs.clear();
followingJobs.clear();
precedenceJobs.clear();
super.releaseResources();
}
protected String brief() {
return S.fmt("job[%s]\none time job?%s\ntrigger:%s", id, oneTime, trigger);
}
@Override
public String toString() {
StringBuilder sb = S.builder(brief());
printSubJobs(parallelJobs, "parallel jobs", sb);
printSubJobs(followingJobs, "following jobs", sb);
printSubJobs(precedenceJobs, "precedence jobs", sb);
return sb.toString();
}
private static void printSubJobs(Collection<? extends _Job> subJobs, String label, StringBuilder sb) {
if (null != subJobs && !subJobs.isEmpty()) {
sb.append("\n").append(label);
for (_Job job : subJobs) {
sb.append("\n\t").append(job.brief());
}
}
}
_Job setOneTime() {
oneTime = true;
return this;
}
boolean isOneTime() {
return oneTime;
}
boolean done() {
return executed && oneTime;
}
final String id() {
return id;
}
final void trigger(JobTrigger trigger) {
E.NPE(trigger);
this.trigger = trigger;
}
final _Job addParallelJob(_Job thatJob) {
if (done()) {
manager.now(thatJob);
return this;
}
parallelJobs.add(thatJob);
if (isOneTime()) {
thatJob.setOneTime();
}
return this;
}
final _Job addFollowingJob(_Job thatJob) {
if (done()) {
thatJob.run();
return this;
}
followingJobs.add(thatJob);
if (isOneTime()) {
thatJob.setOneTime();
}
return this;
}
final _Job addPrecedenceJob(_Job thatJob) {
if (done()) {
thatJob.run();
return this;
}
precedenceJobs.add(thatJob);
if (isOneTime()) {
thatJob.setOneTime();
}
return this;
}
@Override
public void run() {
invokeParallelJobs();
runPrecedenceJobs();
try {
doJob();
} catch (RuntimeException e) {
if (FATAL_EXCEPTIONS.contains(e.getClass())) {
Act.shutdownApp(App.instance());
destroy();
if (App.instance().isMainThread()) {
throw e;
} else {
logger.fatal(e, "Fatal error executing job %s", id());
}
return;
}
// TODO inject Job Exception Handling mechanism here
logger.warn(e, "error executing job %s", id());
} finally {
if (!isDestroyed()) {
executed = true;
if (isOneTime()) {
App app = App.instance();
if (AppEventId.POST_START == app.currentState()) {
manager.removeJob(this);
} else {
app.eventBus().bind(AppEventId.POST_START, new AppEventListenerBase() {
@Override
public void on(EventObject event) throws Exception {
manager.removeJob(_Job.this);
}
});
}
}
}
}
runFollowingJobs();
}
protected void doJob() {
try {
if (null != worker) {
worker.apply();
}
} finally {
scheduleNextInvocation();
}
}
private void runPrecedenceJobs() {
for (_Job precedence : precedenceJobs) {
precedence.run();
}
}
private void runFollowingJobs() {
for (_Job post : followingJobs) {
post.run();
}
}
private void invokeParallelJobs() {
for (_Job alongWith : parallelJobs) {
manager.now(alongWith);
}
}
protected final AppJobManager manager() {
return manager;
}
protected void scheduleNextInvocation() {
if (null != trigger) trigger.scheduleFollowingCalls(manager(), this);
}
private static _Job of(String jobId, final Runnable runnable, AppJobManager manager, boolean oneTime) {
return new _Job(jobId, manager, new $.F0() {
@Override
public Object apply() throws NotAppliedException, $.Break {
runnable.run();
return null;
}
}, oneTime);
}
private static _Job of(final Runnable runnable, AppJobManager manager, boolean oneTime) {
return of(S.uuid(), runnable, manager, oneTime);
}
static _Job once(final Runnable runnable, AppJobManager manager) {
return of(runnable, manager, true);
}
static _Job once(String jobId, final Runnable runnable, AppJobManager manager) {
return of(jobId, runnable, manager, true);
}
static _Job multipleTimes(final Runnable runnable, AppJobManager manager) {
return of(runnable, manager, false);
}
static _Job multipleTimes(String jobId, final Runnable runnable, AppJobManager manager) {
return of(jobId, runnable, manager, false);
}
private static String uuid() {
return UUID.randomUUID().toString();
}
}