@molecule/api-scheduler 1.0.0 → 1.0.2

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 (2) hide show
  1. package/README.md +293 -0
  2. package/package.json +9 -8
package/README.md ADDED
@@ -0,0 +1,293 @@
1
+ <!--
2
+ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
+ Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
+ Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
+ To change this document, edit the module-level JSDoc in src/index.ts.
6
+ Generated: 2026-08-06T03:43:59.008Z
7
+ -->
8
+
9
+ # @molecule/api-scheduler
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
+ Task scheduler interface for molecule.dev.
16
+
17
+ Defines SchedulerProvider and ScheduledTask interfaces for periodic
18
+ background task execution.
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import { schedule, setProvider, start } from '@molecule/api-scheduler'
24
+ import { provider } from '@molecule/api-scheduler-default'
25
+
26
+ setProvider(provider)
27
+
28
+ schedule({
29
+ name: 'cleanup',
30
+ intervalMs: 60000,
31
+ async handler() {
32
+ await cleanupExpiredRecords()
33
+ },
34
+ })
35
+
36
+ // REQUIRED: tasks do not execute until the scheduler is started.
37
+ // (Tasks scheduled after start() begin automatically.)
38
+ start()
39
+ ```
40
+
41
+ ## Type
42
+
43
+ `core`
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ npm install @molecule/api-scheduler @molecule/api-bond @molecule/api-i18n
49
+ ```
50
+
51
+ ## API
52
+
53
+ ### Interfaces
54
+
55
+ #### `ScheduledTask`
56
+
57
+ A scheduled task definition.
58
+
59
+ ```typescript
60
+ interface ScheduledTask {
61
+ /** Unique identifier for this task. */
62
+ name: string
63
+ /** Interval in milliseconds between executions. */
64
+ intervalMs: number
65
+ /** Async function to execute on each tick. */
66
+ handler(): Promise<void>
67
+ /** Whether this task is enabled. Defaults to true. */
68
+ enabled?: boolean
69
+ }
70
+ ```
71
+
72
+ #### `SchedulerProvider`
73
+
74
+ Scheduler provider interface. All scheduler providers must implement this.
75
+
76
+ ```typescript
77
+ interface SchedulerProvider {
78
+ /**
79
+ * Registers a scheduled task.
80
+ * If a task with the same name already exists, it is replaced.
81
+ *
82
+ * The task does NOT execute until `start()` has been called; a task
83
+ * scheduled while the scheduler is already running begins automatically.
84
+ *
85
+ * @param task - The task to schedule.
86
+ */
87
+ schedule(task: ScheduledTask): void
88
+
89
+ /**
90
+ * Stops and removes a scheduled task by name.
91
+ *
92
+ * @param name - The task name to unschedule.
93
+ * @returns true if found and removed, false otherwise.
94
+ */
95
+ unschedule(name: string): boolean
96
+
97
+ /**
98
+ * Returns the runtime status of a specific task.
99
+ *
100
+ * @param name - The task name.
101
+ * @returns The task status, or null if not found.
102
+ */
103
+ getStatus(name: string): TaskStatus | null
104
+
105
+ /**
106
+ * Returns the runtime status of all scheduled tasks.
107
+ */
108
+ getAllStatuses(): TaskStatus[]
109
+
110
+ /**
111
+ * Starts the scheduler. Tasks begin executing according to their intervals.
112
+ */
113
+ start(): void
114
+
115
+ /**
116
+ * Stops the scheduler. All tasks stop executing. Can be restarted with start().
117
+ */
118
+ stop(): void
119
+ }
120
+ ```
121
+
122
+ #### `TaskStatus`
123
+
124
+ Runtime status of a scheduled task.
125
+
126
+ ```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
+ }
149
+ ```
150
+
151
+ ### Functions
152
+
153
+ #### `getAllStatuses()`
154
+
155
+ Returns the runtime status of all scheduled tasks.
156
+
157
+ ```typescript
158
+ function getAllStatuses(): TaskStatus[]
159
+ ```
160
+
161
+ **Returns:** An array of TaskStatus for every registered task.
162
+
163
+ #### `getOptionalProvider()`
164
+
165
+ Retrieves the bonded scheduler provider, returning null if none is bonded.
166
+
167
+ ```typescript
168
+ function getOptionalProvider(): SchedulerProvider | null
169
+ ```
170
+
171
+ **Returns:** The bonded scheduler provider, or null.
172
+
173
+ #### `getProvider()`
174
+
175
+ Retrieves the bonded scheduler provider, throwing if none is configured.
176
+
177
+ ```typescript
178
+ function getProvider(): SchedulerProvider
179
+ ```
180
+
181
+ **Returns:** The bonded scheduler provider.
182
+
183
+ #### `getStatus(name)`
184
+
185
+ Returns the runtime status of a specific task through the bonded provider.
186
+
187
+ ```typescript
188
+ function getStatus(name: string): TaskStatus | null
189
+ ```
190
+
191
+ - `name` — The task name.
192
+
193
+ **Returns:** The task status, or null if no task with that name is registered.
194
+
195
+ #### `hasProvider()`
196
+
197
+ Checks whether a scheduler provider is currently bonded.
198
+
199
+ ```typescript
200
+ function hasProvider(): boolean
201
+ ```
202
+
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
+ ```
212
+
213
+ - `task` — The task to schedule.
214
+
215
+ #### `setProvider(provider)`
216
+
217
+ Registers a scheduler provider as the active singleton.
218
+
219
+ ```typescript
220
+ function setProvider(provider: SchedulerProvider): void
221
+ ```
222
+
223
+ - `provider` — The scheduler provider implementation to bond.
224
+
225
+ #### `start()`
226
+
227
+ Starts the bonded scheduler. Registered tasks do not execute until this is
228
+ called; tasks scheduled while the scheduler is running begin automatically.
229
+
230
+ ```typescript
231
+ function start(): void
232
+ ```
233
+
234
+ #### `stop()`
235
+
236
+ Stops the bonded scheduler. All tasks stop executing; call `start()` to resume.
237
+
238
+ ```typescript
239
+ function stop(): void
240
+ ```
241
+
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
+ ## Available Providers
255
+
256
+ | Provider | Package |
257
+ | ---------------------------- | ------------------------------------ |
258
+ | Cloudflare Workers Scheduler | `@molecule/api-scheduler-cloudflare` |
259
+ | Default (in-process) | `@molecule/api-scheduler-default` |
260
+
261
+ ## Injection Notes
262
+
263
+ ### Requirements
264
+
265
+ Peer dependencies:
266
+
267
+ - `@molecule/api-bond` ^1.0.1
268
+ - `@molecule/api-i18n` ^1.0.1
269
+
270
+ ### Runtime Dependencies
271
+
272
+ - `@molecule/api-bond`
273
+ - `@molecule/api-i18n`
274
+
275
+ - **Nothing runs until `start()`** — call it once at server startup after wiring bonds.
276
+ Tasks scheduled after `start()` begin automatically; `stop()` halts everything.
277
+ - **Intervals only — there is NO cron syntax.** {@link ScheduledTask} takes `intervalMs`
278
+ (milliseconds between runs); a cron expression string is not understood. For
279
+ calendar-time schedules compute the interval or use a cron-capable package.
280
+ - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
281
+ of every task — N instances execute a task N times unless the handler itself dedupes
282
+ (e.g. an atomic claim in the database). Schedules and run history are lost on restart;
283
+ missed runs are not caught up.
284
+ - A tick that arrives while the previous execution is still running is SKIPPED — a handler
285
+ slower than its `intervalMs` lowers its own frequency instead of overlapping.
286
+ - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
287
+ `totalFailures`) — they don't crash the process, and they're invisible unless you check
288
+ `getStatus()`/`getAllStatuses()` or log inside the handler.
289
+ - `schedule()` with an existing `name` REPLACES that task — use stable, unique names.
290
+
291
+ ## Translations
292
+
293
+ Translation strings are provided by `@molecule/api-locales-scheduler`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-scheduler",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Task scheduler interface for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "keywords": [
23
24
  "molecule",
@@ -27,22 +28,22 @@
27
28
  ],
28
29
  "license": "Apache-2.0",
29
30
  "peerDependencies": {
30
- "@molecule/api-bond": "^1.0.0",
31
- "@molecule/api-i18n": "^1.0.0"
31
+ "@molecule/api-bond": "^1.0.1",
32
+ "@molecule/api-i18n": "^1.0.1"
32
33
  },
33
34
  "devDependencies": {
34
- "@molecule/api-bond": "1.0.0",
35
- "@molecule/api-i18n": "1.0.0",
35
+ "@molecule/api-bond": "1.0.2",
36
+ "@molecule/api-i18n": "1.0.3",
36
37
  "@types/node": "26.1.2",
37
38
  "typescript": "6.0.3",
38
- "vitest": "4.1.10"
39
+ "vitest": "4.1.11"
39
40
  },
40
41
  "repository": {
41
42
  "type": "git",
42
43
  "url": "https://github.com/molecule-dev/molecule.git",
43
44
  "directory": "packages/api/core/scheduler"
44
45
  },
45
- "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/core/scheduler",
46
+ "homepage": "https://www.molecule.dev/packages/api-scheduler",
46
47
  "bugs": "https://github.com/molecule-dev/molecule/issues",
47
48
  "publishConfig": {
48
49
  "access": "public"