cron 3.0.0-beta.4 → 3.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/job.js DELETED
@@ -1,215 +0,0 @@
1
- function CronJob(CronTime, spawn) {
2
- function fnWrap(cmd) {
3
- let command;
4
- let args;
5
-
6
- switch (typeof cmd) {
7
- case 'string':
8
- args = cmd.split(' ');
9
- command = args.shift();
10
-
11
- return spawn.bind(undefined, command, args);
12
-
13
- case 'object':
14
- command = cmd && cmd.command;
15
- if (command) {
16
- args = cmd.args;
17
- const options = cmd.options;
18
-
19
- return spawn.bind(undefined, command, args, options);
20
- }
21
- break;
22
- }
23
-
24
- return cmd;
25
- }
26
-
27
- function CJ(
28
- cronTime,
29
- onTick,
30
- onComplete,
31
- startNow,
32
- timeZone,
33
- context,
34
- runOnInit,
35
- utcOffset,
36
- unrefTimeout
37
- ) {
38
- let _cronTime = cronTime;
39
- let argCount = 0;
40
- for (let i = 0; i < arguments.length; i++) {
41
- if (arguments[i] !== undefined) {
42
- argCount++;
43
- }
44
- }
45
-
46
- if (typeof cronTime !== 'string' && argCount === 1) {
47
- // crontime is an object...
48
- onTick = cronTime.onTick;
49
- onComplete = cronTime.onComplete;
50
- context = cronTime.context;
51
- startNow = cronTime.start || cronTime.startNow || cronTime.startJob;
52
- timeZone = cronTime.timeZone;
53
- runOnInit = cronTime.runOnInit;
54
- _cronTime = cronTime.cronTime;
55
- utcOffset = cronTime.utcOffset;
56
- unrefTimeout = cronTime.unrefTimeout;
57
- }
58
-
59
- this.context = context || this;
60
- this._callbacks = [];
61
- this.onComplete = fnWrap(onComplete);
62
- this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);
63
- this.unrefTimeout = unrefTimeout;
64
-
65
- addCallback.call(this, fnWrap(onTick));
66
-
67
- if (runOnInit) {
68
- this.lastExecution = new Date();
69
- fireOnTick.call(this);
70
- }
71
-
72
- if (startNow) {
73
- start.call(this);
74
- }
75
-
76
- return this;
77
- }
78
-
79
- const addCallback = function (callback) {
80
- if (typeof callback === 'function') {
81
- this._callbacks.push(callback);
82
- }
83
- };
84
- CJ.prototype.addCallback = addCallback;
85
-
86
- CJ.prototype.setTime = function (time) {
87
- if (typeof time !== 'object') {
88
- // crontime is an object...
89
- throw new Error('time must be an instance of CronTime.');
90
- }
91
- const wasRunning = this.running;
92
- this.stop();
93
- this.cronTime = time;
94
- if (wasRunning) this.start();
95
- };
96
-
97
- CJ.prototype.nextDate = function () {
98
- return this.cronTime.sendAt();
99
- };
100
-
101
- const fireOnTick = function () {
102
- for (let i = this._callbacks.length - 1; i >= 0; i--) {
103
- this._callbacks[i].call(this.context, this.onComplete);
104
- }
105
- };
106
- CJ.prototype.fireOnTick = fireOnTick;
107
-
108
- CJ.prototype.nextDates = function (i) {
109
- return this.cronTime.sendAt(i || 0);
110
- };
111
-
112
- const start = function () {
113
- if (this.running) {
114
- return;
115
- }
116
-
117
- const MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
118
- const self = this;
119
- let timeout = this.cronTime.getTimeout();
120
- let remaining = 0;
121
- let startTime;
122
-
123
- if (this.cronTime.realDate) {
124
- this.runOnce = true;
125
- }
126
-
127
- function _setTimeout(timeout) {
128
- startTime = Date.now();
129
- self._timeout = setTimeout(callbackWrapper, timeout);
130
- if (self.unrefTimeout && typeof self._timeout.unref === 'function') {
131
- self._timeout.unref();
132
- }
133
- }
134
-
135
- // The callback wrapper checks if it needs to sleep another period or not
136
- // and does the real callback logic when it's time.
137
- function callbackWrapper() {
138
- const diff = startTime + timeout - Date.now();
139
-
140
- if (diff > 0) {
141
- let newTimeout = self.cronTime.getTimeout();
142
-
143
- if (newTimeout > diff) {
144
- newTimeout = diff;
145
- }
146
-
147
- remaining += newTimeout;
148
- }
149
-
150
- // If there is sleep time remaining, calculate how long and go to sleep
151
- // again. This processing might make us miss the deadline by a few ms
152
- // times the number of sleep sessions. Given a MAXDELAY of almost a
153
- // month, this should be no issue.
154
- self.lastExecution = new Date();
155
- if (remaining) {
156
- if (remaining > MAXDELAY) {
157
- remaining -= MAXDELAY;
158
- timeout = MAXDELAY;
159
- } else {
160
- timeout = remaining;
161
- remaining = 0;
162
- }
163
-
164
- _setTimeout(timeout);
165
- } else {
166
- // We have arrived at the correct point in time.
167
-
168
- self.running = false;
169
-
170
- // start before calling back so the callbacks have the ability to stop the cron job
171
- if (!self.runOnce) {
172
- self.start();
173
- }
174
-
175
- self.fireOnTick();
176
- }
177
- }
178
-
179
- if (timeout >= 0) {
180
- this.running = true;
181
-
182
- // Don't try to sleep more than MAXDELAY ms at a time.
183
-
184
- if (timeout > MAXDELAY) {
185
- remaining = timeout - MAXDELAY;
186
- timeout = MAXDELAY;
187
- }
188
-
189
- _setTimeout(timeout);
190
- } else {
191
- this.stop();
192
- }
193
- };
194
-
195
- CJ.prototype.start = start;
196
-
197
- CJ.prototype.lastDate = function () {
198
- return this.lastExecution;
199
- };
200
-
201
- /**
202
- * Stop the cronjob.
203
- */
204
- CJ.prototype.stop = function () {
205
- if (this._timeout) clearTimeout(this._timeout);
206
- this.running = false;
207
- if (typeof this.onComplete === 'function') {
208
- this.onComplete();
209
- }
210
- };
211
-
212
- return CJ;
213
- }
214
-
215
- module.exports = CronJob;