/
AbstractQueueProvider.cfc
351 lines (286 loc) · 9.51 KB
/
AbstractQueueProvider.cfc
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
component accessors="true" {
property name="interceptorService" inject="box:interceptorService";
property name="javaInstant" inject="java:java.time.Instant";
property name="async" inject="coldbox:asyncManager";
property name="log" inject="logbox:logger:{this}";
property name="wirebox" inject="wirebox";
property name="cbq" inject="@cbq";
property name="name";
property name="properties";
/**
* Persists a serialized job to the Queue Connection
*
* @queueName The queue name for the job.
* @payload The serialized job string.
* @delay The delay (in seconds) before working the job.
* @attempts The current attempt number.
*
* @return AbstractQueueProvider
*/
public any function push(
required string queueName,
required string payload,
numeric delay = 0,
numeric attempts = 0
) {
throw(
type = "MissingAbstractMethod",
message = "This is an abstract method and must be implemented in a subclass."
);
}
/**
* Starts a worker for a Worker Pool on this Queue Connection.
*
* @pool The Worker Pool that is working this Queue Connection.
*
* @return A function that when called will stop this worker.
*/
public function function startWorker( required WorkerPool pool ) {
throw(
type = "MissingAbstractMethod",
message = "This is an abstract method and must be implemented in a subclass."
);
}
/**
* Starts any background processes needed for the Worker Pool.
*
* @pool The Worker Pool that is working this Queue Connection.
*
* @return AbstractQueueProvider
*/
public any function listen( required WorkerPool pool ) {
throw(
type = "MissingAbstractMethod",
message = "This is an abstract method and must be implemented in a subclass."
);
}
public any function deserializeJob(
required string payload,
required any jobId,
numeric currentAttempt
) {
var config = deserializeJSON( arguments.payload );
if ( !variables.wirebox.containsInstance( config.mapping ) ) {
afterJobFailed( arguments.jobId );
throw( "Failed to find the [#config.mapping#] instance" );
}
var instance = variables.wirebox.getInstance( config.mapping );
instance.applyMemento( config );
instance.setId( arguments.jobId );
if ( !isNull( arguments.currentAttempt ) ) {
instance.setCurrentAttempt( arguments.currentAttempt );
}
if ( config.keyExists( "batchId" ) ) {
instance.withBatchId( config.batchId );
instance.setIsLifecycleJob( config.isLifecycleJob );
}
return instance;
}
/**
* Get the "available at" UNIX timestamp.
*
* @delay The delay, in seconds, to add to the current timestamp
* @return int
*/
public numeric function getCurrentUnixTimestamp( numeric delay = 0 ) {
return variables.javaInstant.now().getEpochSecond() + arguments.delay;
}
public any function marshalJob(
required AbstractJob job,
required WorkerPool pool,
function afterJobHook
) {
return variables.async
.newFuture( function() {
if ( variables.log.canDebug() ) {
variables.log.debug( "Marshaling job ###job.getId()#", job.getMemento() );
}
beforeJobRun( job );
if ( structKeyExists( job, "before" ) ) {
job.before();
}
variables.interceptorService.announce( "onCBQJobMarshalled", { "job" : job } );
if ( variables.log.canDebug() ) {
variables.log.debug( "Running job ###job.getId()#", job.getMemento() );
}
return job.handle();
}, arguments.pool.getExecutor() )
.orTimeout( getTimeoutForJob( arguments.job, arguments.pool ), "seconds" )
.then( function( result ) {
if ( job.getIsReleased() ) {
variables.log.debug( "Job [#job.getId()#] requested manual release." );
var jobMaxAttempts = getMaxAttemptsForJob( job, pool );
if ( jobMaxAttempts != 0 && job.getCurrentAttempt() >= jobMaxAttempts ) {
throw(
type = "cbq.MaxAttemptsReached",
message = "Job [#job.getId()#] requested manual release, but has reached its maximum attempts [#job.getCurrentAttempt()#]."
);
}
if ( jobMaxAttempts == 0 ) {
variables.log.debug( "Job ###job.getId()# has a maxAttempts of 0 and will always be released." );
}
variables.log.debug( "Releasing job ###job.getId()#" );
releaseJob( job, pool );
variables.log.debug( "Released job ###job.getId()#" );
return;
}
if ( variables.log.canDebug() ) {
variables.log.debug( "Job ###job.getId()# completed successfully." );
}
variables.interceptorService.announce(
"onCBQJobComplete",
{
"job" : job,
"result" : isNull( result ) ? javacast( "null", "" ) : result
}
);
if ( structKeyExists( job, "after" ) ) {
job.after();
}
afterJobRun( job );
ensureSuccessfulBatchJobIsRecorded( job );
dispatchNextJobInChain( job );
if ( !isNull( afterJobHook ) && ( isCustomFunction( afterJobHook ) || isClosure( afterJobHook ) ) ) {
afterJobHook( job );
}
} )
.onException( function( e ) {
// log failed job
if ( "java.util.concurrent.CompletionException" == e.getClass().getName() ) {
e = e.getCause();
}
if ( log.canError() ) {
log.error(
"Exception when running job: #e.message#",
{
"job" : job.getMemento(),
"exception" : e
}
);
}
variables.interceptorService.announce( "onCBQJobException", { "job" : job, "exception" : e } );
var jobMaxAttempts = getMaxAttemptsForJob( job, pool );
if ( jobMaxAttempts == 0 || job.getCurrentAttempt() < jobMaxAttempts ) {
if ( jobMaxAttempts == 0 ) {
variables.log.debug( "Job ###job.getId()# has a maxAttempts of 0 and will always be released." );
}
variables.log.debug( "Releasing job ###job.getId()#" );
releaseJob( job, pool );
variables.log.debug( "Released job ###job.getId()#" );
} else {
variables.log.debug( "Maximum attempts reached. Deleting job ###job.getId()#" );
if ( structKeyExists( job, "onFailure" ) ) {
invoke(
job,
"onFailure",
{ "excpetion" : e }
);
}
variables.interceptorService.announce( "onCBQJobFailed", { "job" : job, "exception" : e } );
afterJobFailed( job.getId(), job );
ensureFailedBatchJobIsRecorded( job, e );
variables.log.debug( "Deleted job ###job.getId()# after maximum failed attempts." );
}
if ( !isNull( afterJobHook ) && ( isCustomFunction( afterJobHook ) || isClosure( afterJobHook ) ) ) {
afterJobHook( job );
}
} );
}
private void function beforeJobRun( required AbstractJob job ) {
}
private void function afterJobRun( required AbstractJob job ) {
}
private void function afterJobFailed( required any id, AbstractJob job ) {
}
public void function releaseJob( required AbstractJob job, required WorkerPool pool ) {
push(
getQueueForJob( arguments.job, arguments.pool ),
serializeJSON( job.getMemento() ),
getBackoffForJob( arguments.job, arguments.pool ),
arguments.job.getCurrentAttempt() + 1
);
}
private string function getQueueForJob( required AbstractJob job, required WorkerPool pool ) {
if ( !isNull( arguments.job.getQueue() ) ) {
return arguments.job.getQueue();
}
if ( !isNull( arguments.pool ) ) {
return arguments.pool.getQueue();
}
return "default";
}
public numeric function getBackoffForJob( required AbstractJob job, required WorkerPool pool ) {
if ( !isNull( arguments.job.getBackoff() ) ) {
return arguments.job.getBackoff();
}
if ( !isNull( arguments.pool ) ) {
return arguments.pool.getBackoff();
}
return 0;
}
private numeric function getTimeoutForJob( required AbstractJob job, required WorkerPool pool ) {
if ( !isNull( arguments.job.getTimeout() ) ) {
return arguments.job.getTimeout();
}
if ( !isNull( arguments.pool ) ) {
return arguments.pool.getTimeout();
}
return 60;
}
private numeric function getMaxAttemptsForJob( required AbstractJob job, required WorkerPool pool ) {
if ( !isNull( arguments.job.getMaxAttempts() ) ) {
return arguments.job.getMaxAttempts();
}
if ( !isNull( arguments.pool ) ) {
return arguments.pool.getMaxAttempts();
}
return 1;
}
private void function dispatchNextJobInChain( required AbstractJob job ) {
var chain = arguments.job.getChained();
if ( chain.isEmpty() ) {
return;
}
var nextJobConfig = chain[ 1 ];
var nextJob = variables.cbq.job( nextJobConfig.mapping );
param nextJobConfig.properties = {};
if ( !isNull( nextJobConfig.queue ) ) {
nextJob.setQueue( nextJobConfig.queue );
}
if ( !isNull( nextJobConfig.connection ) ) {
nextJob.setConnection( nextJobConfig.connection );
}
if ( !isNull( nextJobConfig.timeout ) ) {
nextJob.setTimeout( nextJobConfig.timeout );
}
if ( !isNull( nextJobConfig.backoff ) ) {
nextJob.setBackoff( nextJobConfig.backoff );
}
if ( !isNull( nextJobConfig.maxAttempts ) ) {
nextJob.setMaxAttempts( nextJobConfig.maxAttempts );
}
nextJob.setProperties( nextJobConfig.properties );
if ( chain.len() >= 2 ) {
nextJob.setChained( chain.slice( 2 ) );
}
nextJob.dispatch();
}
private void function ensureSuccessfulBatchJobIsRecorded( required AbstractJob job ) {
if ( !arguments.job.isBatchJob() ) {
return;
}
if ( arguments.job.getIsLifecycleJob() ) {
return;
}
arguments.job.getBatch().recordSuccessfulJob( arguments.job.getId() );
}
private void function ensureFailedBatchJobIsRecorded( required AbstractJob job, required any error ) {
if ( !arguments.job.isBatchJob() ) {
return;
}
if ( arguments.job.getIsLifecycleJob() ) {
return;
}
arguments.job.getBatch().recordFailedJob( arguments.job.getId(), arguments.error );
}
}