@stacksjs/scheduler 0.58.48 → 0.58.49

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.ts +321 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/scheduler",
3
3
  "type": "module",
4
- "version": "0.58.48",
4
+ "version": "0.58.49",
5
5
  "description": "The Stacks scheduler.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -36,7 +36,8 @@
36
36
  ],
37
37
  "files": [
38
38
  "README.md",
39
- "dist"
39
+ "dist",
40
+ "src"
40
41
  ],
41
42
  "scripts": {
42
43
  "build": "bun --bun build.ts",
package/src/index.ts ADDED
@@ -0,0 +1,321 @@
1
+ import cron from 'node-cron'
2
+
3
+ // export interface Scheduler {
4
+ // schedule(schedule: Schedule): void
5
+ // }
6
+
7
+ export interface Schedule {
8
+ action(action: string): this
9
+ command(cmd: string): this
10
+ job(job: string): this
11
+ exec(cmd: string): this
12
+ call(callback: () => void): this
13
+ everyMinute(): this
14
+ everySecond(): this
15
+ everyFiveMinutes(): this
16
+ everyTenMinutes(): this
17
+ everyThirtyMinutes(): this
18
+ hourly(): this
19
+ daily(): this
20
+ twiceDaily(hour1: number, hour2: number): this
21
+ weekly(): this
22
+ monthly(): this
23
+ quarterly(): this
24
+ yearly(): this
25
+ weekdays(): this
26
+ weekends(): this
27
+ mondays(): this
28
+ tuesdays(): this
29
+ wednesdays(): this
30
+ thursdays(): this
31
+ fridays(): this
32
+ saturdays(): this
33
+ sundays(): this
34
+ between(startTime: string, endTime: string): this
35
+ timezone(timezone: string): this
36
+ when(callback: () => boolean): this
37
+ at(time: string): this
38
+ }
39
+
40
+ export class ScheduleImpl implements Schedule {
41
+ private actionName: string = ''
42
+ private commandName: string = ''
43
+ private jobName: string = ''
44
+ private execName: string = ''
45
+ private callback: () => void = () => {}
46
+ private whenCallback: () => boolean = () => true
47
+ private startTime: string = ''
48
+ private endTime: string = ''
49
+ private timezoneName: string = ''
50
+ private time: string = ''
51
+
52
+ action(action: string): this {
53
+ this.actionName = action
54
+ return this
55
+ }
56
+
57
+ command(cmd: string): this {
58
+ this.commandName = cmd
59
+ return this
60
+ }
61
+
62
+ job(job: string): this {
63
+ this.jobName = job
64
+ return this
65
+ }
66
+
67
+ exec(cmd: string): this {
68
+ this.execName = cmd
69
+ return this
70
+ }
71
+
72
+ call(callback: () => void): this {
73
+ this.callback = callback
74
+ return this
75
+ }
76
+
77
+ everyMinute(): this {
78
+ // implementation here
79
+ return this
80
+ }
81
+
82
+ everySecond(): this {
83
+ // implementation here
84
+ return this
85
+ }
86
+
87
+ everyFiveMinutes(): this {
88
+ // implementation here
89
+ return this
90
+ }
91
+
92
+ everyTenMinutes(): this {
93
+ // implementation here
94
+ return this
95
+ }
96
+
97
+ everyThirtyMinutes(): this {
98
+ // implementation here
99
+ return this
100
+ }
101
+
102
+ hourly(): this {
103
+ // implementation here
104
+ return this
105
+ }
106
+
107
+ daily(): this {
108
+ // implementation here
109
+ return this
110
+ }
111
+
112
+ twiceDaily(_hour1: number, _hour2: number): this {
113
+ // implementation here
114
+ return this
115
+ }
116
+
117
+ weekly(): this {
118
+ // implementation here
119
+ return this
120
+ }
121
+
122
+ monthly(): this {
123
+ // implementation here
124
+ return this
125
+ }
126
+
127
+ quarterly(): this {
128
+ // implementation here
129
+ return this
130
+ }
131
+
132
+ yearly(): this {
133
+ // implementation here
134
+ return this
135
+ }
136
+
137
+ weekdays(): this {
138
+ // implementation here
139
+ return this
140
+ }
141
+
142
+ weekends(): this {
143
+ // implementation here
144
+ return this
145
+ }
146
+
147
+ mondays(): this {
148
+ // implementation here
149
+ return this
150
+ }
151
+
152
+ tuesdays(): this {
153
+ // implementation here
154
+ return this
155
+ }
156
+
157
+ wednesdays(): this {
158
+ // implementation here
159
+ return this
160
+ }
161
+
162
+ thursdays(): this {
163
+ // implementation here
164
+ return this
165
+ }
166
+
167
+ fridays(): this {
168
+ // implementation here
169
+ return this
170
+ }
171
+
172
+ saturdays(): this {
173
+ // implementation here
174
+ return this
175
+ }
176
+
177
+ sundays(): this {
178
+ // implementation here
179
+ return this
180
+ }
181
+
182
+ between(startTime: string, endTime: string): this {
183
+ this.startTime = startTime
184
+ this.endTime = endTime
185
+ return this
186
+ }
187
+
188
+ timezone(timezone: string): this {
189
+ this.timezoneName = timezone
190
+ return this
191
+ }
192
+
193
+ when(callback: () => boolean): this {
194
+ this.whenCallback = callback
195
+ return this
196
+ }
197
+
198
+ at(time: string): this {
199
+ this.time = time
200
+ return this
201
+ }
202
+ }
203
+
204
+ export interface Scheduler {
205
+ everySecond: () => void
206
+ everySeconds: (seconds: number) => void
207
+ everyMinute: () => void
208
+ everyMinutes: (minutes: number) => void
209
+ everyTwoMinutes: () => void
210
+ everyThreeMinutes: () => void
211
+ everyFourMinutes: () => void
212
+ everyFiveMinutes: () => void
213
+ everyTenMinutes: () => void
214
+ everyFifteenMinutes: () => void
215
+ everyThirtyMinutes: () => void
216
+ hourly: () => void
217
+ hourlyAt: (minute: number) => void
218
+ everyOddHour: () => void
219
+ everyHours: (hours: number) => void
220
+ everyTwoHours: () => void
221
+ everyThreeHours: () => void
222
+ everyFourHours: () => void
223
+ everySixHours: () => void
224
+ daily: () => void
225
+ dailyAt: (hour: number, minute: number) => void
226
+ everyDays: (days: number) => void
227
+ weekly: () => void
228
+ quarterly: () => void
229
+ yearly: () => void
230
+ cron: (interval: string, timezone?: string) => void
231
+ }
232
+
233
+ // function run(callback: Function): Scheduler {
234
+ function run(callback: ((now: Date | 'manual' | 'init') => void) | string): Scheduler {
235
+ return {
236
+ everySecond: () => {
237
+ cron.schedule('* * * * * *', callback)
238
+ },
239
+ everySeconds: (seconds = 1) => {
240
+ cron.schedule(`*/${seconds} * * * * *`, callback)
241
+ },
242
+ everyMinute: () => {
243
+ cron.schedule('* * * * *', callback)
244
+ },
245
+ everyTwoMinutes: () => {
246
+ cron.schedule('*/2 * * * *', callback)
247
+ },
248
+ everyThreeMinutes: () => {
249
+ cron.schedule('*/3 * * * *', callback)
250
+ },
251
+ everyFourMinutes: () => {
252
+ cron.schedule('*/4 * * * *', callback)
253
+ },
254
+ everyFiveMinutes: () => {
255
+ cron.schedule('*/5 * * * *', callback)
256
+ },
257
+ everyTenMinutes: () => {
258
+ cron.schedule('*/10 * * * *', callback)
259
+ },
260
+ everyFifteenMinutes: () => {
261
+ cron.schedule('*/15 * * * *', callback)
262
+ },
263
+ everyThirtyMinutes: () => {
264
+ cron.schedule('*/30 * * * *', callback)
265
+ },
266
+ everyMinutes: (minutes: number) => {
267
+ cron.schedule(`*/${minutes} * * * *`, callback)
268
+ },
269
+ hourly: () => {
270
+ cron.schedule('0 * * * *', callback)
271
+ },
272
+ everyHours: (hours: number) => {
273
+ cron.schedule(`0 */${hours} * * *`, callback)
274
+ },
275
+ hourlyAt: (minute: number) => {
276
+ cron.schedule(`${minute} * * * *`, callback)
277
+ },
278
+ everyOddHour: () => {
279
+ cron.schedule('0 */2 * * *', callback)
280
+ },
281
+ everyTwoHours: () => {
282
+ cron.schedule('0 */2 * * *', callback)
283
+ },
284
+ everyThreeHours: () => {
285
+ cron.schedule('0 */3 * * *', callback)
286
+ },
287
+ everyFourHours: () => {
288
+ cron.schedule('0 */4 * * *', callback)
289
+ },
290
+ everySixHours: () => {
291
+ cron.schedule('0 */6 * * *', callback)
292
+ },
293
+ dailyAt: (hour: number, minute: number) => {
294
+ cron.schedule(`${minute} ${hour} * * *`, callback)
295
+ },
296
+ daily: () => {
297
+ cron.schedule('0 0 * * *', callback)
298
+ },
299
+ everyDays: (days: number) => {
300
+ cron.schedule(`0 0 */${days} * *`, callback)
301
+ },
302
+ weekly: () => {
303
+ cron.schedule('0 0 * * 0', callback)
304
+ },
305
+ quarterly: () => {
306
+ cron.schedule('0 0 1 */3 *', callback)
307
+ },
308
+ yearly: () => {
309
+ cron.schedule('0 0 1 1 *', callback)
310
+ },
311
+ cron: (interval: string, timezone?: string) => {
312
+ timezone ? cron.schedule(interval, callback, { scheduled: true, timezone }) : cron.schedule(interval, callback)
313
+ },
314
+ }
315
+ }
316
+
317
+ export function useScheduler() {
318
+ return {
319
+ run,
320
+ }
321
+ }