@stacksjs/scheduler 0.59.10 → 0.61.0
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/dist/index.js +151 -150
- package/package.json +4 -3
- package/src/job.ts +47 -82
- package/src/schedule.ts +5 -5
- package/src/time.ts +92 -196
- package/src/types/cron.ts +23 -49
- package/src/types/utils.ts +2 -9
- package/src/utils.ts +4 -7
- package/dist/constants.d.ts +0 -74
- package/dist/errors.d.ts +0 -5
- package/dist/index.d.ts +0 -7
- package/dist/job.d.ts +0 -32
- package/dist/schedule.d.ts +0 -29
- package/dist/time.d.ts +0 -159
- package/dist/types/cron.d.ts +0 -49
- package/dist/types/utils.d.ts +0 -3
- package/dist/utils.d.ts +0 -12
package/src/job.ts
CHANGED
|
@@ -23,9 +23,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
23
23
|
lastExecution: Date | null = null
|
|
24
24
|
runOnce = false
|
|
25
25
|
context: CronContext<C>
|
|
26
|
-
onComplete?: WithOnComplete<OC> extends true
|
|
27
|
-
? CronOnCompleteCallback
|
|
28
|
-
: undefined
|
|
26
|
+
onComplete?: WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined
|
|
29
27
|
|
|
30
28
|
private _timeout?: NodeJS.Timeout
|
|
31
29
|
private _callbacks: CronCallback<C, WithOnComplete<OC>>[] = []
|
|
@@ -41,7 +39,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
41
39
|
runOnInit?: CronJobParams<OC, C>['runOnInit'],
|
|
42
40
|
utcOffset?: null,
|
|
43
41
|
unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'],
|
|
44
|
-
errorHandler?: (error: Error) => void
|
|
42
|
+
errorHandler?: (error: Error) => void,
|
|
45
43
|
)
|
|
46
44
|
constructor(
|
|
47
45
|
cronTime: CronJobParams<OC, C>['cronTime'],
|
|
@@ -53,7 +51,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
53
51
|
runOnInit?: CronJobParams<OC, C>['runOnInit'],
|
|
54
52
|
utcOffset?: CronJobParams<OC, C>['utcOffset'],
|
|
55
53
|
unrefTimeout?: CronJobParams<OC, C>['unrefTimeout'],
|
|
56
|
-
errorHandler?: (error: Error) => void
|
|
54
|
+
errorHandler?: (error: Error) => void,
|
|
57
55
|
)
|
|
58
56
|
constructor(
|
|
59
57
|
cronTime: CronJobParams<OC, C>['cronTime'],
|
|
@@ -70,25 +68,18 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
70
68
|
this._errorHandler = errorHandler
|
|
71
69
|
this.context = (context ?? this) as CronContext<C>
|
|
72
70
|
|
|
73
|
-
const { timeZone: tz, utcOffset: uo } = getTimeZoneAndOffset(
|
|
74
|
-
timeZone,
|
|
75
|
-
utcOffset,
|
|
76
|
-
)
|
|
71
|
+
const { timeZone: tz, utcOffset: uo } = getTimeZoneAndOffset(timeZone, utcOffset)
|
|
77
72
|
|
|
78
73
|
this.cronTime = new CronTime(cronTime, tz, uo as null | undefined)
|
|
79
74
|
|
|
80
|
-
if (unrefTimeout != null)
|
|
81
|
-
this.unrefTimeout = unrefTimeout
|
|
75
|
+
if (unrefTimeout != null) this.unrefTimeout = unrefTimeout
|
|
82
76
|
|
|
83
77
|
if (onComplete != null) {
|
|
84
78
|
// casting to the correct type since we just made sure that WithOnComplete<OC> = true
|
|
85
|
-
this.onComplete = this._fnWrap(
|
|
86
|
-
onComplete,
|
|
87
|
-
) as WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined
|
|
79
|
+
this.onComplete = this._fnWrap(onComplete) as WithOnComplete<OC> extends true ? CronOnCompleteCallback : undefined
|
|
88
80
|
}
|
|
89
81
|
|
|
90
|
-
if (this.cronTime.realDate)
|
|
91
|
-
this.runOnce = true
|
|
82
|
+
if (this.cronTime.realDate) this.runOnce = true
|
|
92
83
|
|
|
93
84
|
this.addCallback(this._fnWrap(onTick))
|
|
94
85
|
|
|
@@ -97,16 +88,12 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
97
88
|
this.fireOnTick()
|
|
98
89
|
}
|
|
99
90
|
|
|
100
|
-
if (start)
|
|
101
|
-
this.start()
|
|
91
|
+
if (start) this.start()
|
|
102
92
|
}
|
|
103
93
|
|
|
104
|
-
static from<OC extends CronOnCompleteCommand | null = null, C = null>(
|
|
105
|
-
params: CronJobParams<OC, C>,
|
|
106
|
-
) {
|
|
94
|
+
static from<OC extends CronOnCompleteCommand | null = null, C = null>(params: CronJobParams<OC, C>) {
|
|
107
95
|
// runtime check for JS users
|
|
108
|
-
if (params.timeZone != null && params.utcOffset != null)
|
|
109
|
-
throw new ExclusiveParametersError('timeZone', 'utcOffset')
|
|
96
|
+
if (params.timeZone != null && params.utcOffset != null) throw new ExclusiveParametersError('timeZone', 'utcOffset')
|
|
110
97
|
|
|
111
98
|
if (params.timeZone != null) {
|
|
112
99
|
return new CronJob<OC, C>(
|
|
@@ -121,7 +108,8 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
121
108
|
params.unrefTimeout,
|
|
122
109
|
)
|
|
123
110
|
}
|
|
124
|
-
|
|
111
|
+
|
|
112
|
+
if (params.utcOffset != null) {
|
|
125
113
|
return new CronJob<OC, C>(
|
|
126
114
|
params.cronTime,
|
|
127
115
|
params.onTick,
|
|
@@ -134,19 +122,18 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
134
122
|
params.unrefTimeout,
|
|
135
123
|
)
|
|
136
124
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
125
|
+
|
|
126
|
+
return new CronJob<OC, C>(
|
|
127
|
+
params.cronTime,
|
|
128
|
+
params.onTick,
|
|
129
|
+
params.onComplete,
|
|
130
|
+
params.start,
|
|
131
|
+
params.timeZone,
|
|
132
|
+
params.context,
|
|
133
|
+
params.runOnInit,
|
|
134
|
+
params.utcOffset,
|
|
135
|
+
params.unrefTimeout,
|
|
136
|
+
)
|
|
150
137
|
}
|
|
151
138
|
|
|
152
139
|
private _fnWrap(cmd: CronCommand<C, boolean>): CronCallback<C, boolean> {
|
|
@@ -162,34 +149,25 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
162
149
|
}
|
|
163
150
|
|
|
164
151
|
case 'object': {
|
|
165
|
-
return spawn.bind(
|
|
166
|
-
undefined,
|
|
167
|
-
cmd.command,
|
|
168
|
-
cmd.args ?? [],
|
|
169
|
-
cmd.options ?? {},
|
|
170
|
-
) as () => void
|
|
152
|
+
return spawn.bind(undefined, cmd.command, cmd.args ?? [], cmd.options ?? {}) as () => void
|
|
171
153
|
}
|
|
172
154
|
}
|
|
173
155
|
}
|
|
174
156
|
|
|
175
157
|
addCallback(callback: CronCallback<C, WithOnComplete<OC>>) {
|
|
176
|
-
if (typeof callback === 'function')
|
|
177
|
-
this._callbacks.push(callback)
|
|
158
|
+
if (typeof callback === 'function') this._callbacks.push(callback)
|
|
178
159
|
}
|
|
179
160
|
|
|
180
161
|
setTime(time: CronTime) {
|
|
181
|
-
if (!(time instanceof CronTime))
|
|
182
|
-
throw new CronError('time must be an instance of CronTime.')
|
|
162
|
+
if (!(time instanceof CronTime)) throw new CronError('time must be an instance of CronTime.')
|
|
183
163
|
|
|
184
164
|
const wasRunning = this.running
|
|
185
165
|
this.stop()
|
|
186
166
|
|
|
187
167
|
this.cronTime = time
|
|
188
|
-
if (time.realDate)
|
|
189
|
-
this.runOnce = true
|
|
168
|
+
if (time.realDate) this.runOnce = true
|
|
190
169
|
|
|
191
|
-
if (wasRunning)
|
|
192
|
-
this.start()
|
|
170
|
+
if (wasRunning) this.start()
|
|
193
171
|
}
|
|
194
172
|
|
|
195
173
|
nextDate() {
|
|
@@ -198,18 +176,16 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
198
176
|
|
|
199
177
|
fireOnTick() {
|
|
200
178
|
try {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
? CronOnCompleteCallback
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
catch (error) {
|
|
179
|
+
for (const callback of this._callbacks) {
|
|
180
|
+
void callback.call(
|
|
181
|
+
this.context,
|
|
182
|
+
this.onComplete as WithOnComplete<OC> extends true ? CronOnCompleteCallback : never,
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
209
186
|
if (this._errorHandler && error instanceof Error) {
|
|
210
187
|
this._errorHandler(error)
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
188
|
+
} else {
|
|
213
189
|
// Handle the case where no error handler is provided or the caught object is not an Error
|
|
214
190
|
console.error('An error occurred in the cron job callback:', error)
|
|
215
191
|
}
|
|
@@ -221,8 +197,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
221
197
|
}
|
|
222
198
|
|
|
223
199
|
start() {
|
|
224
|
-
if (this.running)
|
|
225
|
-
return
|
|
200
|
+
if (this.running) return
|
|
226
201
|
|
|
227
202
|
const MAXDELAY = 2147483647 // The maximum number of milliseconds setTimeout will wait.
|
|
228
203
|
let timeout = this.cronTime.getTimeout()
|
|
@@ -230,11 +205,8 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
230
205
|
let startTime: number
|
|
231
206
|
|
|
232
207
|
const setCronTimeout = (t: number) => {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
this._timeout = setTimeout(callbackWrapper, t)
|
|
236
|
-
if (this.unrefTimeout && typeof this._timeout!.unref === 'function')
|
|
237
|
-
this._timeout!.unref()
|
|
208
|
+
this._timeout = setTimeout(callbackWrapper, t) as NodeJS.Timeout
|
|
209
|
+
if (this.unrefTimeout && typeof this._timeout.unref === 'function') this._timeout.unref()
|
|
238
210
|
}
|
|
239
211
|
|
|
240
212
|
// The callback wrapper checks if it needs to sleep another period or not
|
|
@@ -245,8 +217,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
245
217
|
if (diff > 0) {
|
|
246
218
|
let newTimeout = this.cronTime.getTimeout()
|
|
247
219
|
|
|
248
|
-
if (newTimeout > diff)
|
|
249
|
-
newTimeout = diff
|
|
220
|
+
if (newTimeout > diff) newTimeout = diff
|
|
250
221
|
|
|
251
222
|
remaining += newTimeout
|
|
252
223
|
}
|
|
@@ -259,23 +230,20 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
259
230
|
if (remaining > MAXDELAY) {
|
|
260
231
|
remaining -= MAXDELAY
|
|
261
232
|
timeout = MAXDELAY
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
233
|
+
} else {
|
|
264
234
|
timeout = remaining
|
|
265
235
|
remaining = 0
|
|
266
236
|
}
|
|
267
237
|
|
|
268
238
|
setCronTimeout(timeout)
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
239
|
+
} else {
|
|
271
240
|
// We have arrived at the correct point in time.
|
|
272
241
|
this.lastExecution = new Date()
|
|
273
242
|
|
|
274
243
|
this.running = false
|
|
275
244
|
|
|
276
245
|
// start before calling back so the callbacks have the ability to stop the cron job
|
|
277
|
-
if (!this.runOnce)
|
|
278
|
-
this.start()
|
|
246
|
+
if (!this.runOnce) this.start()
|
|
279
247
|
|
|
280
248
|
this.fireOnTick()
|
|
281
249
|
}
|
|
@@ -292,8 +260,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
292
260
|
}
|
|
293
261
|
|
|
294
262
|
setCronTimeout(timeout)
|
|
295
|
-
}
|
|
296
|
-
else {
|
|
263
|
+
} else {
|
|
297
264
|
this.stop()
|
|
298
265
|
}
|
|
299
266
|
}
|
|
@@ -306,10 +273,8 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
|
|
|
306
273
|
* Stop the cronjob.
|
|
307
274
|
*/
|
|
308
275
|
stop() {
|
|
309
|
-
if (this._timeout)
|
|
310
|
-
clearTimeout(this._timeout)
|
|
276
|
+
if (this._timeout) clearTimeout(this._timeout)
|
|
311
277
|
this.running = false
|
|
312
|
-
if (typeof this.onComplete === 'function')
|
|
313
|
-
void this.onComplete.call(this.context)
|
|
278
|
+
if (typeof this.onComplete === 'function') void this.onComplete.call(this.context)
|
|
314
279
|
}
|
|
315
280
|
}
|
package/src/schedule.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/* eslint-disable no-new */
|
|
2
1
|
import { log } from '@stacksjs/cli'
|
|
3
2
|
import type { DateTime } from 'luxon'
|
|
3
|
+
import { CronJob } from './job'
|
|
4
4
|
import { CronTime } from './time'
|
|
5
5
|
|
|
6
6
|
export class Schedule {
|
|
7
|
-
private cronPattern
|
|
8
|
-
private timezone
|
|
7
|
+
private cronPattern = ''
|
|
8
|
+
private timezone = 'America/Los_Angeles'
|
|
9
9
|
private readonly task: () => void
|
|
10
10
|
// private cmd?: string
|
|
11
11
|
|
|
@@ -110,8 +110,8 @@ export class Schedule {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
static command(cmd: string) {
|
|
113
|
-
|
|
114
|
-
this.cmd = cmd
|
|
113
|
+
log.info(`Executing command: ${cmd}`)
|
|
114
|
+
// this.cmd = cmd
|
|
115
115
|
return this
|
|
116
116
|
}
|
|
117
117
|
}
|