@cleverbrush/scheduler 1.0.0-beta.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/README.md +1 -0
- package/dist/CloneReadableStream.d.ts +9 -0
- package/dist/CloneReadableStream.d.ts.map +1 -0
- package/dist/CloneReadableStream.js +25 -0
- package/dist/CloneReadableStream.js.map +1 -0
- package/dist/ScheduleCalculator.d.ts +10 -0
- package/dist/ScheduleCalculator.d.ts.map +1 -0
- package/dist/ScheduleCalculator.js +208 -0
- package/dist/ScheduleCalculator.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +307 -0
- package/dist/index.js.map +1 -0
- package/dist/jobRepository.d.ts +32 -0
- package/dist/jobRepository.d.ts.map +1 -0
- package/dist/jobRepository.js +84 -0
- package/dist/jobRepository.js.map +1 -0
- package/dist/reports.d.ts +0 -0
- package/dist/reports.js +231 -0
- package/dist/types.d.ts +577 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +122 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -0
- package/src/ScheduleCalculator.test.ts +482 -0
- package/src/ScheduleCalculator.ts +355 -0
- package/src/index.ts +419 -0
- package/src/jobRepository.ts +135 -0
- package/src/types.ts +219 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { Schedule } from './types.js';
|
|
2
|
+
|
|
3
|
+
const MS_IN_DAY = 1000 * 60 * 60 * 24;
|
|
4
|
+
const MS_IN_WEEK = MS_IN_DAY * 7;
|
|
5
|
+
|
|
6
|
+
const getDayOfWeek = (date: Date) => {
|
|
7
|
+
const res = date.getUTCDay();
|
|
8
|
+
if (res === 0) return 7;
|
|
9
|
+
return res;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const getNumberOfDaysInMonth = (date: Date) => {
|
|
13
|
+
return new Date(
|
|
14
|
+
Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0, 0, 0, 0, 0)
|
|
15
|
+
).getDate();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class ScheduleCalculator {
|
|
19
|
+
#schedule: Schedule;
|
|
20
|
+
#currentDate = new Date();
|
|
21
|
+
#hour = 9;
|
|
22
|
+
#minute = 0;
|
|
23
|
+
#maxRepeat = -1;
|
|
24
|
+
#repeatCount = 0;
|
|
25
|
+
|
|
26
|
+
#hasNext = false;
|
|
27
|
+
#next: Date | undefined;
|
|
28
|
+
|
|
29
|
+
constructor(schedule: Schedule) {
|
|
30
|
+
if (!schedule) throw new Error('schedule is required');
|
|
31
|
+
this.#schedule = { ...schedule };
|
|
32
|
+
|
|
33
|
+
if (typeof schedule.startsOn !== 'undefined') {
|
|
34
|
+
this.#currentDate = schedule.startsOn;
|
|
35
|
+
} else {
|
|
36
|
+
this.#schedule.startsOn = new Date();
|
|
37
|
+
this.#currentDate = this.#schedule.startsOn;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (schedule.every !== 'minute') {
|
|
41
|
+
if (typeof schedule.hour === 'number') {
|
|
42
|
+
this.#hour = schedule.hour;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof schedule.minute === 'number') {
|
|
46
|
+
this.#minute = schedule.minute;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
schedule.every === 'day' &&
|
|
51
|
+
new Date(
|
|
52
|
+
Date.UTC(
|
|
53
|
+
this.#currentDate.getUTCFullYear(),
|
|
54
|
+
this.#currentDate.getUTCMonth(),
|
|
55
|
+
this.#currentDate.getUTCDate(),
|
|
56
|
+
this.#hour,
|
|
57
|
+
this.#minute,
|
|
58
|
+
0,
|
|
59
|
+
0
|
|
60
|
+
)
|
|
61
|
+
).getTime() < this.#currentDate.getTime()
|
|
62
|
+
) {
|
|
63
|
+
const date = new Date(
|
|
64
|
+
Date.UTC(
|
|
65
|
+
this.#currentDate.getUTCFullYear(),
|
|
66
|
+
this.#currentDate.getUTCMonth(),
|
|
67
|
+
this.#currentDate.getUTCDate() + 1,
|
|
68
|
+
this.#hour,
|
|
69
|
+
this.#minute,
|
|
70
|
+
0,
|
|
71
|
+
0
|
|
72
|
+
)
|
|
73
|
+
);
|
|
74
|
+
this.#currentDate = date;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (typeof schedule.maxOccurences === 'number') {
|
|
79
|
+
this.#maxRepeat = schedule.maxOccurences;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const next = this.#getNext();
|
|
83
|
+
|
|
84
|
+
if (typeof next !== 'undefined') {
|
|
85
|
+
this.#next = next;
|
|
86
|
+
this.#hasNext = true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let leftToSkip = this.#schedule.startingFromIndex || 1;
|
|
90
|
+
|
|
91
|
+
while (leftToSkip-- > 1 && this.#hasNext) {
|
|
92
|
+
this.next();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#getNext(): Date | undefined {
|
|
97
|
+
let candidate: Date | null = null;
|
|
98
|
+
let dayOfWeek: number;
|
|
99
|
+
|
|
100
|
+
switch (this.#schedule.every) {
|
|
101
|
+
case 'minute':
|
|
102
|
+
candidate =
|
|
103
|
+
this.#repeatCount === 0
|
|
104
|
+
? this.#schedule.startsOn
|
|
105
|
+
: new Date(
|
|
106
|
+
Date.UTC(
|
|
107
|
+
this.#currentDate.getUTCFullYear(),
|
|
108
|
+
this.#currentDate.getUTCMonth(),
|
|
109
|
+
this.#currentDate.getUTCDate(),
|
|
110
|
+
this.#currentDate.getUTCHours(),
|
|
111
|
+
this.#currentDate.getUTCMinutes() +
|
|
112
|
+
this.#schedule.interval,
|
|
113
|
+
this.#currentDate.getUTCSeconds(),
|
|
114
|
+
this.#currentDate.getUTCMilliseconds()
|
|
115
|
+
)
|
|
116
|
+
);
|
|
117
|
+
break;
|
|
118
|
+
case 'day':
|
|
119
|
+
{
|
|
120
|
+
const date = new Date(
|
|
121
|
+
this.#currentDate.getTime() +
|
|
122
|
+
MS_IN_DAY *
|
|
123
|
+
(this.#repeatCount === 0
|
|
124
|
+
? 0
|
|
125
|
+
: this.#schedule.interval - 1)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
candidate = new Date(
|
|
129
|
+
Date.UTC(
|
|
130
|
+
date.getUTCFullYear(),
|
|
131
|
+
date.getUTCMonth(),
|
|
132
|
+
date.getUTCDate(),
|
|
133
|
+
this.#hour,
|
|
134
|
+
this.#minute,
|
|
135
|
+
0,
|
|
136
|
+
0
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
case 'week':
|
|
142
|
+
{
|
|
143
|
+
let date = this.#currentDate;
|
|
144
|
+
|
|
145
|
+
do {
|
|
146
|
+
let found = false;
|
|
147
|
+
dayOfWeek = getDayOfWeek(date);
|
|
148
|
+
if (Number.isNaN(dayOfWeek)) return;
|
|
149
|
+
for (let i = 0; i < 7 - dayOfWeek + 1; i++) {
|
|
150
|
+
date = new Date(
|
|
151
|
+
date.getTime() + (i == 0 ? 0 : MS_IN_DAY)
|
|
152
|
+
);
|
|
153
|
+
if (
|
|
154
|
+
this.#schedule.endsOn &&
|
|
155
|
+
date > this.#schedule.endsOn
|
|
156
|
+
) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (
|
|
160
|
+
this.#schedule.dayOfWeek.includes(
|
|
161
|
+
getDayOfWeek(date)
|
|
162
|
+
)
|
|
163
|
+
) {
|
|
164
|
+
const dateWithTime = new Date(
|
|
165
|
+
Date.UTC(
|
|
166
|
+
date.getUTCFullYear(),
|
|
167
|
+
date.getUTCMonth(),
|
|
168
|
+
date.getUTCDate(),
|
|
169
|
+
this.#hour,
|
|
170
|
+
this.#minute,
|
|
171
|
+
0,
|
|
172
|
+
0
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
if (dateWithTime > this.#schedule.startsOn) {
|
|
176
|
+
candidate = dateWithTime;
|
|
177
|
+
found = true;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (found) break;
|
|
184
|
+
|
|
185
|
+
date = new Date(
|
|
186
|
+
date.getTime() +
|
|
187
|
+
MS_IN_DAY +
|
|
188
|
+
(this.#repeatCount == 0
|
|
189
|
+
? 0
|
|
190
|
+
: (this.#schedule.interval - 1) *
|
|
191
|
+
MS_IN_WEEK)
|
|
192
|
+
);
|
|
193
|
+
} while (
|
|
194
|
+
(this.#schedule.endsOn &&
|
|
195
|
+
date <= this.#schedule.endsOn) ||
|
|
196
|
+
!this.#schedule.endsOn
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
case 'month':
|
|
201
|
+
{
|
|
202
|
+
let dateTime;
|
|
203
|
+
const cDate = this.#currentDate;
|
|
204
|
+
let iteration = 0;
|
|
205
|
+
do {
|
|
206
|
+
const date =
|
|
207
|
+
this.#schedule.day === 'last'
|
|
208
|
+
? getNumberOfDaysInMonth(
|
|
209
|
+
new Date(
|
|
210
|
+
Date.UTC(
|
|
211
|
+
cDate.getUTCFullYear(),
|
|
212
|
+
cDate.getUTCMonth() +
|
|
213
|
+
iteration *
|
|
214
|
+
(this.#repeatCount === 0
|
|
215
|
+
? 1
|
|
216
|
+
: this.#schedule
|
|
217
|
+
.interval),
|
|
218
|
+
1,
|
|
219
|
+
0,
|
|
220
|
+
0,
|
|
221
|
+
0,
|
|
222
|
+
0
|
|
223
|
+
)
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
: (this.#schedule.day as number);
|
|
227
|
+
dateTime = new Date(
|
|
228
|
+
Date.UTC(
|
|
229
|
+
cDate.getUTCFullYear(),
|
|
230
|
+
cDate.getUTCMonth() +
|
|
231
|
+
iteration *
|
|
232
|
+
(this.#repeatCount === 0
|
|
233
|
+
? 1
|
|
234
|
+
: this.#schedule.interval),
|
|
235
|
+
date,
|
|
236
|
+
this.#hour,
|
|
237
|
+
this.#minute,
|
|
238
|
+
0,
|
|
239
|
+
0
|
|
240
|
+
)
|
|
241
|
+
);
|
|
242
|
+
iteration++;
|
|
243
|
+
} while (
|
|
244
|
+
dateTime < this.#schedule.startsOn ||
|
|
245
|
+
dateTime <= this.#currentDate
|
|
246
|
+
);
|
|
247
|
+
candidate = dateTime;
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
case 'year':
|
|
251
|
+
{
|
|
252
|
+
let dateTime;
|
|
253
|
+
const cDate = this.#currentDate;
|
|
254
|
+
let iteration = 0;
|
|
255
|
+
do {
|
|
256
|
+
const date =
|
|
257
|
+
this.#schedule.day === 'last'
|
|
258
|
+
? getNumberOfDaysInMonth(
|
|
259
|
+
new Date(
|
|
260
|
+
Date.UTC(
|
|
261
|
+
cDate.getUTCFullYear() +
|
|
262
|
+
iteration *
|
|
263
|
+
(this.#repeatCount === 0
|
|
264
|
+
? 1
|
|
265
|
+
: this.#schedule
|
|
266
|
+
.interval),
|
|
267
|
+
this.#schedule.month - 1,
|
|
268
|
+
1,
|
|
269
|
+
0,
|
|
270
|
+
0,
|
|
271
|
+
0,
|
|
272
|
+
0
|
|
273
|
+
)
|
|
274
|
+
)
|
|
275
|
+
)
|
|
276
|
+
: (this.#schedule.day as number);
|
|
277
|
+
dateTime = new Date(
|
|
278
|
+
Date.UTC(
|
|
279
|
+
cDate.getUTCFullYear() +
|
|
280
|
+
iteration *
|
|
281
|
+
(this.#repeatCount === 0
|
|
282
|
+
? 1
|
|
283
|
+
: this.#schedule.interval),
|
|
284
|
+
this.#schedule.month - 1,
|
|
285
|
+
date,
|
|
286
|
+
this.#hour,
|
|
287
|
+
this.#minute,
|
|
288
|
+
0,
|
|
289
|
+
0
|
|
290
|
+
)
|
|
291
|
+
);
|
|
292
|
+
iteration++;
|
|
293
|
+
} while (
|
|
294
|
+
dateTime < this.#schedule.startsOn ||
|
|
295
|
+
dateTime <= this.#currentDate
|
|
296
|
+
);
|
|
297
|
+
candidate = dateTime;
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
default:
|
|
301
|
+
throw new Error('unknown schedule type');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (!candidate) return;
|
|
305
|
+
|
|
306
|
+
if (
|
|
307
|
+
typeof this.#schedule.endsOn !== 'undefined' &&
|
|
308
|
+
candidate > this.#schedule.endsOn
|
|
309
|
+
) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return candidate;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public hasNext(span?: number): boolean {
|
|
317
|
+
if (!this.#hasNext) {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (typeof span !== 'number') return this.#hasNext;
|
|
322
|
+
|
|
323
|
+
return this.#next.getTime() - new Date().getTime() <= span;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
public next(): {
|
|
327
|
+
date: Date;
|
|
328
|
+
index: number;
|
|
329
|
+
} {
|
|
330
|
+
if (!this.#hasNext) throw new Error('schedule is over');
|
|
331
|
+
|
|
332
|
+
const result = this.#next as Date;
|
|
333
|
+
|
|
334
|
+
this.#currentDate = new Date(
|
|
335
|
+
result.getTime() +
|
|
336
|
+
(['day', 'week'].includes(this.#schedule.every) ? MS_IN_DAY : 0)
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
this.#repeatCount++;
|
|
340
|
+
const next = this.#getNext();
|
|
341
|
+
|
|
342
|
+
this.#next = next as Date;
|
|
343
|
+
this.#hasNext = typeof next !== 'undefined';
|
|
344
|
+
|
|
345
|
+
if (this.#maxRepeat > 0 && this.#repeatCount >= this.#maxRepeat) {
|
|
346
|
+
this.#next = undefined;
|
|
347
|
+
this.#hasNext = false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return {
|
|
351
|
+
date: result,
|
|
352
|
+
index: this.#repeatCount
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|