@molecule/api-scheduler 1.0.1 → 1.1.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/LICENSE CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  Apache License
2
3
  Version 2.0, January 2004
3
4
  http://www.apache.org/licenses/
@@ -34,21 +35,33 @@
34
35
 
35
36
  "Work" shall mean the work of authorship, whether in Source or
36
37
  Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work.
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
38
40
 
39
41
  "Derivative Works" shall mean any work, whether in Source or Object
40
42
  form, that is based on (or derived from) the Work and for which the
41
43
  editorial revisions, annotations, elaborations, or other modifications
42
- represent, as a whole, an original work of authorship.
43
-
44
- "Contribution" shall mean any work of authorship, including the
45
- original version of the Work and any modifications or additions
46
- to that Work, that is intentionally submitted to the Licensor for
47
- inclusion in the Work by the copyright owner or by an individual or
48
- Legal Entity authorized to submit on behalf of the copyright owner.
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
49
62
 
50
63
  "Contributor" shall mean Licensor and any individual or Legal Entity
51
- on behalf of whom a Contribution has been received by the Licensor and
64
+ on behalf of whom a Contribution has been received by Licensor and
52
65
  subsequently incorporated within the Work.
53
66
 
54
67
  2. Grant of Copyright License. Subject to the terms and conditions of
@@ -61,8 +74,18 @@
61
74
  3. Grant of Patent License. Subject to the terms and conditions of
62
75
  this License, each Contributor hereby grants to You a perpetual,
63
76
  worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
- patent license to make, have made, use, offer to sell, sell, import,
65
- and otherwise transfer the Work.
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
66
89
 
67
90
  4. Redistribution. You may reproduce and distribute copies of the
68
91
  Work or Derivative Works thereof in any medium, with or without
@@ -84,23 +107,87 @@
84
107
  (d) If the Work includes a "NOTICE" text file as part of its
85
108
  distribution, then any Derivative Works that You distribute must
86
109
  include a readable copy of the attribution notices contained
87
- within such NOTICE file.
88
-
89
- 5. Submission of Contributions.
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
90
138
 
91
139
  6. Trademarks. This License does not grant permission to use the trade
92
- names, trademarks, service marks, or product names of the Licensor.
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
93
143
 
94
144
  7. Disclaimer of Warranty. Unless required by applicable law or
95
- agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
-
98
- 8. Limitation of Liability. In no event and under no legal theory shall
99
- any Contributor be liable to You for damages.
100
-
101
- 9. Accepting Warranty or Additional Liability.
102
-
103
- Copyright 2026 Molecule Dev, Inc.
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
104
191
 
105
192
  Licensed under the Apache License, Version 2.0 (the "License");
106
193
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -3,20 +3,20 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-08-04T01:49:25.596Z
6
+ Generated: 2026-10-19T22:02:11.316Z
7
7
  -->
8
8
 
9
9
  # @molecule/api-scheduler
10
10
 
11
- > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
12
- > It is written to be read by coding agents as much as by people, and is generated from this
13
- > package's source — edit `src/index.ts` JSDoc, not this file.
14
-
15
11
  Task scheduler interface for molecule.dev.
16
12
 
17
13
  Defines SchedulerProvider and ScheduledTask interfaces for periodic
18
14
  background task execution.
19
15
 
16
+ > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
17
+ > It is written to be read by coding agents as much as by people, and is generated from this
18
+ > package's source — edit `src/index.ts` JSDoc, not this file.
19
+
20
20
  ## Quick Start
21
21
 
22
22
  ```typescript
@@ -69,6 +69,86 @@ interface ScheduledTask {
69
69
  }
70
70
  ```
71
71
 
72
+ #### `SchedulerTaskRunLock`
73
+
74
+ Cross-instance coordination for one scheduled task run.
75
+
76
+ The scheduler is in-process: every API instance runs its own timers, so with
77
+ more than one instance a task runs once per instance unless the run itself is
78
+ coordinated. A provider that needs "once across the platform" consults a
79
+ `SchedulerTaskRunLock` before invoking the handler; the winning instance runs
80
+ it, the others skip that tick.
81
+
82
+ A provider bond implements this against a store every instance shares (e.g.
83
+ a PostgreSQL session advisory lock in
84
+ `@molecule/api-scheduler-postgresql`). The lock must be held for the
85
+ DURATION of the run — and, when the provider passes `holdMs`, for the rest
86
+ of that interval — so two instances ticking milliseconds apart cannot both
87
+ run one window. Release happens on schedule or when the connection dies;
88
+ never a blind TTL lease that could expire under a slow run.
89
+
90
+ ```typescript
91
+ interface SchedulerTaskRunLock {
92
+ /**
93
+ * Run `run` only when this instance wins the named lock.
94
+ *
95
+ * A provider that cannot reach the lock store must SKIP the run rather than
96
+ * run uncoordinated: a skipped sweep is retried on the next tick, whereas two
97
+ * concurrent runs can corrupt shared state.
98
+ *
99
+ * @param taskName - The stable task name (the same name on every instance).
100
+ * @param run - The handler to run while the lock is held.
101
+ * @param options - `holdMs` keeps the lock after the run finishes, so a
102
+ * sibling instance whose timer ticks a few milliseconds later does not run
103
+ * the SAME interval. Pass the task's `intervalMs`; the lock is released when
104
+ * the window ends, and the instances race again for the next one.
105
+ * @returns `true` when the handler ran on this instance, `false` when another
106
+ * instance held the lock or the store could not be reached.
107
+ */
108
+ runExclusive(
109
+ taskName: string,
110
+ run: () => Promise<void>,
111
+ options?: { holdMs?: number },
112
+ ): Promise<boolean>
113
+
114
+ /**
115
+ * Release any connection the lock holds open.
116
+ *
117
+ * @returns Resolves once the connection is closed.
118
+ */
119
+ close?(): Promise<void>
120
+ }
121
+ ```
122
+
123
+ #### `TaskStatus`
124
+
125
+ Runtime status of a scheduled task.
126
+
127
+ ```typescript
128
+ interface TaskStatus {
129
+ /** Task name. */
130
+ name: string
131
+ /** ISO 8601 timestamp of the last execution, or null if not yet run. */
132
+ lastRunAt: string | null
133
+ /** ISO 8601 timestamp of the next scheduled execution. */
134
+ nextRunAt: string | null
135
+ /** Whether the task is currently executing. */
136
+ isRunning: boolean
137
+ /** Error message from the last failed execution, if any. */
138
+ lastError: string | null
139
+ /** Duration in milliseconds of the last execution, or null if not yet run. */
140
+ durationMs: number | null
141
+ /** Total number of completed executions (both success and failure). */
142
+ totalRuns: number
143
+ /** Total number of failed executions. */
144
+ totalFailures: number
145
+ /** ISO 8601 timestamp of the last successful execution, or null. */
146
+ lastSuccessAt: string | null
147
+ /** Whether this task is enabled. */
148
+ enabled: boolean
149
+ }
150
+ ```
151
+
72
152
  #### `SchedulerProvider`
73
153
 
74
154
  Scheduler provider interface. All scheduler providers must implement this.
@@ -119,46 +199,37 @@ interface SchedulerProvider {
119
199
  }
120
200
  ```
121
201
 
122
- #### `TaskStatus`
202
+ ### Functions
123
203
 
124
- Runtime status of a scheduled task.
204
+ #### `setProvider(provider)`
205
+
206
+ Registers a scheduler provider as the active singleton.
125
207
 
126
208
  ```typescript
127
- interface TaskStatus {
128
- /** Task name. */
129
- name: string
130
- /** ISO 8601 timestamp of the last execution, or null if not yet run. */
131
- lastRunAt: string | null
132
- /** ISO 8601 timestamp of the next scheduled execution. */
133
- nextRunAt: string | null
134
- /** Whether the task is currently executing. */
135
- isRunning: boolean
136
- /** Error message from the last failed execution, if any. */
137
- lastError: string | null
138
- /** Duration in milliseconds of the last execution, or null if not yet run. */
139
- durationMs: number | null
140
- /** Total number of completed executions (both success and failure). */
141
- totalRuns: number
142
- /** Total number of failed executions. */
143
- totalFailures: number
144
- /** ISO 8601 timestamp of the last successful execution, or null. */
145
- lastSuccessAt: string | null
146
- /** Whether this task is enabled. */
147
- enabled: boolean
148
- }
209
+ function setProvider(provider: SchedulerProvider): void
149
210
  ```
150
211
 
151
- ### Functions
212
+ - `provider` — The scheduler provider implementation to bond.
152
213
 
153
- #### `getAllStatuses()`
214
+ #### `getProvider()`
154
215
 
155
- Returns the runtime status of all scheduled tasks.
216
+ Retrieves the bonded scheduler provider, throwing if none is configured.
156
217
 
157
218
  ```typescript
158
- function getAllStatuses(): TaskStatus[]
219
+ function getProvider(): SchedulerProvider
159
220
  ```
160
221
 
161
- **Returns:** An array of TaskStatus for every registered task.
222
+ **Returns:** The bonded scheduler provider.
223
+
224
+ #### `hasProvider()`
225
+
226
+ Checks whether a scheduler provider is currently bonded.
227
+
228
+ ```typescript
229
+ function hasProvider(): boolean
230
+ ```
231
+
232
+ **Returns:** `true` if a scheduler provider is bonded.
162
233
 
163
234
  #### `getOptionalProvider()`
164
235
 
@@ -170,57 +241,49 @@ function getOptionalProvider(): SchedulerProvider | null
170
241
 
171
242
  **Returns:** The bonded scheduler provider, or null.
172
243
 
173
- #### `getProvider()`
244
+ #### `schedule(task)`
174
245
 
175
- Retrieves the bonded scheduler provider, throwing if none is configured.
246
+ Registers and starts a scheduled task through the bonded provider.
176
247
 
177
248
  ```typescript
178
- function getProvider(): SchedulerProvider
249
+ function schedule(task: ScheduledTask): void
179
250
  ```
180
251
 
181
- **Returns:** The bonded scheduler provider.
252
+ - `task` — The task to schedule.
182
253
 
183
- #### `getStatus(name)`
254
+ #### `unschedule(name)`
184
255
 
185
- Returns the runtime status of a specific task through the bonded provider.
256
+ Stops and removes a scheduled task by name.
186
257
 
187
258
  ```typescript
188
- function getStatus(name: string): TaskStatus | null
259
+ function unschedule(name: string): boolean
189
260
  ```
190
261
 
191
262
  - `name` — The task name.
192
263
 
193
- **Returns:** The task status, or null if no task with that name is registered.
264
+ **Returns:** true if found and removed, false otherwise.
194
265
 
195
- #### `hasProvider()`
266
+ #### `getStatus(name)`
196
267
 
197
- Checks whether a scheduler provider is currently bonded.
268
+ Returns the runtime status of a specific task through the bonded provider.
198
269
 
199
270
  ```typescript
200
- function hasProvider(): boolean
271
+ function getStatus(name: string): TaskStatus | null
201
272
  ```
202
273
 
203
- **Returns:** `true` if a scheduler provider is bonded.
204
-
205
- #### `schedule(task)`
206
-
207
- Registers and starts a scheduled task through the bonded provider.
208
-
209
- ```typescript
210
- function schedule(task: ScheduledTask): void
211
- ```
274
+ - `name` — The task name.
212
275
 
213
- - `task` — The task to schedule.
276
+ **Returns:** The task status, or null if no task with that name is registered.
214
277
 
215
- #### `setProvider(provider)`
278
+ #### `getAllStatuses()`
216
279
 
217
- Registers a scheduler provider as the active singleton.
280
+ Returns the runtime status of all scheduled tasks.
218
281
 
219
282
  ```typescript
220
- function setProvider(provider: SchedulerProvider): void
283
+ function getAllStatuses(): TaskStatus[]
221
284
  ```
222
285
 
223
- - `provider` — The scheduler provider implementation to bond.
286
+ **Returns:** An array of TaskStatus for every registered task.
224
287
 
225
288
  #### `start()`
226
289
 
@@ -239,23 +302,13 @@ Stops the bonded scheduler. All tasks stop executing; call `start()` to resume.
239
302
  function stop(): void
240
303
  ```
241
304
 
242
- #### `unschedule(name)`
243
-
244
- Stops and removes a scheduled task by name.
245
-
246
- ```typescript
247
- function unschedule(name: string): boolean
248
- ```
249
-
250
- - `name` — The task name.
251
-
252
- **Returns:** true if found and removed, false otherwise.
253
-
254
305
  ## Available Providers
255
306
 
256
- | Provider | Package |
257
- | -------------------- | --------------------------------- |
258
- | Default (in-process) | `@molecule/api-scheduler-default` |
307
+ | Provider | Package |
308
+ | ---------------------------- | ------------------------------------ |
309
+ | Cloudflare Workers Scheduler | `@molecule/api-scheduler-cloudflare` |
310
+ | Default (in-process) | `@molecule/api-scheduler-default` |
311
+ | PostgreSQL | `@molecule/api-scheduler-postgresql` |
259
312
 
260
313
  ## Injection Notes
261
314
 
@@ -278,8 +331,10 @@ Peer dependencies:
278
331
  calendar-time schedules compute the interval or use a cron-capable package.
279
332
  - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
280
333
  of every task — N instances execute a task N times unless the handler itself dedupes
281
- (e.g. an atomic claim in the database). Schedules and run history are lost on restart;
282
- missed runs are not caught up.
334
+ (e.g. an atomic claim in the database) or the provider coordinates runs. With more than
335
+ one API instance, bond `@molecule/api-scheduler-postgresql` (a per-task advisory lock:
336
+ one run per tick across every instance) instead of `@molecule/api-scheduler-default`.
337
+ Schedules and run history are lost on restart; missed runs are not caught up.
283
338
  - A tick that arrives while the previous execution is still running is SKIPPED — a handler
284
339
  slower than its `intervalMs` lowers its own frequency instead of overlapping.
285
340
  - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
package/dist/index.d.ts CHANGED
@@ -32,8 +32,10 @@
32
32
  * calendar-time schedules compute the interval or use a cron-capable package.
33
33
  * - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
34
34
  * of every task — N instances execute a task N times unless the handler itself dedupes
35
- * (e.g. an atomic claim in the database). Schedules and run history are lost on restart;
36
- * missed runs are not caught up.
35
+ * (e.g. an atomic claim in the database) or the provider coordinates runs. With more than
36
+ * one API instance, bond `@molecule/api-scheduler-postgresql` (a per-task advisory lock:
37
+ * one run per tick across every instance) instead of `@molecule/api-scheduler-default`.
38
+ * Schedules and run history are lost on restart; missed runs are not caught up.
37
39
  * - A tick that arrives while the previous execution is still running is SKIPPED — a handler
38
40
  * slower than its `intervalMs` lowers its own frequency instead of overlapping.
39
41
  * - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -32,8 +32,10 @@
32
32
  * calendar-time schedules compute the interval or use a cron-capable package.
33
33
  * - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
34
34
  * of every task — N instances execute a task N times unless the handler itself dedupes
35
- * (e.g. an atomic claim in the database). Schedules and run history are lost on restart;
36
- * missed runs are not caught up.
35
+ * (e.g. an atomic claim in the database) or the provider coordinates runs. With more than
36
+ * one API instance, bond `@molecule/api-scheduler-postgresql` (a per-task advisory lock:
37
+ * one run per tick across every instance) instead of `@molecule/api-scheduler-default`.
38
+ * Schedules and run history are lost on restart; missed runs are not caught up.
37
39
  * - A tick that arrives while the previous execution is still running is SKIPPED — a handler
38
40
  * slower than its `intervalMs` lowers its own frequency instead of overlapping.
39
41
  * - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
package/dist/types.d.ts CHANGED
@@ -15,6 +15,50 @@ export interface ScheduledTask {
15
15
  /** Whether this task is enabled. Defaults to true. */
16
16
  enabled?: boolean;
17
17
  }
18
+ /**
19
+ * Cross-instance coordination for one scheduled task run.
20
+ *
21
+ * The scheduler is in-process: every API instance runs its own timers, so with
22
+ * more than one instance a task runs once per instance unless the run itself is
23
+ * coordinated. A provider that needs "once across the platform" consults a
24
+ * `SchedulerTaskRunLock` before invoking the handler; the winning instance runs
25
+ * it, the others skip that tick.
26
+ *
27
+ * A provider bond implements this against a store every instance shares (e.g.
28
+ * a PostgreSQL session advisory lock in
29
+ * `@molecule/api-scheduler-postgresql`). The lock must be held for the
30
+ * DURATION of the run — and, when the provider passes `holdMs`, for the rest
31
+ * of that interval — so two instances ticking milliseconds apart cannot both
32
+ * run one window. Release happens on schedule or when the connection dies;
33
+ * never a blind TTL lease that could expire under a slow run.
34
+ */
35
+ export interface SchedulerTaskRunLock {
36
+ /**
37
+ * Run `run` only when this instance wins the named lock.
38
+ *
39
+ * A provider that cannot reach the lock store must SKIP the run rather than
40
+ * run uncoordinated: a skipped sweep is retried on the next tick, whereas two
41
+ * concurrent runs can corrupt shared state.
42
+ *
43
+ * @param taskName - The stable task name (the same name on every instance).
44
+ * @param run - The handler to run while the lock is held.
45
+ * @param options - `holdMs` keeps the lock after the run finishes, so a
46
+ * sibling instance whose timer ticks a few milliseconds later does not run
47
+ * the SAME interval. Pass the task's `intervalMs`; the lock is released when
48
+ * the window ends, and the instances race again for the next one.
49
+ * @returns `true` when the handler ran on this instance, `false` when another
50
+ * instance held the lock or the store could not be reached.
51
+ */
52
+ runExclusive(taskName: string, run: () => Promise<void>, options?: {
53
+ holdMs?: number;
54
+ }): Promise<boolean>;
55
+ /**
56
+ * Release any connection the lock holds open.
57
+ *
58
+ * @returns Resolves once the connection is closed.
59
+ */
60
+ close?(): Promise<void>;
61
+ }
18
62
  /**
19
63
  * Runtime status of a scheduled task.
20
64
  */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.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,EAAE,OAAO,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,aAAa,GAAG,IAAI,CAAA;IAEnC;;;;;OAKG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IAEjC;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAAA;IAE1C;;OAEG;IACH,cAAc,IAAI,UAAU,EAAE,CAAA;IAE9B;;OAEG;IACH,KAAK,IAAI,IAAI,CAAA;IAEb;;OAEG;IACH,IAAI,IAAI,IAAI,CAAA;CACb"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-scheduler",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Task scheduler interface for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,18 +32,18 @@
32
32
  "@molecule/api-i18n": "^1.0.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@molecule/api-bond": "1.0.1",
36
- "@molecule/api-i18n": "1.0.1",
35
+ "@molecule/api-bond": "1.0.2",
36
+ "@molecule/api-i18n": "1.0.3",
37
37
  "@types/node": "26.1.2",
38
38
  "typescript": "6.0.3",
39
- "vitest": "4.1.10"
39
+ "vitest": "4.1.11"
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",
43
43
  "url": "https://github.com/molecule-dev/molecule.git",
44
44
  "directory": "packages/api/core/scheduler"
45
45
  },
46
- "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/core/scheduler",
46
+ "homepage": "https://www.molecule.dev/packages/api-scheduler",
47
47
  "bugs": "https://github.com/molecule-dev/molecule/issues",
48
48
  "publishConfig": {
49
49
  "access": "public"