@nwire/cron 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alex Gefter / 200apps Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # @nwire/cron
2
+
3
+ > Scheduled-trigger interface — fires actions on a cron expression.
4
+
5
+ ## What it does
6
+
7
+ A small wire that ticks named cron expressions and dispatches Nwire actions against the right app. Each tick seeds a fresh envelope; correlation/causation propagate through any chained dispatches. Built on [`croner`](https://github.com/Hexagon/croner). For multi-process deployments run cron in ONE process (typically the worker wire) — replicating it fires each job N times.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @nwire/cron
13
+ ```
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { createCronWire } from "@nwire/cron";
19
+ import { apps } from "@amit/learnflow";
20
+
21
+ createCronWire({
22
+ apps,
23
+ schedule: {
24
+ "0 2 * * *": "submissions.reprocess-stale",
25
+ "*/15 * * * *": { action: "mastery.recompute" },
26
+ "0 9 * * 1": { action: "reports.weekly", input: { audience: "admins" } },
27
+ },
28
+ }).start();
29
+ ```
30
+
31
+ ## API surface
32
+
33
+ - `createCronWire({ apps, schedule, logger? })` — boot the cron wire.
34
+ - `CronScheduleEntry` — `string` shortcut (action name) or `{ action, input? }`.
35
+
36
+ ## When to use
37
+
38
+ Whenever you need time-based triggers — nightly aggregations, weekly reports, periodic syncs. Fits L3 and up.
39
+
40
+ ## Standalone use
41
+
42
+ For developers using `@nwire/cron` **without the rest of Nwire** — pair it with any TypeScript project, any container, any HTTP framework.
43
+
44
+ ```ts
45
+ // See the package's main entry (src/) for the standalone surface.
46
+ // The exports below work without @nwire/app or @nwire/forge.
47
+ import {} from /* ...standalone exports... */ "@nwire/cron";
48
+ ```
49
+
50
+ ## Within nwire-app
51
+
52
+ For developers using this package as part of the Nwire stack — register it via `app.use(...)` or it auto-wires when you compose `createApp({ modules })`.
53
+
54
+ ```ts
55
+ import { createApp } from "@nwire/forge";
56
+
57
+ const app = createApp({
58
+ /* ...config... */
59
+ });
60
+ // Adapter/plugin wiring happens here when applicable.
61
+ ```
62
+
63
+ ## See also
64
+
65
+ - [Architecture sketch §05 — Adapters tier](../../architecture-sketch.html#packages)
66
+ - Sibling packages: [@nwire/queue](../nwire-queue), [@nwire/forge](../nwire-forge)
package/dist/cron.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * `@nwire/cron` — scheduled-trigger interface for Nwire apps.
3
+ *
4
+ * Ticks a cron expression and dispatches the named action against the
5
+ * owning app. Fresh envelope per tick; correlation/causation propagate
6
+ * through chained dispatches. Built on `croner`. For multi-process
7
+ * deployments run cron in ONE process.
8
+ *
9
+ * See: architecture-sketch.html §05 (Adapters tier).
10
+ */
11
+ import { Cron } from "croner";
12
+ import type { AppDefinition, App } from "@nwire/forge";
13
+ export type CronScheduleEntry = string | {
14
+ readonly action: string;
15
+ readonly input?: unknown;
16
+ readonly tenant?: string;
17
+ };
18
+ export interface CronWireOptions {
19
+ /** Apps to host. Cron locates which app owns each action by name. */
20
+ readonly apps: readonly AppDefinition[];
21
+ /** Pre-instantiated `App`s (for tests where stores are injected). */
22
+ readonly appInstances?: readonly App[];
23
+ /**
24
+ * Schedule: keys are cron expressions, values are action names or full specs.
25
+ * Examples: `0 2 * * *` (daily 02:00), `@hourly` (Croner shortcuts supported).
26
+ */
27
+ readonly schedule: Readonly<Record<string, CronScheduleEntry>>;
28
+ /** Timezone (IANA) for evaluating cron expressions. Default: process default. */
29
+ readonly timezone?: string;
30
+ }
31
+ export interface CronWire {
32
+ readonly apps: readonly App[];
33
+ /** Active Cron handles (for inspection / testing). */
34
+ readonly schedules: readonly Cron[];
35
+ start(): Promise<void>;
36
+ stop(): Promise<void>;
37
+ }
38
+ export declare function createCronWire(options: CronWireOptions): CronWire;
39
+ //# sourceMappingURL=cron.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cron.d.ts","sourceRoot":"","sources":["../src/cron.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAGvD,MAAM,MAAM,iBAAiB,GACzB,MAAM,GACN;IACE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEN,MAAM,WAAW,eAAe;IAC9B,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,SAAS,aAAa,EAAE,CAAC;IACxC,qEAAqE;IACrE,QAAQ,CAAC,YAAY,CAAC,EAAE,SAAS,GAAG,EAAE,CAAC;IACvC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC;IAC/D,iFAAiF;IACjF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,EAAE,CAAC;IAC9B,sDAAsD;IACtD,QAAQ,CAAC,SAAS,EAAE,SAAS,IAAI,EAAE,CAAC;IACpC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,QAAQ,CAiCjE"}
package/dist/cron.js ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * `@nwire/cron` — scheduled-trigger interface for Nwire apps.
3
+ *
4
+ * Ticks a cron expression and dispatches the named action against the
5
+ * owning app. Fresh envelope per tick; correlation/causation propagate
6
+ * through chained dispatches. Built on `croner`. For multi-process
7
+ * deployments run cron in ONE process.
8
+ *
9
+ * See: architecture-sketch.html §05 (Adapters tier).
10
+ */
11
+ import { Cron } from "croner";
12
+ import { seedEnvelope } from "@nwire/envelope";
13
+ export function createCronWire(options) {
14
+ const instances = options.appInstances?.slice() ?? options.apps.map((def) => def.create());
15
+ const schedules = [];
16
+ return {
17
+ apps: instances,
18
+ schedules,
19
+ async start() {
20
+ await Promise.all(instances.map((a) => a.start()));
21
+ for (const [expr, entry] of Object.entries(options.schedule)) {
22
+ const spec = typeof entry === "string" ? { action: entry, input: {} } : entry;
23
+ const owner = instances.find((a) => actionNames(a).includes(spec.action));
24
+ if (!owner) {
25
+ throw new Error(`@nwire/cron: action "${spec.action}" not found in any registered app`);
26
+ }
27
+ const action = findAction(owner, spec.action);
28
+ if (!action)
29
+ continue;
30
+ const cron = new Cron(expr, { timezone: options.timezone, protect: true }, async () => {
31
+ const envelope = seedEnvelope({ tenant: spec.tenant });
32
+ await owner.runtime.dispatch(action, spec.input ?? {}, envelope);
33
+ });
34
+ schedules.push(cron);
35
+ }
36
+ },
37
+ async stop() {
38
+ for (const c of schedules)
39
+ c.stop();
40
+ schedules.length = 0;
41
+ },
42
+ };
43
+ }
44
+ function actionNames(app) {
45
+ const names = [];
46
+ for (const m of app.modules) {
47
+ for (const a of m.manifest.actions ?? [])
48
+ names.push(a.name);
49
+ }
50
+ return names;
51
+ }
52
+ function findAction(app, name) {
53
+ for (const m of app.modules) {
54
+ for (const a of m.manifest.actions ?? []) {
55
+ if (a.name === name)
56
+ return a;
57
+ }
58
+ }
59
+ return undefined;
60
+ }
61
+ //# sourceMappingURL=cron.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cron.js","sourceRoot":"","sources":["../src/cron.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAgC/C,MAAM,UAAU,cAAc,CAAC,OAAwB;IACrD,MAAM,SAAS,GAAU,OAAO,CAAC,YAAY,EAAE,KAAK,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;IAElG,MAAM,SAAS,GAAW,EAAE,CAAC;IAE7B,OAAO;QACL,IAAI,EAAE,SAAS;QACf,SAAS;QACT,KAAK,CAAC,KAAK;YACT,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAEnD,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OA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AS,WAAW,CAAC,GAAQ;IAC3B,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;QAC5B,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE;YAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,UAAU,CAAC,GAAQ,EAAE,IAAY;IACxC,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;QAC5B,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,EAAE,CAAC;YACzC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI;gBAAE,OAAO,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@nwire/cron",
3
+ "version": "0.7.0",
4
+ "description": "Nwire — cron interface factory. Dispatch actions on a schedule (croner-backed).",
5
+ "keywords": [
6
+ "cron",
7
+ "interface",
8
+ "nwire",
9
+ "scheduler",
10
+ "wire"
11
+ ],
12
+ "files": [
13
+ "dist",
14
+ "README.md"
15
+ ],
16
+ "type": "module",
17
+ "main": "./dist/cron.js",
18
+ "types": "./dist/cron.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/cron.js",
22
+ "types": "./dist/cron.d.ts"
23
+ }
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "dependencies": {
29
+ "croner": "^9.1.0",
30
+ "@nwire/forge": "0.7.0",
31
+ "@nwire/envelope": "0.7.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^22.19.9",
35
+ "typescript": "^5.9.3"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",
39
+ "dev": "tsc --watch",
40
+ "typecheck": "tsc --noEmit"
41
+ }
42
+ }