niahere 0.2.62 → 0.2.64

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.
@@ -0,0 +1,44 @@
1
+ import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
2
+ import { dirname } from "path";
3
+ import { getPaths } from "./paths";
4
+ import { log } from "./log";
5
+
6
+ export function writePid(pid: number): void {
7
+ const { pid: pidPath } = getPaths();
8
+ mkdirSync(dirname(pidPath), { recursive: true });
9
+ writeFileSync(pidPath, String(pid));
10
+ }
11
+
12
+ export function readPid(): number | null {
13
+ const { pid: pidPath } = getPaths();
14
+ if (!existsSync(pidPath)) return null;
15
+
16
+ try {
17
+ return parseInt(readFileSync(pidPath, "utf8").trim(), 10);
18
+ } catch {
19
+ return null;
20
+ }
21
+ }
22
+
23
+ export function removePid(): void {
24
+ const { pid: pidPath } = getPaths();
25
+ try {
26
+ unlinkSync(pidPath);
27
+ } catch {
28
+ // Already gone
29
+ }
30
+ }
31
+
32
+ export function isRunning(): boolean {
33
+ const pid = readPid();
34
+ if (pid === null) return false;
35
+
36
+ try {
37
+ process.kill(pid, 0);
38
+ return true;
39
+ } catch {
40
+ log.warn({ stalePid: pid }, "removing stale pid file (process not running)");
41
+ removePid();
42
+ return false;
43
+ }
44
+ }
@@ -0,0 +1,39 @@
1
+ import { CronExpressionParser } from "cron-parser";
2
+ import { parseDuration } from "./duration";
3
+ import type { ScheduleType } from "../types";
4
+
5
+ export function computeNextRun(
6
+ scheduleType: ScheduleType,
7
+ schedule: string,
8
+ timezone: string,
9
+ lastRunAt?: Date,
10
+ ): Date | null {
11
+ switch (scheduleType) {
12
+ case "cron": {
13
+ const expr = CronExpressionParser.parse(schedule, { tz: timezone });
14
+ return expr.next().toDate();
15
+ }
16
+ case "interval": {
17
+ const ms = parseDuration(schedule);
18
+ const base = lastRunAt || new Date();
19
+ return new Date(base.getTime() + ms);
20
+ }
21
+ case "once":
22
+ return null;
23
+ }
24
+ }
25
+
26
+ export function computeInitialNextRun(scheduleType: ScheduleType, schedule: string, timezone: string): Date {
27
+ switch (scheduleType) {
28
+ case "cron": {
29
+ const expr = CronExpressionParser.parse(schedule, { tz: timezone });
30
+ return expr.next().toDate();
31
+ }
32
+ case "interval": {
33
+ const ms = parseDuration(schedule);
34
+ return new Date(Date.now() + ms);
35
+ }
36
+ case "once":
37
+ return new Date(schedule);
38
+ }
39
+ }