@uns-kit/cron 0.0.38 → 0.0.41

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 CHANGED
@@ -36,15 +36,33 @@ import "@uns-kit/cron";
36
36
  async function main() {
37
37
  const process = new UnsProxyProcess("mqtt-broker:1883", { processName: "cron-demo" }) as UnsProxyProcessWithCron;
38
38
 
39
- const cronProxy = await process.createCrontabProxy("*/5 * * * *");
40
- cronProxy.event.on("cronEvent", () => {
41
- 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);
42
42
  });
43
43
  }
44
44
 
45
45
  void main();
46
46
  ```
47
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
+
48
66
  ## Scripts
49
67
 
50
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(cronExpression: string, options?: TaskOptions): Promise<UnsCronProxy>;
7
+ createCrontabProxy(cronInput: CronScheduleInput, options?: CronProxyOptions): Promise<UnsCronProxy>;
9
8
  };
@@ -11,9 +11,9 @@ const getCronProxies = (instance) => {
11
11
  };
12
12
  const unsCronPlugin = ({ define }) => {
13
13
  define({
14
- async createCrontabProxy(cronExpression, options) {
14
+ async createCrontabProxy(cronInput, options) {
15
15
  await this.waitForProcessConnection();
16
- const unsCronProxy = new UnsCronProxy(cronExpression, options);
16
+ const unsCronProxy = new UnsCronProxy(cronInput, options);
17
17
  getCronProxies(this).push(unsCronProxy);
18
18
  return unsCronProxy;
19
19
  },
@@ -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 cronExpression;
5
- private readonly task;
6
- constructor(cronExpression: string, options?: TaskOptions);
13
+ private readonly tasks;
14
+ constructor(cronInput: CronScheduleInput, options?: CronProxyOptions);
7
15
  stop(): Promise<void>;
8
16
  }
@@ -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
- cronExpression;
5
- task;
6
- constructor(cronExpression, options) {
29
+ tasks;
30
+ constructor(cronInput, options) {
7
31
  super();
8
- this.cronExpression = cronExpression;
9
- this.task = cron.schedule(this.cronExpression, () => {
10
- this.event.emit("cronEvent", {});
11
- }, options);
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.38",
3
+ "version": "0.0.41",
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.27"
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
  }