-
Notifications
You must be signed in to change notification settings - Fork 1
/
fatus.js
executable file
·542 lines (492 loc) · 15.6 KB
/
fatus.js
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
'use strict';
/** constants */
const MODULE_NAME = 'Fatusjs'
const FATUS_QUEUE_NAME = process.env.FATUS_QUEUE_NAME || 'fatusjs-queue-test';
const FATUS_QUEUE_FAIL_NAME = process.env.FATUS_QUEUE_FAIL_NAME || 'fatusjs-queue-fail';
const FATUS_MAX_WORKER = process.env.FATUS_MAX_WORKER || 3;
const FATUS_EQ_RETRY_TIME = process.env.FATUS_EQ_RETRY_TIME || 4000; // millisec
const FATUS_WRK_RETRY_ATTEMP = process.env.FATUS_WRK_RETRY_ATTEMP || 2;
const FATUS_WRK_STACK_TRSHLD = process.env.FATUS_WRK_STACK_TRSHLD || 10;
const FATUS_JOB_RESERV_TIME = process.env.FATUS_JOB_RESERV_TIME || 600000; // millisec
const FATUS_MAX_FAIL = process.env.FATUS_MAX_FAIL || 10; // max num of fails
const FATUS_MAX_FAIL_TOTAL = process.env.FATUS_MAX_FAIL_TOTAL || 1000; // total number of fails before be suspended
/** inner refs */
const AzureQueue = require('./azurequeue');
const FatusWorker = require('./worker');
const FatusPriorityWorker = require('./priorityworker');
const FatusFailWorker = require('./failworker');
const FatusCollectorWorker = require('./collectorworker');
const MessageJob = require('./messagejob');
const ServeQueue = require('./servequeue');
/** outher refs */
const assert = require('assert');
const EventEmitter = require('events');
const shortid = require('shortid');
const util = require('util');
/** singleton */
let singleton = Symbol();
let singletonEnforcer = Symbol();
/**
* Lightweight azure queue job processor
*/
class Fatusjs extends EventEmitter{
/**
* constructor for the fatus queue
*/
constructor(enforcer){
// singleton enforcer pattern
if(enforcer != singletonEnforcer){
throw "Cannot construct singleton";
}
super();
this.FatusWorker = FatusWorker; // put here to manage outside
this.MessageJob = MessageJob; // put here to manage outside
this.queueMgr = new AzureQueue();
this.queueMgr.createQueue(
FATUS_QUEUE_NAME,
function onCreate(err,res){
if(!err){
console.log(MODULE_NAME + ': init-queue created or found with name %s',FATUS_QUEUE_NAME);
}else{
console.error(err);
}
});
this.queueMgr.createQueue(
FATUS_QUEUE_FAIL_NAME,
function onCreate(err,res){
if(!err){
console.log(MODULE_NAME + ': init-queue created or found with name %s',FATUS_QUEUE_FAIL_NAME);
}else{
console.error(err);
}
});
console.log(MODULE_NAME + ': init worker pool with max %s ',FATUS_MAX_WORKER);
this.workerPool = [];
this.priorityWorkerPool = [];
this.failWorkerPool = [];
this.collectorPool = [];
this.registerMonitor();
this.registerFailover();
this.registerAutoWorker();
this.registerPrint();
}
/**
* get the instance
* @returns {*}
*/
static get instance(){
if(!this[singleton]) {
this[singleton] = new Fatusjs(singletonEnforcer);
}
return this[singleton];
}
/**
* add a worker to the worker pool
*/
addWorker(){
if(this.workerPool.length<FATUS_MAX_WORKER){
let worker = new FatusWorker(this);
this.initWorker(worker);
this.workerPool.push(worker);
console.log(MODULE_NAME + ': new worker created %s',worker.name);
worker.run();
}else{
console.log(MODULE_NAME + ': workerpool full, skip adding');
}
}
/**
* add a worker to the pool
*/
addPriorityWorker(){
if(this.priorityWorkerPool.length<10) {
console.log(MODULE_NAME + ': adding priority worker');
let worker = new FatusPriorityWorker(this);
this.initWorker(worker);
worker.setStackProtection(3);
this.priorityWorkerPool.push(worker);
worker.run();
}
}
/**
* add a worker for the failed jobs pool
*/
addFailWorker(){
if(this.failWorkerPool.length<5) {
let worker = new FatusFailWorker(this);
this.initWorker(worker);
worker.setMaxFails(FATUS_MAX_FAIL_TOTAL);
this.failWorkerPool.push(worker);
worker.run();
}
}
/**
* add a collector to the pool
*/
addCollector(){
if(this.collectorPool.length<2){
let worker = new FatusCollectorWorker(this);
this.initWorker(worker);
worker.setStackProtection(3);
this.collectorPool.push(worker);
worker.run();
}
}
/**
* init all values for a worker
* @param worker
*/
initWorker(worker) {
worker.setRetryTime(FATUS_EQ_RETRY_TIME);
worker.setMaxAttempts(FATUS_WRK_RETRY_ATTEMP);
worker.setStackProtection(FATUS_WRK_STACK_TRSHLD);
worker.setReservationTime(FATUS_JOB_RESERV_TIME);
worker.setMaxFails(FATUS_MAX_FAIL);
}
/**
* remove a worker
* @param name
*/
removeWorker(name){
let indx = 0;
// try remove from worker
for(let w of this.workerPool){
if(w.name === name){
this.workerPool.splice(indx,1);
return;
}
indx ++;
}
indx = 0;
// try remove from priority worker
for(let w of this.priorityWorkerPool){
if(w.name === name){
this.priorityWorkerPool.splice(indx,1);
return;
}
indx ++;
}
indx = 0;
// try remove from fail worker
for(let w of this.failWorkerPool){
if(w.name === name){
this.failWorkerPool.splice(indx,1);
return;
}
indx ++;
}
indx = 0;
for(let w of this.collectorPool){
if(w.name === name){
this.collectorPool.splice(indx,1);
return;
}
indx ++;
}
}
/**
* insert a new msg in the queue
* @param msg
* @param onComplete
*/
insertInQueue(msg,onComplete){
assert.equal(typeof msg,'object','msg must be an object');
assert.equal(typeof onComplete,'function','onComplete must be a function');
let th = this;
console.log(MODULE_NAME + ': insert new msg');
this.queueMgr.insertInQueue(
FATUS_QUEUE_NAME,
msg,
function onDone(e,v){
th.addWorker();
onComplete(e,v);
}
);
}
/**
* insert a new msg in the queue
* @param msg
* @param onComplete
*/
insertInFailQueue(msg,onComplete){
assert.equal(typeof msg,'object','msg must be an object');
assert.equal(typeof onComplete,'function','onComplete must be a function');
let th = this;
let msgObj = msg;
console.log(MODULE_NAME + ': moving to fail');
var insertFunction = function insert(err,res){
// if(err)//ignore
th.queueMgr.insertInQueue(
FATUS_QUEUE_FAIL_NAME,
msgObj.messageText || msgObj,
function onDone(e,v){
onComplete(e,v);
}
);
};
try {
th.popMsg(msgObj, insertFunction);
}catch(ex){
insertFunction(null,null);
}
}
/**
* get the first element of the queue
* @param onGet callback function
*/
getQueueTop(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.getMessage(FATUS_QUEUE_NAME,{visibilitytimeout:0},onGet);
}
/**
* get the first element of the fail queue
* @param onGet callback function
*/
getFailTop(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.getMessage(FATUS_QUEUE_FAIL_NAME,{visibilitytimeout:0},onGet);
}
/**
* get the size of the queue
* @param onGet
*/
getQueueSize(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.countQueue(FATUS_QUEUE_NAME,onGet);
}
/**
* get the size of the fail queue
* @param onGet
*/
getFailSize(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.countQueue(FATUS_QUEUE_FAIL_NAME,onGet);
}
/**
* pop a message from the queue
* @param msg
* @param onDelete
*/
popMsg(msg,onDelete){
assert.equal(typeof onDelete,'function','onDelete must be a function');
assert.equal(typeof msg,'object','msg must be an object');
this.queueMgr.deleteMessage(FATUS_QUEUE_NAME,msg.messageId,msg.popReceipt,{},onDelete);
}
/**
* pop a message from the queue
* @param msg
* @param onDelete
*/
popFail(msg,onDelete){
assert.equal(typeof onDelete,'function','onDelete must be a function');
assert.equal(typeof msg,'object','msg must be an object');
this.queueMgr.deleteMessage(FATUS_QUEUE_FAIL_NAME,msg.messageId,msg.popReceipt,{},onDelete);
}
/**
* update a message in the main queue
* @param msg
* @param onUpdate
*/
updateMsg(msg,onUpdate){
assert.equal(typeof onUpdate,'function','onUpdate must be a function');
assert.equal(typeof msg,'object','msg must be an object');
assert.equal(typeof msg.messageText.messageText,'undefined','ECCO LERRORE');
this.queueMgr.updateMessage(FATUS_QUEUE_NAME,msg.messageId,msg.popReceipt,null,msg.messageText,{},onUpdate);
}
/**
* update a message in the fail queue
* @param msg
* @param onUpdate
*/
updateFail(msg,onUpdate){
assert.equal(typeof onUpdate,'function','onUpdate must be a function');
assert.equal(typeof msg,'object','msg must be an object');
assert.equal(typeof msg.messageText.messageText,'undefined','ECCO LERRORE');
this.queueMgr.updateMessage(FATUS_QUEUE_FAIL_NAME,msg.messageId,msg.popReceipt,null,msg.messageText,{},onUpdate);
}
/**
* peek at the top of the cue
* @param onGet function(err,res)
*/
peekTop(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.peekMsg(FATUS_QUEUE_NAME,onGet);
}
/**
* return ALL the cue
* @param onGet function(err,res)
*/
getAll(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.peekQueue(FATUS_QUEUE_NAME,onGet);
}
/**
* return ALL the cue
* @param onGet function(err,res)
*/
getAllFail(onGet){
assert.equal(typeof onGet,'function','onGet must be a function');
this.queueMgr.peekQueue(FATUS_QUEUE_FAIL_NAME,onGet);
}
/**
* clear all the queue
* @param onClear function(err,res)
*/
clear(onClear){
this.queueMgr.clearQueue(FATUS_QUEUE_NAME,onClear);
}
/**
* clear all the queue
* @param onClear function(err,res)
*/
clearFail(onClear){
this.queueMgr.clearQueue(FATUS_QUEUE_FAIL_NAME,onClear);
}
/**
* get a new messagejob
* @returns a messageJob object
*/
createMessageJob(){
return new MessageJob();
}
/**
* staticaly add a new priority worker if the queue is not empty
*/
static monitor(){
let fatus = Fatusjs.instance;
fatus.getQueueSize(function onGet(err,count){
if(count && count >0){
fatus.addPriorityWorker();
}
});
}
/**
* staticaly add a new fail worker if the fail queue is not empty
*/
static failRetry(){
let fatus = Fatusjs.instance;
fatus.getFailSize(function onGet(err,count){
if(count && count >0){
console.log(MODULE_NAME + ': adding fail worker');
fatus.addFailWorker();
}
});
fatus.getQueueSize(function onGet(err,count){
if(count && count >0){
console.log(MODULE_NAME + ': adding collector' )
fatus.addCollector();
}
})
}
/**
* staticaly add a new worker if the queue is not empty
*/
static autoWorker(){
let fatus = Fatusjs.instance;
fatus.getQueueSize(function onGet(err,count){
if(count && count >0){
console.log(MODULE_NAME + ': adding fail worker');
fatus.addWorker();
}
});
}
/**
* statically print the queue
*/
static printQ(){
let fatus = Fatusjs.instance;
fatus.getAll(function onGet(err, res) {
if(res && res.length>0)
console.log(MODULE_NAME + ': queue is %s', util.inspect(res, {colors: true}));
});
fatus.getAllFail(function onGet(err, res) {
if(res && res.length>0)
console.log(MODULE_NAME + ': queue fail is %s', util.inspect(res, {colors: true}));
});
}
/**
* register a scheduled auto worker
*/
registerAutoWorker(){
var functionPtr = Fatusjs.autoWorker;
if(!this.autoInterval) {
this.autoInterval = setInterval(
functionPtr,
30000
);
}
}
/**
* register a scheduled monitoring worker
*/
registerMonitor(){
var functionPtr = Fatusjs.monitor;
if(!this.monitorInterval) {
this.monitorInterval = setInterval(
functionPtr,
120000
);
}
}
/**
* register a scheduled fail worker
*/
registerFailover(){
var functionPtr = Fatusjs.failRetry;
if(!this.failInterval) {
this.failInterval = setInterval(
functionPtr,
180000
);
}
}
/**
* register a scheduled fail worker
*/
registerPrint(){
var functionPtr = Fatusjs.printQ;
if(!this.printInterval) {
this.printInterval = setInterval(
functionPtr,
180000
);
}
}
/**
* remove monitoring worker, if any
*/
unregisterMonitor(){
if(this.monitorInterval){
clearInterval(this.monitorInterval);
this.monitorInterval = false;
}
}
/**
* remove failover managemnt, if any
*/
unregisterFailover(){
if(this.failInterval){
clearInterval(this.failInterval);
this.failInterval = false;
}
}
/**
* remove failover managemnt, if any
*/
unregisterAutoWorker(){
if(this.autoInterval){
clearInterval(this.autoInterval);
this.autoInterval = false;
}
}
/**
* wrapper for the serve queue invoke - still in super development
* @param req
* @param res
*/
static serveQueue(req,res){
let sq = new ServeQueue();
sq.invoke(req,res);
}
}
/** Exports */
module.exports = Fatusjs;