@molecule/api-scheduler 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.
package/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ 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.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ 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.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 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.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=browser-guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Browser guard — `@molecule/api-scheduler` is SERVER-ONLY.
3
+ *
4
+ * Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
5
+ * Evaluating a server package in a browser bundle is always an import-graph mistake
6
+ * (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
7
+ * ("Buffer is not defined") far from the culprit. Throwing here names the package and
8
+ * the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
9
+ * unaffected: the throw requires browser globals AND the absence of a node runtime.
10
+ */
11
+ const g = globalThis;
12
+ if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
13
+ throw new Error('@molecule/api-scheduler is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
14
+ 'from server code (a server route/function or your API), or dynamic-import it inside ' +
15
+ 'the server handler — never from components or shared client modules, and never ' +
16
+ 'polyfill Buffer/process to silence this.');
17
+ }
18
+ export {};
19
+ //# sourceMappingURL=browser-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,kGAAkG;QAChG,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Task scheduler interface for molecule.dev.
3
+ *
4
+ * Defines SchedulerProvider and ScheduledTask interfaces for periodic
5
+ * background task execution.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { schedule, setProvider, start } from '@molecule/api-scheduler'
10
+ * import { provider } from '@molecule/api-scheduler-default'
11
+ *
12
+ * setProvider(provider)
13
+ *
14
+ * schedule({
15
+ * name: 'cleanup',
16
+ * intervalMs: 60000,
17
+ * async handler() {
18
+ * await cleanupExpiredRecords()
19
+ * },
20
+ * })
21
+ *
22
+ * // REQUIRED: tasks do not execute until the scheduler is started.
23
+ * // (Tasks scheduled after start() begin automatically.)
24
+ * start()
25
+ * ```
26
+ *
27
+ * @remarks
28
+ * - **Nothing runs until `start()`** — call it once at server startup after wiring bonds.
29
+ * Tasks scheduled after `start()` begin automatically; `stop()` halts everything.
30
+ * - **Intervals only — there is NO cron syntax.** {@link ScheduledTask} takes `intervalMs`
31
+ * (milliseconds between runs); a cron expression string is not understood. For
32
+ * calendar-time schedules compute the interval or use a cron-capable package.
33
+ * - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
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.
37
+ * - A tick that arrives while the previous execution is still running is SKIPPED — a handler
38
+ * slower than its `intervalMs` lowers its own frequency instead of overlapping.
39
+ * - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
40
+ * `totalFailures`) — they don't crash the process, and they're invisible unless you check
41
+ * `getStatus()`/`getAllStatuses()` or log inside the handler.
42
+ * - `schedule()` with an existing `name` REPLACES that task — use stable, unique names.
43
+ *
44
+ * @module
45
+ */
46
+ export * from './browser-guard.js';
47
+ export * from './provider.js';
48
+ export * from './types.js';
49
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Task scheduler interface for molecule.dev.
3
+ *
4
+ * Defines SchedulerProvider and ScheduledTask interfaces for periodic
5
+ * background task execution.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { schedule, setProvider, start } from '@molecule/api-scheduler'
10
+ * import { provider } from '@molecule/api-scheduler-default'
11
+ *
12
+ * setProvider(provider)
13
+ *
14
+ * schedule({
15
+ * name: 'cleanup',
16
+ * intervalMs: 60000,
17
+ * async handler() {
18
+ * await cleanupExpiredRecords()
19
+ * },
20
+ * })
21
+ *
22
+ * // REQUIRED: tasks do not execute until the scheduler is started.
23
+ * // (Tasks scheduled after start() begin automatically.)
24
+ * start()
25
+ * ```
26
+ *
27
+ * @remarks
28
+ * - **Nothing runs until `start()`** — call it once at server startup after wiring bonds.
29
+ * Tasks scheduled after `start()` begin automatically; `stop()` halts everything.
30
+ * - **Intervals only — there is NO cron syntax.** {@link ScheduledTask} takes `intervalMs`
31
+ * (milliseconds between runs); a cron expression string is not understood. For
32
+ * calendar-time schedules compute the interval or use a cron-capable package.
33
+ * - **The scheduler is in-process and unpersisted.** Every server instance runs its own copy
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.
37
+ * - A tick that arrives while the previous execution is still running is SKIPPED — a handler
38
+ * slower than its `intervalMs` lowers its own frequency instead of overlapping.
39
+ * - Handler errors are caught and recorded on {@link TaskStatus} (`lastError`,
40
+ * `totalFailures`) — they don't crash the process, and they're invisible unless you check
41
+ * `getStatus()`/`getAllStatuses()` or log inside the handler.
42
+ * - `schedule()` with an existing `name` REPLACES that task — use stable, unique names.
43
+ *
44
+ * @module
45
+ */
46
+ export * from './browser-guard.js';
47
+ export * from './provider.js';
48
+ export * from './types.js';
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Scheduler provider bond accessor and convenience functions.
3
+ *
4
+ * @module
5
+ */
6
+ import type { ScheduledTask, SchedulerProvider, TaskStatus } from './types.js';
7
+ /**
8
+ * Registers a scheduler provider as the active singleton.
9
+ *
10
+ * @param provider - The scheduler provider implementation to bond.
11
+ */
12
+ export declare const setProvider: (provider: SchedulerProvider) => void;
13
+ /**
14
+ * Retrieves the bonded scheduler provider, throwing if none is configured.
15
+ *
16
+ * @returns The bonded scheduler provider.
17
+ * @throws {Error} If no scheduler provider has been bonded.
18
+ */
19
+ export declare const getProvider: () => SchedulerProvider;
20
+ /**
21
+ * Checks whether a scheduler provider is currently bonded.
22
+ *
23
+ * @returns `true` if a scheduler provider is bonded.
24
+ */
25
+ export declare const hasProvider: () => boolean;
26
+ /**
27
+ * Retrieves the bonded scheduler provider, returning null if none is bonded.
28
+ *
29
+ * @returns The bonded scheduler provider, or null.
30
+ */
31
+ export declare const getOptionalProvider: () => SchedulerProvider | null;
32
+ /**
33
+ * Registers and starts a scheduled task through the bonded provider.
34
+ *
35
+ * @param task - The task to schedule.
36
+ * @throws {Error} If no scheduler provider is bonded.
37
+ */
38
+ export declare const schedule: (task: ScheduledTask) => void;
39
+ /**
40
+ * Stops and removes a scheduled task by name.
41
+ *
42
+ * @param name - The task name.
43
+ * @returns true if found and removed, false otherwise.
44
+ * @throws {Error} If no scheduler provider is bonded.
45
+ */
46
+ export declare const unschedule: (name: string) => boolean;
47
+ /**
48
+ * Returns the runtime status of a specific task through the bonded provider.
49
+ *
50
+ * @param name - The task name.
51
+ * @returns The task status, or null if no task with that name is registered.
52
+ * @throws {Error} If no scheduler provider is bonded.
53
+ */
54
+ export declare const getStatus: (name: string) => TaskStatus | null;
55
+ /**
56
+ * Returns the runtime status of all scheduled tasks.
57
+ *
58
+ * @returns An array of TaskStatus for every registered task.
59
+ * @throws {Error} If no scheduler provider is bonded.
60
+ */
61
+ export declare const getAllStatuses: () => TaskStatus[];
62
+ /**
63
+ * Starts the bonded scheduler. Registered tasks do not execute until this is
64
+ * called; tasks scheduled while the scheduler is running begin automatically.
65
+ *
66
+ * @throws {Error} If no scheduler provider is bonded.
67
+ */
68
+ export declare const start: () => void;
69
+ /**
70
+ * Stops the bonded scheduler. All tasks stop executing; call `start()` to resume.
71
+ *
72
+ * @throws {Error} If no scheduler provider is bonded.
73
+ */
74
+ export declare const stop: () => void;
75
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAI9E;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,iBAAiB,KAAG,IAEzD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,QAAO,iBAW9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,QAAO,OAE9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,QAAO,iBAAiB,GAAG,IAE1D,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,aAAa,KAAG,IAE9C,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,KAAG,OAEzC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,UAAU,GAAG,IAErD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,QAAO,UAAU,EAE3C,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,KAAK,QAAO,IAExB,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,IAAI,QAAO,IAEvB,CAAA"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Scheduler provider bond accessor and convenience functions.
3
+ *
4
+ * @module
5
+ */
6
+ import { bond, get as bondGet, isBonded, require as bondRequire } from '@molecule/api-bond';
7
+ import { t } from '@molecule/api-i18n';
8
+ const BOND_TYPE = 'scheduler';
9
+ /**
10
+ * Registers a scheduler provider as the active singleton.
11
+ *
12
+ * @param provider - The scheduler provider implementation to bond.
13
+ */
14
+ export const setProvider = (provider) => {
15
+ bond(BOND_TYPE, provider);
16
+ };
17
+ /**
18
+ * Retrieves the bonded scheduler provider, throwing if none is configured.
19
+ *
20
+ * @returns The bonded scheduler provider.
21
+ * @throws {Error} If no scheduler provider has been bonded.
22
+ */
23
+ export const getProvider = () => {
24
+ try {
25
+ return bondRequire(BOND_TYPE);
26
+ }
27
+ catch (error) {
28
+ throw new Error(t('scheduler.error.noProvider', undefined, {
29
+ defaultValue: 'Scheduler provider not configured. Call setProvider() first.',
30
+ }), { cause: error });
31
+ }
32
+ };
33
+ /**
34
+ * Checks whether a scheduler provider is currently bonded.
35
+ *
36
+ * @returns `true` if a scheduler provider is bonded.
37
+ */
38
+ export const hasProvider = () => {
39
+ return isBonded(BOND_TYPE);
40
+ };
41
+ /**
42
+ * Retrieves the bonded scheduler provider, returning null if none is bonded.
43
+ *
44
+ * @returns The bonded scheduler provider, or null.
45
+ */
46
+ export const getOptionalProvider = () => {
47
+ return bondGet(BOND_TYPE) ?? null;
48
+ };
49
+ /**
50
+ * Registers and starts a scheduled task through the bonded provider.
51
+ *
52
+ * @param task - The task to schedule.
53
+ * @throws {Error} If no scheduler provider is bonded.
54
+ */
55
+ export const schedule = (task) => {
56
+ getProvider().schedule(task);
57
+ };
58
+ /**
59
+ * Stops and removes a scheduled task by name.
60
+ *
61
+ * @param name - The task name.
62
+ * @returns true if found and removed, false otherwise.
63
+ * @throws {Error} If no scheduler provider is bonded.
64
+ */
65
+ export const unschedule = (name) => {
66
+ return getProvider().unschedule(name);
67
+ };
68
+ /**
69
+ * Returns the runtime status of a specific task through the bonded provider.
70
+ *
71
+ * @param name - The task name.
72
+ * @returns The task status, or null if no task with that name is registered.
73
+ * @throws {Error} If no scheduler provider is bonded.
74
+ */
75
+ export const getStatus = (name) => {
76
+ return getProvider().getStatus(name);
77
+ };
78
+ /**
79
+ * Returns the runtime status of all scheduled tasks.
80
+ *
81
+ * @returns An array of TaskStatus for every registered task.
82
+ * @throws {Error} If no scheduler provider is bonded.
83
+ */
84
+ export const getAllStatuses = () => {
85
+ return getProvider().getAllStatuses();
86
+ };
87
+ /**
88
+ * Starts the bonded scheduler. Registered tasks do not execute until this is
89
+ * called; tasks scheduled while the scheduler is running begin automatically.
90
+ *
91
+ * @throws {Error} If no scheduler provider is bonded.
92
+ */
93
+ export const start = () => {
94
+ getProvider().start();
95
+ };
96
+ /**
97
+ * Stops the bonded scheduler. All tasks stop executing; call `start()` to resume.
98
+ *
99
+ * @throws {Error} If no scheduler provider is bonded.
100
+ */
101
+ export const stop = () => {
102
+ getProvider().stop();
103
+ };
104
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAC3F,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAItC,MAAM,SAAS,GAAG,WAAW,CAAA;AAE7B;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAA2B,EAAQ,EAAE;IAC/D,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAsB,EAAE;IACjD,IAAI,CAAC;QACH,OAAO,WAAW,CAAoB,SAAS,CAAC,CAAA;IAClD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,4BAA4B,EAAE,SAAS,EAAE;YACzC,YAAY,EAAE,8DAA8D;SAC7E,CAAC,EACF,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAA;IACH,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAY,EAAE;IACvC,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAA6B,EAAE;IAChE,OAAO,OAAO,CAAoB,SAAS,CAAC,IAAI,IAAI,CAAA;AACtD,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,IAAmB,EAAQ,EAAE;IACpD,WAAW,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;AAC9B,CAAC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAW,EAAE;IAClD,OAAO,WAAW,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;AACvC,CAAC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAqB,EAAE;IAC3D,OAAO,WAAW,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;AACtC,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAiB,EAAE;IAC/C,OAAO,WAAW,EAAE,CAAC,cAAc,EAAE,CAAA;AACvC,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,GAAS,EAAE;IAC9B,WAAW,EAAE,CAAC,KAAK,EAAE,CAAA;AACvB,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,GAAS,EAAE;IAC7B,WAAW,EAAE,CAAC,IAAI,EAAE,CAAA;AACtB,CAAC,CAAA"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Type definitions for the scheduler core interface.
3
+ * @module
4
+ */
5
+ /**
6
+ * A scheduled task definition.
7
+ */
8
+ export interface ScheduledTask {
9
+ /** Unique identifier for this task. */
10
+ name: string;
11
+ /** Interval in milliseconds between executions. */
12
+ intervalMs: number;
13
+ /** Async function to execute on each tick. */
14
+ handler(): Promise<void>;
15
+ /** Whether this task is enabled. Defaults to true. */
16
+ enabled?: boolean;
17
+ }
18
+ /**
19
+ * Runtime status of a scheduled task.
20
+ */
21
+ export interface TaskStatus {
22
+ /** Task name. */
23
+ name: string;
24
+ /** ISO 8601 timestamp of the last execution, or null if not yet run. */
25
+ lastRunAt: string | null;
26
+ /** ISO 8601 timestamp of the next scheduled execution. */
27
+ nextRunAt: string | null;
28
+ /** Whether the task is currently executing. */
29
+ isRunning: boolean;
30
+ /** Error message from the last failed execution, if any. */
31
+ lastError: string | null;
32
+ /** Duration in milliseconds of the last execution, or null if not yet run. */
33
+ durationMs: number | null;
34
+ /** Total number of completed executions (both success and failure). */
35
+ totalRuns: number;
36
+ /** Total number of failed executions. */
37
+ totalFailures: number;
38
+ /** ISO 8601 timestamp of the last successful execution, or null. */
39
+ lastSuccessAt: string | null;
40
+ /** Whether this task is enabled. */
41
+ enabled: boolean;
42
+ }
43
+ /**
44
+ * Scheduler provider interface. All scheduler providers must implement this.
45
+ */
46
+ export interface SchedulerProvider {
47
+ /**
48
+ * Registers a scheduled task.
49
+ * If a task with the same name already exists, it is replaced.
50
+ *
51
+ * The task does NOT execute until `start()` has been called; a task
52
+ * scheduled while the scheduler is already running begins automatically.
53
+ *
54
+ * @param task - The task to schedule.
55
+ */
56
+ schedule(task: ScheduledTask): void;
57
+ /**
58
+ * Stops and removes a scheduled task by name.
59
+ *
60
+ * @param name - The task name to unschedule.
61
+ * @returns true if found and removed, false otherwise.
62
+ */
63
+ unschedule(name: string): boolean;
64
+ /**
65
+ * Returns the runtime status of a specific task.
66
+ *
67
+ * @param name - The task name.
68
+ * @returns The task status, or null if not found.
69
+ */
70
+ getStatus(name: string): TaskStatus | null;
71
+ /**
72
+ * Returns the runtime status of all scheduled tasks.
73
+ */
74
+ getAllStatuses(): TaskStatus[];
75
+ /**
76
+ * Starts the scheduler. Tasks begin executing according to their intervals.
77
+ */
78
+ start(): void;
79
+ /**
80
+ * Stops the scheduler. All tasks stop executing. Can be restarted with start().
81
+ */
82
+ stop(): void;
83
+ }
84
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAA;IACZ,mDAAmD;IACnD,UAAU,EAAE,MAAM,CAAA;IAClB,8CAA8C;IAC9C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACxB,sDAAsD;IACtD,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,iBAAiB;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,wEAAwE;IACxE,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,0DAA0D;IAC1D,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,+CAA+C;IAC/C,SAAS,EAAE,OAAO,CAAA;IAClB,4DAA4D;IAC5D,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,8EAA8E;IAC9E,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,uEAAuE;IACvE,SAAS,EAAE,MAAM,CAAA;IACjB,yCAAyC;IACzC,aAAa,EAAE,MAAM,CAAA;IACrB,oEAAoE;IACpE,aAAa,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,oCAAoC;IACpC,OAAO,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/dist/types.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type definitions for the scheduler core interface.
3
+ * @module
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@molecule/api-scheduler",
3
+ "version": "1.0.0",
4
+ "description": "Task scheduler interface for molecule.dev",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run",
11
+ "test:watch": "vitest"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "molecule",
24
+ "scheduler",
25
+ "cron",
26
+ "tasks"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "peerDependencies": {
30
+ "@molecule/api-bond": "^1.0.0",
31
+ "@molecule/api-i18n": "^1.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@molecule/api-bond": "1.0.0",
35
+ "@molecule/api-i18n": "1.0.0",
36
+ "@types/node": "26.1.2",
37
+ "typescript": "6.0.3",
38
+ "vitest": "4.1.10"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/molecule-dev/molecule.git",
43
+ "directory": "packages/api/core/scheduler"
44
+ },
45
+ "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/core/scheduler",
46
+ "bugs": "https://github.com/molecule-dev/molecule/issues",
47
+ "publishConfig": {
48
+ "access": "public"
49
+ }
50
+ }