-
Notifications
You must be signed in to change notification settings - Fork 4
/
task.js
199 lines (167 loc) · 3.28 KB
/
task.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
var ms = require('ms');
/**
* expose Task
*/
module.exports = Task;
/**
* create a new task
* @param {String} name
*
* @api private
*/
function Task(name) {
this.name = name;
this_interval = ms('2s');
this._retry = 0;
}
/**
* Process a job
* @param {Object}
*
* @api private
*/
Task.prototype.process = function(job) {
var _this = this,
timeout, death, delay;
if (this._online && !navigator.onLine) {
job.emit('offline');
replay(job);
return;
}
// set job retry
job.retry = (job.retry || 0) + 1;
// get delay
delay = (this._delay && job.retry === 1) ? this._delay : 0;
setTimeout(function () {
// check timeout
if (_this._timeout) {
job.timeout = false;
timeout = setTimeout(function () {
job.timeout = true;
job.emit('timeout');
_this.replay(job);
}, _this._timeout);
}
// check lifetime
if (_this._lifetime) {
job.death = false;
death = setTimeout(function () {
job.death = true;
job.emit('error');
}, _this.timeToLive(job));
}
// start action
_this._action(job, function(err) {
if (job.timeout || job.death) return;
clearTimeout(timeout);
clearTimeout(death);
if (err) {
job.emit('fail', err);
_this.replay(job);
} else {
job.emit('complete');
}
});
}, delay);
};
/**
* set task action
* @param {[Function]}
* @return {Task}
*
* @api public
*/
Task.prototype.action = function(action) {
this._action = action;
return this;
};
/**
* replay a job
*
* @api private
*/
Task.prototype.replay = function(job) {
var _this = this,
canReplay = ((job.retry || 0) <= this._retry) &&
(this.timeToLive(job) > 0);
if (canReplay)
setTimeout(function() {_this.process(job);}, this._interval);
else
job.emit('error');
};
/**
* set interval between two tentatives (default is 2secs)
* @param {Number} interval
* @return {Task}
*
* @api public
*/
Task.prototype.interval = function(interval) {
this._interval = ms(interval);
return this;
};
/**
* set task lifetime
* @param {String} lifetime (default is Infinity)
* @return {Task}
*
* @api public
*/
Task.prototype.lifetime = function(lifetime) {
this._lifetime = ms(lifetime);
return this;
};
/**
* get time to live
* @param {Object} job
* @return {Number} time to live in ms
*
* @api private
*/
Task.prototype.timeToLive = function (job) {
if (!this._lifetime) return Infinity;
Math.max(0, job.time + this._lifetime - new Date().getTime());
};
/**
* set retry count
* @param {Number} retry (default is 0)
* @return {Task}
*
* @api public
*/
Task.prototype.retry = function(retry) {
this._retry = retry;
return this;
};
/**
* set online flag, defines wether or not the task can be executed offline
* @return {Task}
*
* @api public
*/
Task.prototype.online = function() {
this._online = true;
return this;
};
/**
* set timeout
* @param {String} timeout
* @return {Task}
*
* @api public
*/
Task.prototype.timeout = function(timeout) {
this._timeout = ms(timeout);
return this;
};
/**
* set delay
* @param {String} delay
* @return {Task}
*
* @api public
*/
Task.prototype.delay = function(delay) {
this._delay = ms(delay);
return this;
};