@navios/schedule 0.4.0 → 0.7.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +7 -0
  3. package/README.md +221 -317
  4. package/dist/src/cron.constants.d.mts +25 -0
  5. package/dist/src/cron.constants.d.mts.map +1 -1
  6. package/dist/src/decorators/cron.decorator.d.mts +48 -0
  7. package/dist/src/decorators/cron.decorator.d.mts.map +1 -1
  8. package/dist/src/decorators/schedulable.decorator.d.mts +25 -0
  9. package/dist/src/decorators/schedulable.decorator.d.mts.map +1 -1
  10. package/dist/src/scheduler.service.d.mts +102 -0
  11. package/dist/src/scheduler.service.d.mts.map +1 -1
  12. package/dist/tsconfig.tsbuildinfo +1 -1
  13. package/lib/index.cjs +626 -0
  14. package/lib/index.cjs.map +1 -0
  15. package/lib/index.d.cts +264 -0
  16. package/lib/index.d.cts.map +1 -0
  17. package/lib/index.d.mts +264 -14
  18. package/lib/index.d.mts.map +1 -0
  19. package/lib/index.mjs +592 -209
  20. package/lib/index.mjs.map +1 -1
  21. package/package.json +6 -6
  22. package/project.json +2 -2
  23. package/src/__tests__/schedule.spec.mts +5 -3
  24. package/src/cron.constants.mts +25 -0
  25. package/src/decorators/cron.decorator.mts +48 -1
  26. package/src/decorators/schedulable.decorator.mts +25 -0
  27. package/src/scheduler.service.mts +105 -10
  28. package/tsdown.config.mts +33 -0
  29. package/dist/tsdown.config.d.mts +0 -3
  30. package/dist/tsdown.config.d.mts.map +0 -1
  31. package/dist/tsup.config.d.mts +0 -3
  32. package/dist/tsup.config.d.mts.map +0 -1
  33. package/dist/vitest.config.d.mts +0 -3
  34. package/dist/vitest.config.d.mts.map +0 -1
  35. package/lib/_tsup-dts-rollup.d.mts +0 -105
  36. package/lib/_tsup-dts-rollup.d.ts +0 -105
  37. package/lib/index.d.ts +0 -14
  38. package/lib/index.js +0 -240
  39. package/lib/index.js.map +0 -1
  40. package/tsup.config.mts +0 -12
@@ -1,6 +1,54 @@
1
1
  import type { CronJobParams } from 'cron';
2
+ /**
3
+ * Options for configuring a cron job.
4
+ *
5
+ * @public
6
+ */
2
7
  export interface CronOptions {
8
+ /**
9
+ * Whether the job should be disabled by default.
10
+ * Disabled jobs won't start automatically but can be manually started later.
11
+ *
12
+ * @default false
13
+ */
3
14
  disabled?: boolean;
4
15
  }
16
+ /**
17
+ * Decorator that marks a method to run on a cron schedule.
18
+ *
19
+ * The method must be in a class decorated with `@Schedulable()`.
20
+ * The method will be automatically executed according to the provided cron expression.
21
+ *
22
+ * @param cronTime - Cron expression (5 or 6 fields) or a pre-defined Schedule constant
23
+ * @param options - Optional configuration for the cron job
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * @Schedulable()
28
+ * class TaskService {
29
+ * // Run daily at midnight
30
+ * @Cron('0 0 * * *')
31
+ * async dailyTask() {
32
+ * console.log('Running daily task')
33
+ * }
34
+ *
35
+ * // Use pre-defined schedule
36
+ * @Cron(Schedule.EveryFiveMinutes)
37
+ * async frequentTask() {
38
+ * console.log('Running every 5 minutes')
39
+ * }
40
+ *
41
+ * // Disabled job (won't start automatically)
42
+ * @Cron('0 2 * * *', { disabled: true })
43
+ * async maintenanceTask() {
44
+ * console.log('Maintenance task')
45
+ * }
46
+ * }
47
+ * ```
48
+ *
49
+ * @throws {Error} If applied to something other than a method
50
+ *
51
+ * @public
52
+ */
5
53
  export declare function Cron(cronTime: CronJobParams['cronTime'], options?: CronOptions): (target: () => Promise<void>, context: ClassMethodDecoratorContext) => () => Promise<void>;
6
54
  //# sourceMappingURL=cron.decorator.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cron.decorator.d.mts","sourceRoot":"","sources":["../../../src/decorators/cron.decorator.mts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,MAAM,CAAA;AAIzC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,wBAAgB,IAAI,CAClB,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,EAAE,WAAW,IAGnB,QAAQ,MAAM,OAAO,CAAC,IAAI,CAAC,EAC3B,SAAS,2BAA2B,WADtB,OAAO,CAAC,IAAI,CAAC,CAe9B"}
1
+ {"version":3,"file":"cron.decorator.d.mts","sourceRoot":"","sources":["../../../src/decorators/cron.decorator.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,MAAM,CAAA;AAIzC;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,IAAI,CAClB,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,EACnC,OAAO,CAAC,EAAE,WAAW,IAGnB,QAAQ,MAAM,OAAO,CAAC,IAAI,CAAC,EAC3B,SAAS,2BAA2B,WADtB,OAAO,CAAC,IAAI,CAAC,CAe9B"}
@@ -1,3 +1,28 @@
1
1
  import type { ClassType } from '@navios/core';
2
+ /**
3
+ * Decorator that marks a class as schedulable and makes it injectable.
4
+ *
5
+ * Classes decorated with `@Schedulable()` can contain methods decorated with `@Cron()`
6
+ * that will be automatically scheduled and executed. This decorator also applies
7
+ * the `@Injectable()` decorator, making the class available for dependency injection.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * @Schedulable()
12
+ * class TaskService {
13
+ * @Cron('0 0 * * *')
14
+ * async dailyTask() {
15
+ * // This will run daily at midnight
16
+ * }
17
+ * }
18
+ *
19
+ * // Register the service
20
+ * schedulerService.register(TaskService)
21
+ * ```
22
+ *
23
+ * @throws {Error} If applied to something other than a class
24
+ *
25
+ * @public
26
+ */
2
27
  export declare function Schedulable(): (target: ClassType, context: ClassDecoratorContext) => ClassType;
3
28
  //# sourceMappingURL=schedulable.decorator.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schedulable.decorator.d.mts","sourceRoot":"","sources":["../../../src/decorators/schedulable.decorator.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAM7C,wBAAgB,WAAW,KACjB,QAAQ,SAAS,EAAE,SAAS,qBAAqB,eAW1D"}
1
+ {"version":3,"file":"schedulable.decorator.d.mts","sourceRoot":"","sources":["../../../src/decorators/schedulable.decorator.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAM7C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,KACjB,QAAQ,SAAS,EAAE,SAAS,qBAAqB,eAW1D"}
@@ -1,12 +1,114 @@
1
1
  import type { ClassType } from '@navios/core';
2
2
  import { CronJob } from 'cron';
3
+ /**
4
+ * Service responsible for managing and executing scheduled cron jobs.
5
+ *
6
+ * The SchedulerService registers schedulable services decorated with `@Schedulable()`
7
+ * and automatically starts their cron jobs based on the `@Cron()` decorator configuration.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { inject, Injectable } from '@navios/core'
12
+ * import { SchedulerService } from '@navios/schedule'
13
+ *
14
+ * @Injectable()
15
+ * class AppModule {
16
+ * private readonly scheduler = inject(SchedulerService)
17
+ *
18
+ * async onModuleInit() {
19
+ * this.scheduler.register(MySchedulableService)
20
+ * }
21
+ * }
22
+ * ```
23
+ *
24
+ * @public
25
+ */
3
26
  export declare class SchedulerService {
4
27
  private readonly logger;
28
+ private readonly container;
5
29
  private readonly jobs;
30
+ /**
31
+ * Registers a schedulable service and starts all its cron jobs.
32
+ *
33
+ * The service must be decorated with `@Schedulable()` and contain methods
34
+ * decorated with `@Cron()` to be registered successfully.
35
+ *
36
+ * @param service - The schedulable service class to register
37
+ * @throws {Error} If the service is not decorated with `@Schedulable()`
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * @Schedulable()
42
+ * class TaskService {
43
+ * @Cron('0 0 * * *')
44
+ * async dailyTask() {
45
+ * // Runs daily at midnight
46
+ * }
47
+ * }
48
+ *
49
+ * schedulerService.register(TaskService)
50
+ * ```
51
+ *
52
+ * @public
53
+ */
6
54
  register(service: ClassType): void;
55
+ /**
56
+ * Retrieves a specific cron job instance for a method in a schedulable service.
57
+ *
58
+ * @param service - The schedulable service class
59
+ * @param method - The name of the method decorated with `@Cron()`
60
+ * @returns The CronJob instance if found, undefined otherwise
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const job = schedulerService.getJob(TaskService, 'dailyTask')
65
+ * if (job) {
66
+ * console.log('Job is active:', job.isActive)
67
+ * job.start() // Manually start the job
68
+ * job.stop() // Manually stop the job
69
+ * }
70
+ * ```
71
+ *
72
+ * @public
73
+ */
7
74
  getJob<T extends ClassType>(service: T, method: keyof InstanceType<T>): CronJob | undefined;
8
75
  private registerJobs;
76
+ /**
77
+ * Starts all registered cron jobs that are currently inactive.
78
+ *
79
+ * Only jobs that are not already active will be started. This method
80
+ * is useful for resuming all jobs after calling `stopAll()`.
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * // Stop all jobs
85
+ * schedulerService.stopAll()
86
+ *
87
+ * // Later, resume all jobs
88
+ * schedulerService.startAll()
89
+ * ```
90
+ *
91
+ * @public
92
+ */
9
93
  startAll(): void;
94
+ /**
95
+ * Stops all registered cron jobs that are currently active.
96
+ *
97
+ * Only jobs that are currently active will be stopped. This method
98
+ * is useful for pausing all scheduled tasks, for example during
99
+ * application shutdown or maintenance.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * // Pause all scheduled jobs
104
+ * schedulerService.stopAll()
105
+ *
106
+ * // Jobs can be resumed later with startAll()
107
+ * schedulerService.startAll()
108
+ * ```
109
+ *
110
+ * @public
111
+ */
10
112
  stopAll(): void;
11
113
  }
12
114
  //# sourceMappingURL=scheduler.service.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduler.service.d.mts","sourceRoot":"","sources":["../../src/scheduler.service.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAU7C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAS9B,qBACa,gBAAgB;IAE3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAErB;IACF,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkC;IAEvD,QAAQ,CAAC,OAAO,EAAE,SAAS;IAW3B,MAAM,CAAC,CAAC,SAAS,SAAS,EACxB,OAAO,EAAE,CAAC,EACV,MAAM,EAAE,MAAM,YAAY,CAAC,CAAC,CAAC,GAC5B,OAAO,GAAG,SAAS;IAMtB,OAAO,CAAC,YAAY;IA4BpB,QAAQ;IASR,OAAO;CAQR"}
1
+ {"version":3,"file":"scheduler.service.d.mts","sourceRoot":"","sources":["../../src/scheduler.service.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAI7C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAS9B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBACa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAErB;IACF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAoB;IAC9C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkC;IAEvD;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,QAAQ,CAAC,OAAO,EAAE,SAAS;IAW3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,CAAC,SAAS,SAAS,EACxB,OAAO,EAAE,CAAC,EACV,MAAM,EAAE,MAAM,YAAY,CAAC,CAAC,CAAC,GAC5B,OAAO,GAAG,SAAS;IAMtB,OAAO,CAAC,YAAY;IA4BpB;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ;IASR;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO;CAQR"}