-
Notifications
You must be signed in to change notification settings - Fork 1
/
failworker.js
149 lines (137 loc) · 4.46 KB
/
failworker.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
/**
* Created by mox on 29/03/16.
* Fatus worker for fail queue
*
*/
'use strict';
const MODULE_NAME = 'FatusFailWorker';
const FatusWorker = require('./worker');
const MessageJob = require('./messagejob');
const moment = require('moment');
const assert = require('assert');
const util = require('util');
const retry = require('retry');
const async = require('async');
/** the worker for the fatus */
class FatusFailWorker extends FatusWorker {
/**
* set the maximum number of failure that a failed job allow
* @param maxFail
*/
setMaxFails(maxFail){
this.MAX_FAIL_ALLOWED = maxFail;
}
/**
* overridable execute method
* @param th
* @param jobObj
* @param wfcallback
* @override
*/
execute(th, jobObj, cb) {
console.log(MODULE_NAME + '%s: executing from fail queue ', th.name);
if(jobObj.fail>th.MAX_FAIL_ALLOWED){
console.error(MODULE_NAME + '%s: job %s SUSPENDED for too many fails',th.name,jobObj.getId());
cb(new Error('job suspended',null));
}else {
jobObj.execute(th, cb);
}
}
/**
* print all the queue to console
* @param th pointer to worker
*/
printQueue(th) {
th.fatus.getAllFail(function onGet(err, res) {
console.log(MODULE_NAME + '%s: fail queue is %s', th.name, util.inspect(res, {colors: true}));
});
}
/**
* get the fail queue size
* @param fatus
* @param onGet
* @override
*/
getQueueSize(fatus,onGet){
fatus.getFailSize(onGet);
}
/**
* get a new job from the fail queue
* @param th
* @param wfcallback
* @override
*/
fetchNewJob(th, wfcallback) {
th.fetchIteration = th.fetchIteration +1;
if(th.fetchIteration<(th.STACK_PROTECTION_THRSD*2)) {
let NOW = moment();
th.fatus.getFailTop(function onGet(err, msg) {
if (!err && msg && msg[0] && msg[0].messageText) {
if(th.isMsgReserved(msg, NOW, th)) {
return th.fetchNewJob(th, wfcallback);
}else{
wfcallback(err, msg);
}
}else{
wfcallback(err, null);
}
});
}else{
wfcallback(null,null);
}
}
/**
* pop a message from the fail queue, while completed,with fault tollerance
* @param msg the message to pop
* @param th the reference to the worker
* @param callback classic err/res callback
* @override
*/
popMessageFT(msg, th, callback){
th.emit('pop',msg.messageText);
var ftOperation = retry.operation({
retries: 10, // number of retry times
factor: 1, // moltiplication factor for every rerty
minTimeout: 1 * 1000, // minimum timeout allowed
maxTimeout: 1 * 1000 // maximum timeout allowed
});
console.log('======POP: ' + util.inspect(msg));
ftOperation.attempt(
function (currentAttempt){
th.fatus.popFail(msg,function(err,res){
if (ftOperation.retry(err)){
return;
}
callback(err ? ftOperation.mainError() : null, res);
})
});
}
/**update a messase in the queue, with fault tollerance
* run a update
* @param msg
* @param th
* @param callback
* @override
*/
updateMessageFT(msg, th, callback){
var ftOperation = retry.operation({
retries: 10, // number of retry times
factor: 1, // moltiplication factor for every rerty
minTimeout: 1 * 1000, // minimum timeout allowed
maxTimeout: 1 * 1000 // maximum timeout allowed
});
var fatus = th.fatus;
ftOperation.attempt(
function (currentAttempt){
fatus.updateFail(msg,function(err,res){
if (ftOperation.retry(err)){
//console.log(MODULE_NAME + '%s: err on updateMessage for msg %s ',th.name,msg.messageId);
return;
}
callback(err ? ftOperation.mainError() : null, res);
})
});
}
}
/** Exports */
module.exports = FatusFailWorker;