@veloxts/scheduler 0.6.57 → 0.6.59
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 +2 -30
- package/dist/index.d.ts +14 -16
- package/dist/index.js +13 -16
- package/dist/task.d.ts +51 -5
- package/dist/task.js +59 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,36 +1,8 @@
|
|
|
1
1
|
# @veloxts/scheduler
|
|
2
2
|
|
|
3
|
-
> **Early Preview** - APIs may change
|
|
3
|
+
> **Early Preview (v0.6.x)** - APIs are stabilizing but may still change. Use with caution in production.
|
|
4
4
|
|
|
5
|
-
Cron-based task scheduling
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @veloxts/scheduler
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Quick Start
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { createScheduler, task } from '@veloxts/scheduler';
|
|
17
|
-
|
|
18
|
-
const scheduler = createScheduler([
|
|
19
|
-
task('cleanup', () => db.token.deleteExpired())
|
|
20
|
-
.daily()
|
|
21
|
-
.at('02:00')
|
|
22
|
-
.build(),
|
|
23
|
-
|
|
24
|
-
task('digest', () => sendDailyDigest())
|
|
25
|
-
.weekdays()
|
|
26
|
-
.at('09:00')
|
|
27
|
-
.build(),
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
scheduler.start();
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
See [GUIDE.md](./GUIDE.md) for detailed documentation.
|
|
5
|
+
Cron-based task scheduling for VeloxTS Framework - provides a fluent API for defining recurring tasks with overlap prevention and timezone support. Learn more at [@veloxts/velox](https://www.npmjs.com/package/@veloxts/velox).
|
|
34
6
|
|
|
35
7
|
## License
|
|
36
8
|
|
package/dist/index.d.ts
CHANGED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```typescript
|
|
9
|
-
* import { schedulerPlugin,
|
|
9
|
+
* import { schedulerPlugin, defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
10
10
|
*
|
|
11
|
-
* // Define scheduled tasks with fluent API
|
|
11
|
+
* // Define scheduled tasks with fluent API - .build() is auto-called
|
|
12
12
|
* const schedule = defineSchedule([
|
|
13
|
-
*
|
|
13
|
+
* defineTask('cleanup-expired-tokens', async () => {
|
|
14
14
|
* await db.token.deleteMany({
|
|
15
15
|
* where: { expiresAt: { lt: new Date() } }
|
|
16
16
|
* });
|
|
@@ -21,26 +21,23 @@
|
|
|
21
21
|
* .withoutOverlapping()
|
|
22
22
|
* .onSuccess((ctx, duration) => {
|
|
23
23
|
* console.log(`Cleanup completed in ${duration}ms`);
|
|
24
|
-
* })
|
|
25
|
-
* .build(),
|
|
24
|
+
* }),
|
|
26
25
|
*
|
|
27
|
-
*
|
|
26
|
+
* defineTask('send-daily-digest', async () => {
|
|
28
27
|
* await sendDailyDigest();
|
|
29
28
|
* })
|
|
30
29
|
* .daily()
|
|
31
30
|
* .at('09:00')
|
|
32
|
-
* .weekdays()
|
|
33
|
-
* .build(),
|
|
31
|
+
* .weekdays(),
|
|
34
32
|
*
|
|
35
|
-
*
|
|
33
|
+
* defineTask('backup-database', async () => {
|
|
36
34
|
* await runDatabaseBackup();
|
|
37
35
|
* })
|
|
38
36
|
* .weekly()
|
|
39
37
|
* .sundays()
|
|
40
38
|
* .at('03:00')
|
|
41
39
|
* .onSuccess(() => notifySlack('Backup complete'))
|
|
42
|
-
* .onFailure((ctx, error) => notifyPagerDuty(error))
|
|
43
|
-
* .build(),
|
|
40
|
+
* .onFailure((ctx, error) => notifyPagerDuty(error)),
|
|
44
41
|
* ]);
|
|
45
42
|
*
|
|
46
43
|
* // Register plugin
|
|
@@ -59,7 +56,8 @@
|
|
|
59
56
|
*/
|
|
60
57
|
export { createScheduler, scheduler } from './manager.js';
|
|
61
58
|
export { _resetStandaloneScheduler, getScheduler, getSchedulerFromInstance, schedulerPlugin, } from './plugin.js';
|
|
62
|
-
export {
|
|
59
|
+
export type { ScheduleInput } from './task.js';
|
|
60
|
+
export { defineSchedule, defineTask, schedule, task } from './task.js';
|
|
63
61
|
export type { DayOfWeek, DayOfWeekNumber, ScheduleConstraint, ScheduledTask, SchedulerManager, SchedulerOptions, SchedulerPluginOptions, TaskBuilder, TaskContext, TaskExecution, TaskFailureCallback, TaskHandler, TaskSkipCallback, TaskSuccessCallback, } from './types.js';
|
|
64
62
|
/**
|
|
65
63
|
* DI tokens and providers for @veloxts/scheduler
|
|
@@ -69,13 +67,13 @@ export type { DayOfWeek, DayOfWeekNumber, ScheduleConstraint, ScheduledTask, Sch
|
|
|
69
67
|
* @example
|
|
70
68
|
* ```typescript
|
|
71
69
|
* import { Container } from '@veloxts/core';
|
|
72
|
-
* import { registerSchedulerProviders, SCHEDULER_MANAGER,
|
|
70
|
+
* import { registerSchedulerProviders, SCHEDULER_MANAGER, defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
73
71
|
*
|
|
74
72
|
* const container = new Container();
|
|
75
73
|
* registerSchedulerProviders(container, {
|
|
76
|
-
* tasks: [
|
|
77
|
-
*
|
|
78
|
-
* ],
|
|
74
|
+
* tasks: defineSchedule([
|
|
75
|
+
* defineTask('cleanup', () => db.cleanup()).daily(),
|
|
76
|
+
* ]),
|
|
79
77
|
* });
|
|
80
78
|
*
|
|
81
79
|
* const scheduler = container.resolve(SCHEDULER_MANAGER);
|
package/dist/index.js
CHANGED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```typescript
|
|
9
|
-
* import { schedulerPlugin,
|
|
9
|
+
* import { schedulerPlugin, defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
10
10
|
*
|
|
11
|
-
* // Define scheduled tasks with fluent API
|
|
11
|
+
* // Define scheduled tasks with fluent API - .build() is auto-called
|
|
12
12
|
* const schedule = defineSchedule([
|
|
13
|
-
*
|
|
13
|
+
* defineTask('cleanup-expired-tokens', async () => {
|
|
14
14
|
* await db.token.deleteMany({
|
|
15
15
|
* where: { expiresAt: { lt: new Date() } }
|
|
16
16
|
* });
|
|
@@ -21,26 +21,23 @@
|
|
|
21
21
|
* .withoutOverlapping()
|
|
22
22
|
* .onSuccess((ctx, duration) => {
|
|
23
23
|
* console.log(`Cleanup completed in ${duration}ms`);
|
|
24
|
-
* })
|
|
25
|
-
* .build(),
|
|
24
|
+
* }),
|
|
26
25
|
*
|
|
27
|
-
*
|
|
26
|
+
* defineTask('send-daily-digest', async () => {
|
|
28
27
|
* await sendDailyDigest();
|
|
29
28
|
* })
|
|
30
29
|
* .daily()
|
|
31
30
|
* .at('09:00')
|
|
32
|
-
* .weekdays()
|
|
33
|
-
* .build(),
|
|
31
|
+
* .weekdays(),
|
|
34
32
|
*
|
|
35
|
-
*
|
|
33
|
+
* defineTask('backup-database', async () => {
|
|
36
34
|
* await runDatabaseBackup();
|
|
37
35
|
* })
|
|
38
36
|
* .weekly()
|
|
39
37
|
* .sundays()
|
|
40
38
|
* .at('03:00')
|
|
41
39
|
* .onSuccess(() => notifySlack('Backup complete'))
|
|
42
|
-
* .onFailure((ctx, error) => notifyPagerDuty(error))
|
|
43
|
-
* .build(),
|
|
40
|
+
* .onFailure((ctx, error) => notifyPagerDuty(error)),
|
|
44
41
|
* ]);
|
|
45
42
|
*
|
|
46
43
|
* // Register plugin
|
|
@@ -62,7 +59,7 @@ export { createScheduler, scheduler } from './manager.js';
|
|
|
62
59
|
// Plugin
|
|
63
60
|
export { _resetStandaloneScheduler, getScheduler, getSchedulerFromInstance, schedulerPlugin, } from './plugin.js';
|
|
64
61
|
// Task builder
|
|
65
|
-
export { defineSchedule, task } from './task.js';
|
|
62
|
+
export { defineSchedule, defineTask, schedule, task } from './task.js';
|
|
66
63
|
// ============================================================================
|
|
67
64
|
// Dependency Injection
|
|
68
65
|
// ============================================================================
|
|
@@ -74,13 +71,13 @@ export { defineSchedule, task } from './task.js';
|
|
|
74
71
|
* @example
|
|
75
72
|
* ```typescript
|
|
76
73
|
* import { Container } from '@veloxts/core';
|
|
77
|
-
* import { registerSchedulerProviders, SCHEDULER_MANAGER,
|
|
74
|
+
* import { registerSchedulerProviders, SCHEDULER_MANAGER, defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
78
75
|
*
|
|
79
76
|
* const container = new Container();
|
|
80
77
|
* registerSchedulerProviders(container, {
|
|
81
|
-
* tasks: [
|
|
82
|
-
*
|
|
83
|
-
* ],
|
|
78
|
+
* tasks: defineSchedule([
|
|
79
|
+
* defineTask('cleanup', () => db.cleanup()).daily(),
|
|
80
|
+
* ]),
|
|
84
81
|
* });
|
|
85
82
|
*
|
|
86
83
|
* const scheduler = container.resolve(SCHEDULER_MANAGER);
|
package/dist/task.d.ts
CHANGED
|
@@ -29,19 +29,65 @@ import type { ScheduledTask, TaskBuilder, TaskHandler } from './types.js';
|
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
31
|
export declare function task(name: string, handler: TaskHandler): TaskBuilder;
|
|
32
|
+
/**
|
|
33
|
+
* Alias for task() - provides consistency with defineJob() and defineMail().
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import { defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
38
|
+
*
|
|
39
|
+
* const cleanup = defineTask('cleanup-expired-tokens', async () => {
|
|
40
|
+
* await db.token.deleteMany({
|
|
41
|
+
* where: { expiresAt: { lt: new Date() } }
|
|
42
|
+
* });
|
|
43
|
+
* })
|
|
44
|
+
* .daily()
|
|
45
|
+
* .at('02:00')
|
|
46
|
+
* .build();
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const defineTask: typeof task;
|
|
50
|
+
/**
|
|
51
|
+
* Input type for defineSchedule - accepts either built tasks or builders.
|
|
52
|
+
*/
|
|
53
|
+
export type ScheduleInput = ScheduledTask | TaskBuilder;
|
|
32
54
|
/**
|
|
33
55
|
* Define a schedule with multiple tasks.
|
|
34
56
|
*
|
|
35
|
-
*
|
|
57
|
+
* Auto-calls `.build()` on TaskBuilder instances, allowing either:
|
|
58
|
+
* - Pre-built ScheduledTask objects
|
|
59
|
+
* - TaskBuilder instances (build() called automatically)
|
|
60
|
+
*
|
|
61
|
+
* @param tasks - Array of scheduled tasks or task builders
|
|
36
62
|
* @returns Array of scheduled tasks
|
|
37
63
|
*
|
|
38
64
|
* @example
|
|
39
65
|
* ```typescript
|
|
66
|
+
* // .build() is now optional - defineSchedule calls it automatically
|
|
40
67
|
* export const schedule = defineSchedule([
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
68
|
+
* defineTask('cleanup', () => db.cleanup()).daily().at('02:00'),
|
|
69
|
+
* defineTask('digest', () => sendDigest()).daily().at('09:00'),
|
|
70
|
+
* defineTask('backup', () => runBackup()).weekly().sundays().at('03:00'),
|
|
71
|
+
* ]);
|
|
72
|
+
*
|
|
73
|
+
* // Explicit .build() still works for backward compatibility
|
|
74
|
+
* export const schedule = defineSchedule([
|
|
75
|
+
* defineTask('cleanup', () => db.cleanup()).daily().at('02:00').build(),
|
|
76
|
+
* ]);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function defineSchedule(tasks: ScheduleInput[]): ScheduledTask[];
|
|
80
|
+
/**
|
|
81
|
+
* Alias for defineSchedule() - matches pattern of job/mail/task short aliases.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import { schedule, task } from '@veloxts/scheduler';
|
|
86
|
+
*
|
|
87
|
+
* const tasks = schedule([
|
|
88
|
+
* task('cleanup', () => db.cleanup()).daily().at('02:00'),
|
|
89
|
+
* task('digest', () => sendDigest()).daily().at('09:00'),
|
|
44
90
|
* ]);
|
|
45
91
|
* ```
|
|
46
92
|
*/
|
|
47
|
-
export declare
|
|
93
|
+
export declare const schedule: typeof defineSchedule;
|
package/dist/task.js
CHANGED
|
@@ -414,29 +414,82 @@ class TaskBuilderImpl {
|
|
|
414
414
|
export function task(name, handler) {
|
|
415
415
|
return new TaskBuilderImpl(name, handler);
|
|
416
416
|
}
|
|
417
|
+
/**
|
|
418
|
+
* Alias for task() - provides consistency with defineJob() and defineMail().
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```typescript
|
|
422
|
+
* import { defineTask, defineSchedule } from '@veloxts/scheduler';
|
|
423
|
+
*
|
|
424
|
+
* const cleanup = defineTask('cleanup-expired-tokens', async () => {
|
|
425
|
+
* await db.token.deleteMany({
|
|
426
|
+
* where: { expiresAt: { lt: new Date() } }
|
|
427
|
+
* });
|
|
428
|
+
* })
|
|
429
|
+
* .daily()
|
|
430
|
+
* .at('02:00')
|
|
431
|
+
* .build();
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
export const defineTask = task;
|
|
435
|
+
/**
|
|
436
|
+
* Type guard to check if value is a TaskBuilder (has build method).
|
|
437
|
+
*/
|
|
438
|
+
function isTaskBuilder(value) {
|
|
439
|
+
return (value !== null &&
|
|
440
|
+
typeof value === 'object' &&
|
|
441
|
+
'build' in value &&
|
|
442
|
+
typeof value.build === 'function');
|
|
443
|
+
}
|
|
417
444
|
/**
|
|
418
445
|
* Define a schedule with multiple tasks.
|
|
419
446
|
*
|
|
420
|
-
*
|
|
447
|
+
* Auto-calls `.build()` on TaskBuilder instances, allowing either:
|
|
448
|
+
* - Pre-built ScheduledTask objects
|
|
449
|
+
* - TaskBuilder instances (build() called automatically)
|
|
450
|
+
*
|
|
451
|
+
* @param tasks - Array of scheduled tasks or task builders
|
|
421
452
|
* @returns Array of scheduled tasks
|
|
422
453
|
*
|
|
423
454
|
* @example
|
|
424
455
|
* ```typescript
|
|
456
|
+
* // .build() is now optional - defineSchedule calls it automatically
|
|
425
457
|
* export const schedule = defineSchedule([
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
458
|
+
* defineTask('cleanup', () => db.cleanup()).daily().at('02:00'),
|
|
459
|
+
* defineTask('digest', () => sendDigest()).daily().at('09:00'),
|
|
460
|
+
* defineTask('backup', () => runBackup()).weekly().sundays().at('03:00'),
|
|
461
|
+
* ]);
|
|
462
|
+
*
|
|
463
|
+
* // Explicit .build() still works for backward compatibility
|
|
464
|
+
* export const schedule = defineSchedule([
|
|
465
|
+
* defineTask('cleanup', () => db.cleanup()).daily().at('02:00').build(),
|
|
429
466
|
* ]);
|
|
430
467
|
* ```
|
|
431
468
|
*/
|
|
432
469
|
export function defineSchedule(tasks) {
|
|
470
|
+
// Normalize: call build() on TaskBuilders, pass through ScheduledTasks
|
|
471
|
+
const builtTasks = tasks.map((t) => (isTaskBuilder(t) ? t.build() : t));
|
|
433
472
|
// Validate unique names
|
|
434
473
|
const names = new Set();
|
|
435
|
-
for (const t of
|
|
474
|
+
for (const t of builtTasks) {
|
|
436
475
|
if (names.has(t.name)) {
|
|
437
476
|
throw new Error(`Duplicate task name: "${t.name}". Task names must be unique.`);
|
|
438
477
|
}
|
|
439
478
|
names.add(t.name);
|
|
440
479
|
}
|
|
441
|
-
return
|
|
480
|
+
return builtTasks;
|
|
442
481
|
}
|
|
482
|
+
/**
|
|
483
|
+
* Alias for defineSchedule() - matches pattern of job/mail/task short aliases.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* import { schedule, task } from '@veloxts/scheduler';
|
|
488
|
+
*
|
|
489
|
+
* const tasks = schedule([
|
|
490
|
+
* task('cleanup', () => db.cleanup()).daily().at('02:00'),
|
|
491
|
+
* task('digest', () => sendDigest()).daily().at('09:00'),
|
|
492
|
+
* ]);
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
export const schedule = defineSchedule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/scheduler",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.59",
|
|
4
4
|
"description": "Task scheduling for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"cron": "3.5.0",
|
|
22
22
|
"fastify-plugin": "5.1.0",
|
|
23
|
-
"@veloxts/core": "0.6.
|
|
23
|
+
"@veloxts/core": "0.6.59"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"fastify": "^5.0.0"
|