@uns-kit/cron 0.0.37 → 0.0.40
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/README.md +23 -3
- package/dist/index.d.ts +1 -0
- package/dist/uns-cron-plugin.d.ts +2 -3
- package/dist/uns-cron-plugin.js +2 -2
- package/dist/uns-cron-proxy.d.ts +11 -3
- package/dist/uns-cron-proxy.js +35 -8
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
`@uns-kit/cron` adds cron-style scheduling to the UNS runtime. It registers a `createCrontabProxy` method on `UnsProxyProcess`, emitting `cronEvent` notifications that can be bridged to MQTT topics or any downstream integration.
|
|
4
4
|
|
|
5
|
+
Note: Apps built with uns-kit are intended to be managed by the **UNS Datahub controller**.
|
|
6
|
+
|
|
5
7
|
## uns-kit in context
|
|
6
8
|
|
|
7
9
|
uns-kit is a batteries-included toolkit for Unified Namespace applications. It standardizes MQTT wiring, auth, config schemas, and scaffolding so you can focus on scheduled behaviors and domain logic. The toolkit includes:
|
|
@@ -34,15 +36,33 @@ import "@uns-kit/cron";
|
|
|
34
36
|
async function main() {
|
|
35
37
|
const process = new UnsProxyProcess("mqtt-broker:1883", { processName: "cron-demo" }) as UnsProxyProcessWithCron;
|
|
36
38
|
|
|
37
|
-
const cronProxy = await process.createCrontabProxy("*/5 * * * *");
|
|
38
|
-
cronProxy.event.on("cronEvent", () => {
|
|
39
|
-
console.log("tick");
|
|
39
|
+
const cronProxy = await process.createCrontabProxy("*/5 * * * *", { event: "heartbeat" });
|
|
40
|
+
cronProxy.event.on("cronEvent", ({ event, cronExpression }) => {
|
|
41
|
+
console.log("tick", event, cronExpression);
|
|
40
42
|
});
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
void main();
|
|
44
46
|
```
|
|
45
47
|
|
|
48
|
+
## Multiple schedules
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const cronProxy = await process.createCrontabProxy([
|
|
52
|
+
{ cronExpression: "* * * * * *", event: "a" },
|
|
53
|
+
{ cronExpression: "*/10 * * * * *", event: "b" },
|
|
54
|
+
]);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can also pass an array of cron strings and optional default options:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const cronProxy = await process.createCrontabProxy(
|
|
61
|
+
["*/5 * * * *", "*/15 * * * *"],
|
|
62
|
+
{ timezone: "UTC" },
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
46
66
|
## Scripts
|
|
47
67
|
|
|
48
68
|
```bash
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { default } from "./uns-cron-plugin.js";
|
|
2
2
|
export { UnsCronProxy, type UnsProxyProcessWithCron } from "./uns-cron-plugin.js";
|
|
3
3
|
export { default as UnsCronProxyClass } from "./uns-cron-proxy.js";
|
|
4
|
+
export type { CronProxyOptions, CronSchedule, CronScheduleInput } from "./uns-cron-proxy.js";
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type { TaskOptions } from "node-cron";
|
|
2
1
|
import UnsProxyProcess, { type UnsProxyProcessPlugin } from "@uns-kit/core/uns/uns-proxy-process.js";
|
|
3
|
-
import UnsCronProxy from "./uns-cron-proxy.js";
|
|
2
|
+
import UnsCronProxy, { type CronProxyOptions, type CronScheduleInput } from "./uns-cron-proxy.js";
|
|
4
3
|
declare const unsCronPlugin: UnsProxyProcessPlugin;
|
|
5
4
|
export default unsCronPlugin;
|
|
6
5
|
export { UnsCronProxy };
|
|
7
6
|
export type UnsProxyProcessWithCron = UnsProxyProcess & {
|
|
8
|
-
createCrontabProxy(
|
|
7
|
+
createCrontabProxy(cronInput: CronScheduleInput, options?: CronProxyOptions): Promise<UnsCronProxy>;
|
|
9
8
|
};
|
package/dist/uns-cron-plugin.js
CHANGED
|
@@ -11,9 +11,9 @@ const getCronProxies = (instance) => {
|
|
|
11
11
|
};
|
|
12
12
|
const unsCronPlugin = ({ define }) => {
|
|
13
13
|
define({
|
|
14
|
-
async createCrontabProxy(
|
|
14
|
+
async createCrontabProxy(cronInput, options) {
|
|
15
15
|
await this.waitForProcessConnection();
|
|
16
|
-
const unsCronProxy = new UnsCronProxy(
|
|
16
|
+
const unsCronProxy = new UnsCronProxy(cronInput, options);
|
|
17
17
|
getCronProxies(this).push(unsCronProxy);
|
|
18
18
|
return unsCronProxy;
|
|
19
19
|
},
|
package/dist/uns-cron-proxy.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { TaskOptions } from "node-cron";
|
|
2
2
|
import UnsProxy from "@uns-kit/core/uns/uns-proxy.js";
|
|
3
|
+
export type CronSchedule = {
|
|
4
|
+
cronExpression: string;
|
|
5
|
+
event?: string;
|
|
6
|
+
options?: TaskOptions;
|
|
7
|
+
};
|
|
8
|
+
export type CronScheduleInput = string | string[] | CronSchedule[];
|
|
9
|
+
export type CronProxyOptions = TaskOptions & {
|
|
10
|
+
event?: string;
|
|
11
|
+
};
|
|
3
12
|
export default class UnsCronProxy extends UnsProxy {
|
|
4
|
-
private readonly
|
|
5
|
-
|
|
6
|
-
constructor(cronExpression: string, options?: TaskOptions);
|
|
13
|
+
private readonly tasks;
|
|
14
|
+
constructor(cronInput: CronScheduleInput, options?: CronProxyOptions);
|
|
7
15
|
stop(): Promise<void>;
|
|
8
16
|
}
|
package/dist/uns-cron-proxy.js
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
import cron from "node-cron";
|
|
2
2
|
import UnsProxy from "@uns-kit/core/uns/uns-proxy.js";
|
|
3
|
+
const normalizeTaskOptions = (options) => {
|
|
4
|
+
if (!options) {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
const { event: _event, ...taskOptions } = options;
|
|
8
|
+
return Object.keys(taskOptions).length > 0 ? taskOptions : undefined;
|
|
9
|
+
};
|
|
10
|
+
const normalizeCronSchedules = (input, options) => {
|
|
11
|
+
const defaultEvent = options?.event;
|
|
12
|
+
const defaultOptions = normalizeTaskOptions(options);
|
|
13
|
+
const toSchedule = (cronExpression, event, scheduleOptions) => ({
|
|
14
|
+
cronExpression,
|
|
15
|
+
event,
|
|
16
|
+
options: scheduleOptions,
|
|
17
|
+
});
|
|
18
|
+
if (typeof input === "string") {
|
|
19
|
+
return [toSchedule(input, defaultEvent, defaultOptions)];
|
|
20
|
+
}
|
|
21
|
+
return input.map((entry) => {
|
|
22
|
+
if (typeof entry === "string") {
|
|
23
|
+
return toSchedule(entry, defaultEvent, defaultOptions);
|
|
24
|
+
}
|
|
25
|
+
return toSchedule(entry.cronExpression, entry.event ?? defaultEvent, entry.options ?? defaultOptions);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
3
28
|
export default class UnsCronProxy extends UnsProxy {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
constructor(cronExpression, options) {
|
|
29
|
+
tasks;
|
|
30
|
+
constructor(cronInput, options) {
|
|
7
31
|
super();
|
|
8
|
-
|
|
9
|
-
this.
|
|
10
|
-
this.event.emit("cronEvent", {
|
|
11
|
-
|
|
32
|
+
const schedules = normalizeCronSchedules(cronInput, options);
|
|
33
|
+
this.tasks = schedules.map((schedule) => cron.schedule(schedule.cronExpression, () => {
|
|
34
|
+
this.event.emit("cronEvent", {
|
|
35
|
+
event: schedule.event,
|
|
36
|
+
cronExpression: schedule.cronExpression,
|
|
37
|
+
});
|
|
38
|
+
}, schedule.options));
|
|
12
39
|
}
|
|
13
40
|
async stop() {
|
|
14
41
|
await super.stop();
|
|
15
|
-
this.task.stop();
|
|
42
|
+
this.tasks.forEach((task) => task.stop());
|
|
16
43
|
}
|
|
17
44
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uns-kit/cron",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "Cron-driven plugin for UnsProxyProcess that emits UNS events on a schedule.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,10 +32,11 @@
|
|
|
32
32
|
"types": "dist/index.d.ts",
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"node-cron": "^4.2.1",
|
|
35
|
-
"@uns-kit/core": "1.0.
|
|
35
|
+
"@uns-kit/core": "1.0.31"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsc -p tsconfig.build.json",
|
|
39
|
-
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
39
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
40
|
+
"postversion": "node ../../scripts/update-cli-package-version.cjs"
|
|
40
41
|
}
|
|
41
42
|
}
|