forked from kelektiv/node-cron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.js
215 lines (176 loc) · 4.58 KB
/
job.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
function CronJob(CronTime, spawn) {
function fnWrap(cmd) {
let command;
let args;
switch (typeof cmd) {
case 'string':
args = cmd.split(' ');
command = args.shift();
return spawn.bind(undefined, command, args);
case 'object':
command = cmd && cmd.command;
if (command) {
args = cmd.args;
const options = cmd.options;
return spawn.bind(undefined, command, args, options);
}
break;
}
return cmd;
}
function CJ(
cronTime,
onTick,
onComplete,
startNow,
timeZone,
context,
runOnInit,
utcOffset,
unrefTimeout
) {
let _cronTime = cronTime;
let argCount = 0;
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] !== undefined) {
argCount++;
}
}
if (typeof cronTime !== 'string' && argCount === 1) {
// crontime is an object...
onTick = cronTime.onTick;
onComplete = cronTime.onComplete;
context = cronTime.context;
startNow = cronTime.start || cronTime.startNow || cronTime.startJob;
timeZone = cronTime.timeZone;
runOnInit = cronTime.runOnInit;
_cronTime = cronTime.cronTime;
utcOffset = cronTime.utcOffset;
unrefTimeout = cronTime.unrefTimeout;
}
this.context = context || this;
this._callbacks = [];
this.onComplete = fnWrap(onComplete);
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);
this.unrefTimeout = unrefTimeout;
addCallback.call(this, fnWrap(onTick));
if (runOnInit) {
this.lastExecution = new Date();
fireOnTick.call(this);
}
if (startNow) {
start.call(this);
}
return this;
}
const addCallback = function (callback) {
if (typeof callback === 'function') {
this._callbacks.push(callback);
}
};
CJ.prototype.addCallback = addCallback;
CJ.prototype.setTime = function (time) {
if (typeof time !== 'object') {
// crontime is an object...
throw new Error('time must be an instance of CronTime.');
}
const wasRunning = this.running;
this.stop();
this.cronTime = time;
if (wasRunning) this.start();
};
CJ.prototype.nextDate = function () {
return this.cronTime.sendAt();
};
const fireOnTick = function () {
for (let i = this._callbacks.length - 1; i >= 0; i--) {
this._callbacks[i].call(this.context, this.onComplete);
}
};
CJ.prototype.fireOnTick = fireOnTick;
CJ.prototype.nextDates = function (i) {
return this.cronTime.sendAt(i);
};
const start = function () {
if (this.running) {
return;
}
const MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
const self = this;
let timeout = this.cronTime.getTimeout();
let remaining = 0;
let startTime;
if (this.cronTime.realDate) {
this.runOnce = true;
}
function _setTimeout(timeout) {
startTime = Date.now();
self._timeout = setTimeout(callbackWrapper, timeout);
if (self.unrefTimeout && typeof self._timeout.unref === 'function') {
self._timeout.unref();
}
}
// The callback wrapper checks if it needs to sleep another period or not
// and does the real callback logic when it's time.
function callbackWrapper() {
const diff = startTime + timeout - Date.now();
if (diff > 0) {
let newTimeout = self.cronTime.getTimeout();
if (newTimeout > diff) {
newTimeout = diff;
}
remaining += newTimeout;
}
// If there is sleep time remaining, calculate how long and go to sleep
// again. This processing might make us miss the deadline by a few ms
// times the number of sleep sessions. Given a MAXDELAY of almost a
// month, this should be no issue.
self.lastExecution = new Date();
if (remaining) {
if (remaining > MAXDELAY) {
remaining -= MAXDELAY;
timeout = MAXDELAY;
} else {
timeout = remaining;
remaining = 0;
}
_setTimeout(timeout);
} else {
// We have arrived at the correct point in time.
self.running = false;
// start before calling back so the callbacks have the ability to stop the cron job
if (!self.runOnce) {
self.start();
}
self.fireOnTick();
}
}
if (timeout >= 0) {
this.running = true;
// Don't try to sleep more than MAXDELAY ms at a time.
if (timeout > MAXDELAY) {
remaining = timeout - MAXDELAY;
timeout = MAXDELAY;
}
_setTimeout(timeout);
} else {
this.stop();
}
};
CJ.prototype.start = start;
CJ.prototype.lastDate = function () {
return this.lastExecution;
};
/**
* Stop the cronjob.
*/
CJ.prototype.stop = function () {
if (this._timeout) clearTimeout(this._timeout);
this.running = false;
if (typeof this.onComplete === 'function') {
this.onComplete();
}
};
return CJ;
}
export default CronJob;