@zudojs/scheduler 0.0.1 → 1.0.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 (104) hide show
  1. package/README.md +170 -16
  2. package/dist/index.d.ts +7 -3
  3. package/dist/index.js +4 -3
  4. package/dist/scheduler/constants/index.d.ts +1 -1
  5. package/dist/scheduler/constants/index.js +1 -1
  6. package/dist/scheduler/constants/schedulerConstants.core.d.ts +0 -4
  7. package/dist/scheduler/constants/schedulerConstants.core.js +0 -4
  8. package/dist/scheduler/duration/duration.parser.d.ts +13 -5
  9. package/dist/scheduler/duration/duration.parser.js +54 -19
  10. package/dist/scheduler/errors/index.d.ts +1 -1
  11. package/dist/scheduler/errors/index.js +1 -1
  12. package/dist/scheduler/errors/scheduler.errors.d.ts +7 -1
  13. package/dist/scheduler/errors/scheduler.errors.js +7 -1
  14. package/dist/scheduler/executor/index.d.ts +1 -1
  15. package/dist/scheduler/executor/index.js +1 -1
  16. package/dist/scheduler/executor/jobExecutor.core.d.ts +24 -4
  17. package/dist/scheduler/executor/jobExecutor.core.js +151 -28
  18. package/dist/scheduler/job/jobOptions.type.d.ts +11 -0
  19. package/dist/scheduler/priorityQueue/schedulerPriorityQueue.core.d.ts +12 -0
  20. package/dist/scheduler/priorityQueue/schedulerPriorityQueue.core.js +32 -5
  21. package/dist/scheduler/registry/jobRegistry.core.d.ts +8 -0
  22. package/dist/scheduler/registry/jobRegistry.core.js +14 -0
  23. package/dist/scheduler/schedule/schedule.type.d.ts +11 -6
  24. package/dist/scheduler/schedule/schedule.type.js +10 -1
  25. package/dist/scheduler/schedule/scheduleOptions.type.d.ts +24 -1
  26. package/dist/scheduler/scheduleHandle/index.d.ts +1 -0
  27. package/dist/scheduler/scheduleHandle/scheduleHandle.type.d.ts +26 -3
  28. package/dist/scheduler/scheduleHandle/scheduleHandle.type.js +23 -7
  29. package/dist/scheduler/scheduler.core.d.ts +120 -11
  30. package/dist/scheduler/scheduler.core.js +424 -53
  31. package/dist/scheduler/trigger/cron.parser.d.ts +44 -0
  32. package/dist/scheduler/trigger/cron.parser.js +256 -0
  33. package/dist/scheduler/trigger/index.d.ts +2 -0
  34. package/dist/scheduler/trigger/index.js +1 -0
  35. package/dist/scheduler/trigger/schedulerTrigger.core.d.ts +22 -1
  36. package/dist/scheduler/trigger/schedulerTrigger.core.js +69 -6
  37. package/dist/scheduler/types/index.d.ts +12 -1
  38. package/dist/scheduler/types/index.js +7 -0
  39. package/dist/scheduler/types/schedulerTypes.core.d.ts +7 -87
  40. package/dist/scheduler/types/schedulerTypes.core.js +7 -1
  41. package/package.json +15 -8
  42. package/dist/.tsbuildinfo +0 -1
  43. package/dist/index.d.ts.map +0 -1
  44. package/dist/index.js.map +0 -1
  45. package/dist/scheduler/clock/index.d.ts.map +0 -1
  46. package/dist/scheduler/clock/index.js.map +0 -1
  47. package/dist/scheduler/clock/schedulerClock.type.d.ts.map +0 -1
  48. package/dist/scheduler/clock/schedulerClock.type.js.map +0 -1
  49. package/dist/scheduler/constants/index.d.ts.map +0 -1
  50. package/dist/scheduler/constants/index.js.map +0 -1
  51. package/dist/scheduler/constants/schedulerConstants.core.d.ts.map +0 -1
  52. package/dist/scheduler/constants/schedulerConstants.core.js.map +0 -1
  53. package/dist/scheduler/duration/duration.parser.d.ts.map +0 -1
  54. package/dist/scheduler/duration/duration.parser.js.map +0 -1
  55. package/dist/scheduler/duration/index.d.ts.map +0 -1
  56. package/dist/scheduler/duration/index.js.map +0 -1
  57. package/dist/scheduler/errors/index.d.ts.map +0 -1
  58. package/dist/scheduler/errors/index.js.map +0 -1
  59. package/dist/scheduler/errors/scheduler.errors.d.ts.map +0 -1
  60. package/dist/scheduler/errors/scheduler.errors.js.map +0 -1
  61. package/dist/scheduler/executor/index.d.ts.map +0 -1
  62. package/dist/scheduler/executor/index.js.map +0 -1
  63. package/dist/scheduler/executor/jobExecutor.core.d.ts.map +0 -1
  64. package/dist/scheduler/executor/jobExecutor.core.js.map +0 -1
  65. package/dist/scheduler/job/index.d.ts.map +0 -1
  66. package/dist/scheduler/job/index.js.map +0 -1
  67. package/dist/scheduler/job/jobContext.type.d.ts.map +0 -1
  68. package/dist/scheduler/job/jobContext.type.js.map +0 -1
  69. package/dist/scheduler/job/jobDefinition.type.d.ts.map +0 -1
  70. package/dist/scheduler/job/jobDefinition.type.js.map +0 -1
  71. package/dist/scheduler/job/jobHandler.type.d.ts.map +0 -1
  72. package/dist/scheduler/job/jobHandler.type.js.map +0 -1
  73. package/dist/scheduler/job/jobOptions.type.d.ts.map +0 -1
  74. package/dist/scheduler/job/jobOptions.type.js.map +0 -1
  75. package/dist/scheduler/priorityQueue/index.d.ts.map +0 -1
  76. package/dist/scheduler/priorityQueue/index.js.map +0 -1
  77. package/dist/scheduler/priorityQueue/schedulerPriorityQueue.core.d.ts.map +0 -1
  78. package/dist/scheduler/priorityQueue/schedulerPriorityQueue.core.js.map +0 -1
  79. package/dist/scheduler/registry/index.d.ts.map +0 -1
  80. package/dist/scheduler/registry/index.js.map +0 -1
  81. package/dist/scheduler/registry/jobRegistry.core.d.ts.map +0 -1
  82. package/dist/scheduler/registry/jobRegistry.core.js.map +0 -1
  83. package/dist/scheduler/schedule/index.d.ts.map +0 -1
  84. package/dist/scheduler/schedule/index.js.map +0 -1
  85. package/dist/scheduler/schedule/schedule.type.d.ts.map +0 -1
  86. package/dist/scheduler/schedule/schedule.type.js.map +0 -1
  87. package/dist/scheduler/schedule/scheduleOptions.type.d.ts.map +0 -1
  88. package/dist/scheduler/schedule/scheduleOptions.type.js.map +0 -1
  89. package/dist/scheduler/scheduleHandle/index.d.ts.map +0 -1
  90. package/dist/scheduler/scheduleHandle/index.js.map +0 -1
  91. package/dist/scheduler/scheduleHandle/scheduleHandle.type.d.ts.map +0 -1
  92. package/dist/scheduler/scheduleHandle/scheduleHandle.type.js.map +0 -1
  93. package/dist/scheduler/scheduler.core.d.ts.map +0 -1
  94. package/dist/scheduler/scheduler.core.js.map +0 -1
  95. package/dist/scheduler/trigger/index.d.ts.map +0 -1
  96. package/dist/scheduler/trigger/index.js.map +0 -1
  97. package/dist/scheduler/trigger/schedulerTrigger.core.d.ts.map +0 -1
  98. package/dist/scheduler/trigger/schedulerTrigger.core.js.map +0 -1
  99. package/dist/scheduler/trigger/trigger.type.d.ts.map +0 -1
  100. package/dist/scheduler/trigger/trigger.type.js.map +0 -1
  101. package/dist/scheduler/types/index.d.ts.map +0 -1
  102. package/dist/scheduler/types/index.js.map +0 -1
  103. package/dist/scheduler/types/schedulerTypes.core.d.ts.map +0 -1
  104. package/dist/scheduler/types/schedulerTypes.core.js.map +0 -1
@@ -1,11 +1,42 @@
1
+ import type { JobExecution } from "./types/schedulerTypes.core.js";
1
2
  import type { JobDefinition } from "./job/jobDefinition.type.js";
3
+ import type { Schedule } from "./schedule/schedule.type.js";
4
+ import type { ScheduleOptions } from "./schedule/scheduleOptions.type.js";
2
5
  import type { ScheduleHandle } from "./scheduleHandle/scheduleHandle.type.js";
3
6
  import type { Clock } from "./clock/schedulerClock.type.js";
4
7
  import { JobRegistry } from "./registry/jobRegistry.core.js";
5
8
  import { JobExecutor } from "./executor/jobExecutor.core.js";
6
9
  import { PriorityQueue } from "./priorityQueue/schedulerPriorityQueue.core.js";
10
+ /** Details reported to an error listener when a job fails. */
11
+ export interface SchedulerErrorEvent {
12
+ readonly scheduleId: string;
13
+ readonly jobId: string;
14
+ readonly executionId: string;
15
+ readonly error: unknown;
16
+ }
17
+ /** Options accepted by {@link Scheduler}. */
18
+ export interface SchedulerOptions {
19
+ readonly jobs?: JobRegistry;
20
+ readonly executor?: JobExecutor;
21
+ readonly queue?: PriorityQueue;
22
+ readonly clock?: Clock;
23
+ /** Maximum job executions in flight at once (default: 10). */
24
+ readonly maxConcurrency?: number;
25
+ /** Called when a job execution fails. */
26
+ readonly onError?: (event: SchedulerErrorEvent) => void;
27
+ }
7
28
  /**
8
29
  * Scheduler for time-based job execution.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const scheduler = new Scheduler({ onError: (e) => log.error(e) });
34
+ * scheduler.define({ id: "cleanup", name: "Cleanup", handler: run });
35
+ * scheduler.every("5m", "cleanup");
36
+ * scheduler.start();
37
+ * // …later
38
+ * await scheduler.stop();
39
+ * ```
9
40
  */
10
41
  export declare class Scheduler {
11
42
  private readonly jobs;
@@ -13,17 +44,52 @@ export declare class Scheduler {
13
44
  private readonly queue;
14
45
  private readonly clock;
15
46
  private readonly schedules;
47
+ private readonly maxConcurrency;
48
+ private readonly onError;
49
+ /** Executions currently in flight, across all schedules. */
50
+ private readonly inFlight;
51
+ /**
52
+ * Abort controllers for every in-flight execution.
53
+ *
54
+ * Tracked here as well as on the schedule record: a one-shot schedule is
55
+ * retired as soon as it is dispatched, so by the time `stop()` runs its
56
+ * record is already gone and a per-record set alone would have nothing left
57
+ * to abort.
58
+ */
59
+ private readonly runningControllers;
60
+ /** Executions in flight per job id, for the per-job concurrency ceiling. */
61
+ private readonly runningByJob;
62
+ /** Records with `pendingRuns > 0`, drained as executions finish. */
63
+ private readonly pending;
64
+ /** Bounded ring of execution records, newest last. */
65
+ private readonly executionHistory;
16
66
  private running;
17
67
  private timer?;
18
- constructor(jobs?: JobRegistry, executor?: JobExecutor, queue?: PriorityQueue, clock?: Clock);
68
+ /**
69
+ * @param options - Scheduler options, or a {@link JobRegistry} for the
70
+ * positional form kept for backwards compatibility.
71
+ */
72
+ constructor(options?: SchedulerOptions | JobRegistry, executor?: JobExecutor, queue?: PriorityQueue, clock?: Clock);
73
+ /** Whether the scheduler is currently running. */
74
+ get isRunning(): boolean;
75
+ /** Number of live schedules. */
76
+ get scheduleCount(): number;
19
77
  /**
20
78
  * Starts the scheduler.
21
79
  */
22
80
  start(): void;
23
81
  /**
24
- * Stops the scheduler.
82
+ * Stops the scheduler, aborting in-flight jobs and waiting for them to settle.
83
+ *
84
+ * @param options - `drain` waits for running jobs to finish instead of
85
+ * aborting them; `timeoutMs` bounds the wait either way.
25
86
  */
26
- stop(): void;
87
+ stop(options?: {
88
+ readonly drain?: boolean;
89
+ readonly timeoutMs?: number;
90
+ }): Promise<void>;
91
+ /** Waits for in-flight executions to finish, up to an optional timeout. */
92
+ private settle;
27
93
  /**
28
94
  * Defines a new job.
29
95
  */
@@ -31,34 +97,77 @@ export declare class Scheduler {
31
97
  /**
32
98
  * Schedules a job to run once after a delay.
33
99
  */
34
- after(delay: string, jobId: string): ScheduleHandle;
100
+ after(delay: string, jobId: string, options?: ScheduleOptions): ScheduleHandle;
35
101
  /**
36
102
  * Schedules a job to run at a specific date.
37
103
  */
38
- at(date: Date, jobId: string): ScheduleHandle;
104
+ at(date: Date, jobId: string, options?: ScheduleOptions): ScheduleHandle;
39
105
  /**
40
106
  * Schedules a job to run at a fixed interval.
41
107
  */
42
- every(interval: string, jobId: string): ScheduleHandle;
108
+ every(interval: string, jobId: string, options?: ScheduleOptions): ScheduleHandle;
43
109
  /**
44
110
  * Schedules a job using a cron expression.
45
111
  */
46
- cron(expression: string, jobId: string): ScheduleHandle;
112
+ cron(expression: string, jobId: string, options?: ScheduleOptions & {
113
+ readonly timezone?: string;
114
+ }): ScheduleHandle;
115
+ /** Returns a snapshot of a schedule, or undefined. */
116
+ getSchedule(scheduleId: string): Schedule | undefined;
117
+ /** Returns snapshots of every live schedule. */
118
+ listSchedules(): readonly Schedule[];
47
119
  /**
48
120
  * Schedules a job with a trigger.
49
121
  */
50
122
  private scheduleJob;
123
+ /** Applies a state change to a live schedule. */
124
+ private setScheduleState;
125
+ /** Aborts every in-flight execution of one schedule. */
126
+ private abortSchedule;
51
127
  /**
52
128
  * Internal tick method for processing due jobs.
53
129
  */
54
130
  private tick;
131
+ /** (Re)arms the tick timer for the next due schedule. */
132
+ private rearm;
55
133
  /**
56
- * Calculates the delay until the next tick.
134
+ * Computes the next fire time and puts the schedule back on the queue.
135
+ *
136
+ * Nothing did this before, so `every()` and `cron()` fired exactly once and
137
+ * then sat in the map forever.
57
138
  */
58
- private calculateDelay;
139
+ private reschedule;
140
+ /** Starts one execution of a schedule and tracks it. */
141
+ private dispatch;
142
+ /** Holds a fire time back until capacity frees up. */
143
+ private defer;
144
+ /**
145
+ * Dispatches held-back runs that can now proceed.
146
+ *
147
+ * A record that is still blocked re-defers itself, which shows up as its
148
+ * counter returning to where it started; that is the loop's exit condition,
149
+ * so a blocked record cannot spin.
150
+ */
151
+ private drainPending;
152
+ /** Records a started execution, evicting the oldest beyond the cap. */
153
+ private beginExecution;
154
+ /** Completes the record for an execution, if it is still in the history. */
155
+ private finishExecution;
59
156
  /**
60
- * Executes a schedule.
157
+ * Returns the recorded executions, oldest first.
158
+ *
159
+ * At most {@link MAX_EXECUTION_HISTORY} are kept. `JobExecution` and that
160
+ * constant were both exported from the beginning and nothing produced or
161
+ * read either.
162
+ *
163
+ * @param jobId - Restrict to one job's executions.
61
164
  */
62
- private executeSchedule;
165
+ getExecutions(jobId?: string): readonly JobExecution[];
166
+ /** Hands an execution failure to the error listener. */
167
+ private reportError;
168
+ /**
169
+ * Calculates the delay until the next tick.
170
+ */
171
+ private calculateDelay;
63
172
  }
64
173
  //# sourceMappingURL=scheduler.core.d.ts.map