agent-yes 1.306.3 → 1.307.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/README.md +1 -0
- package/dist/{SUPPORTED_CLIS-Dvgot0oL.js → SUPPORTED_CLIS-BOGmmIrH.js} +3 -3
- package/dist/{SUPPORTED_CLIS-XUv7pAhg.js → SUPPORTED_CLIS-qL9_2DZ8.js} +2 -2
- package/dist/{agentShare-DHslHOe9.js → agentShare-C2oXrGvf.js} +4 -4
- package/dist/{callback-BnPh1ipj.js → callback-CNL-PGwh.js} +4 -4
- package/dist/{callback-BbDa82Yw.js → callback-ie8yyKma.js} +3 -3
- package/dist/cli.js +8 -8
- package/dist/{cmdPs-D_9k-_up.js → cmdPs-CfR5qCiO.js} +4 -4
- package/dist/index.js +2 -2
- package/dist/{notifyDaemon-Dsb009Id.js → notifyDaemon-DcAbvP43.js} +4 -4
- package/dist/{parentPingLoop-cXg1hXg0.js → parentPingLoop-BYT4XgfN.js} +2 -2
- package/dist/{remotes-BsMGvAay.js → remotes-CAZymHd7.js} +2 -2
- package/dist/{remotes-BqLoB5GR.js → remotes-W6NhMDfP.js} +1 -1
- package/dist/{roomReplyAlias-B-v76TA8.js → roomReplyAlias-B1EJft2d.js} +2 -2
- package/dist/roomReplyAlias-BeGVkAlc.js +6 -0
- package/dist/{rustBinary-BaadP-ZC.js → rustBinary-BjsZbiv5.js} +2 -2
- package/dist/{rustBinary-0BGlZPM2.js → rustBinary-CXQGcJEm.js} +2 -2
- package/dist/{schedule-CEZ-hTWV.js → schedule-zacFV53l.js} +49 -8
- package/dist/{schedulesCli-CLqieyQZ.js → schedules-BvwTGXU2.js} +2 -339
- package/dist/schedulesCli-Donh0LS2.js +343 -0
- package/dist/{serve-CuqQMmlO.js → serve-C38WYmzz.js} +35 -28
- package/dist/{serveRust-BfWkeL6T.js → serveRust-76_apYb8.js} +1 -1
- package/dist/{setup-D2hLlUYT.js → setup-CeAisuhv.js} +4 -4
- package/dist/{shareCmd-DbMp0rph.js → shareCmd-foO5MbJp.js} +6 -6
- package/dist/{spawnGate-I4p2fbz7.js → spawnGate-DhPgMs0Y.js} +1 -1
- package/dist/{spawnGate-3YSXiRFS.js → spawnGate-nUrmf4dd.js} +2 -2
- package/dist/{subcommands-7SrEP-BA.js → subcommands-DE31Tfv_.js} +24 -24
- package/dist/{subcommands-_Neez7D-.js → subcommands-DLQEC24N.js} +3 -3
- package/dist/{systemPathLink-Y3iC7bYV.js → systemPathLink-DI9WdoqQ.js} +1 -1
- package/dist/{terminal-iUqKIceC.js → terminal-CBbp6PfB.js} +2 -2
- package/dist/{todoCli-uJ3nK9j4.js → todoCli-BllShFnc.js} +1 -1
- package/dist/{tray-CLktEN3G.js → tray-RIjHSw7z.js} +1 -1
- package/dist/trayApp-BSOmax2E.js +5 -0
- package/dist/{trayApp-l9q9To-G.js → trayApp-C9SZOPpw.js} +2 -2
- package/dist/{ts-BLBWiX6i.js → ts-CO1H1P2R.js} +3 -3
- package/dist/{versionChecker-Dt2Aewei.js → versionChecker-CG--tQsV.js} +2 -2
- package/dist/{webrtcRemote-C3xQyf3u.js → webrtcRemote-B4pwES4i.js} +1 -1
- package/dist/{widget-_K5rPsLe.js → widget-CI7BtN94.js} +3 -3
- package/dist/{workspaceConfig-DBgsu81B.js → workspaceConfig-DjEklzxf.js} +1 -1
- package/dist/{ws-BCJF2ZKx.js → ws-DLJSBws6.js} +3 -3
- package/dist/{ws-BaBtzqo_.js → ws-W5nWHjDt.js} +4 -4
- package/lab/ui/console-logic.js +2 -2
- package/lab/ui/device-sync.js +209 -0
- package/lab/ui/device-transfer.js +99 -0
- package/lab/ui/index.html +129 -40
- package/lab/ui/sw.js +2 -0
- package/package.json +3 -1
- package/ts/schedule.ts +51 -1
- package/ts/scheduleSummary.bun.spec.ts +74 -0
- package/ts/serve.ts +11 -0
- package/dist/roomReplyAlias-BzWUBkQy.js +0 -6
- package/dist/trayApp-oEZBU7Cx.js +0 -5
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { spawn, spawnSync } from "child_process";
|
|
2
|
-
import { appendFileSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, watch, writeFileSync } from "fs";
|
|
3
1
|
import { homedir } from "os";
|
|
4
2
|
import path from "path";
|
|
5
3
|
|
|
@@ -445,340 +443,5 @@ function legacyRows(oxmgrList) {
|
|
|
445
443
|
}
|
|
446
444
|
|
|
447
445
|
//#endregion
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const argv1 = process.argv[1];
|
|
451
|
-
return argv1 && /\.(c|m)?[jt]s$/.test(argv1) ? [process.execPath, argv1] : [argv1 || "ay"];
|
|
452
|
-
}
|
|
453
|
-
function runSelf(args) {
|
|
454
|
-
const [cmd, ...pre] = selfCmd();
|
|
455
|
-
return spawnSync(cmd, [...pre, ...args], {
|
|
456
|
-
encoding: "utf8",
|
|
457
|
-
stdio: [
|
|
458
|
-
"ignore",
|
|
459
|
-
"pipe",
|
|
460
|
-
"pipe"
|
|
461
|
-
],
|
|
462
|
-
maxBuffer: 64 * 1024 * 1024
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
function readText(f) {
|
|
466
|
-
return existsSync(f) ? readFileSync(f, "utf8") : null;
|
|
467
|
-
}
|
|
468
|
-
function writeAtomic(f, text) {
|
|
469
|
-
mkdirSync(path.dirname(f), { recursive: true });
|
|
470
|
-
const tmp = `${f}.${process.pid}.tmp`;
|
|
471
|
-
writeFileSync(tmp, text);
|
|
472
|
-
renameSync(tmp, f);
|
|
473
|
-
}
|
|
474
|
-
const loadStore = () => parseStore(readText(storeFile()));
|
|
475
|
-
const saveStore = (s) => writeAtomic(storeFile(), JSON.stringify(s, null, 2) + "\n");
|
|
476
|
-
const loadState = () => parseState(readText(stateFile()));
|
|
477
|
-
const saveState = (s) => writeAtomic(stateFile(), JSON.stringify(s) + "\n");
|
|
478
|
-
function log(e) {
|
|
479
|
-
const f = logFile();
|
|
480
|
-
mkdirSync(path.dirname(f), { recursive: true });
|
|
481
|
-
appendFileSync(f, JSON.stringify(e) + "\n");
|
|
482
|
-
}
|
|
483
|
-
/** Resolve the target and `ay send --force`. Returns the exit code (3 = target not live). */
|
|
484
|
-
function fire(e, now, manual = false) {
|
|
485
|
-
let live = [];
|
|
486
|
-
try {
|
|
487
|
-
live = JSON.parse(runSelf(["ls", "--json"]).stdout || "[]");
|
|
488
|
-
} catch {}
|
|
489
|
-
const r = resolveTarget(e.target, live, (lane) => {
|
|
490
|
-
const x = runSelf([
|
|
491
|
-
"lanes",
|
|
492
|
-
"id",
|
|
493
|
-
lane
|
|
494
|
-
]);
|
|
495
|
-
return {
|
|
496
|
-
ok: x.status === 0,
|
|
497
|
-
out: x.status === 0 ? x.stdout : (x.stderr || "").trim().split("\n")[0] || ""
|
|
498
|
-
};
|
|
499
|
-
});
|
|
500
|
-
const base = {
|
|
501
|
-
ts: new Date(now).toISOString(),
|
|
502
|
-
name: e.name,
|
|
503
|
-
target: e.target,
|
|
504
|
-
...manual ? { manual } : {}
|
|
505
|
-
};
|
|
506
|
-
if (!r.ok) {
|
|
507
|
-
log({
|
|
508
|
-
...base,
|
|
509
|
-
ok: false,
|
|
510
|
-
code: 3,
|
|
511
|
-
error: r.error
|
|
512
|
-
});
|
|
513
|
-
console.error(`ay cron ${e.name}: ${r.error}; nothing sent`);
|
|
514
|
-
return 3;
|
|
515
|
-
}
|
|
516
|
-
const s = runSelf([
|
|
517
|
-
"send",
|
|
518
|
-
r.agentId,
|
|
519
|
-
renderMessage(e, now),
|
|
520
|
-
"--force"
|
|
521
|
-
]);
|
|
522
|
-
const code = s.status ?? 1;
|
|
523
|
-
log({
|
|
524
|
-
...base,
|
|
525
|
-
agent_id: r.agentId,
|
|
526
|
-
ok: code === 0,
|
|
527
|
-
code,
|
|
528
|
-
...code ? { error: (s.stderr || "").trim().slice(0, 500) } : {}
|
|
529
|
-
});
|
|
530
|
-
return code;
|
|
531
|
-
}
|
|
532
|
-
function lastStr(st, name, tz) {
|
|
533
|
-
const l = st.last[name];
|
|
534
|
-
return l ? `${fmtTime(l.at, tz)} ${l.ok ? "ok" : `exit ${l.code}`}` : "-";
|
|
535
|
-
}
|
|
536
|
-
function printEntries(entries, st, withSource) {
|
|
537
|
-
const now = Date.now();
|
|
538
|
-
for (const e of entries) {
|
|
539
|
-
const next = e.enabled ? nextRuns(e.cron, e.tz, now, 1)[0] : void 0;
|
|
540
|
-
console.log([
|
|
541
|
-
e.name.padEnd(6),
|
|
542
|
-
...withSource ? [`source=${e.source}`] : [],
|
|
543
|
-
`"${e.cron}" ${e.tz}`,
|
|
544
|
-
e.target,
|
|
545
|
-
e.enabled ? "" : "(disabled)",
|
|
546
|
-
`next ${next ? fmtTime(next, e.tz) : "-"}`,
|
|
547
|
-
`last ${lastStr(st, e.name, e.tz)}`,
|
|
548
|
-
`: ${e.message.length > 60 ? e.message.slice(0, 59) + "…" : e.message}`
|
|
549
|
-
].filter(Boolean).join(" "));
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
async function cmdCron(argv) {
|
|
553
|
-
const [sub, ...rest] = argv;
|
|
554
|
-
if (!sub || sub === "help" || sub === "--help" || sub === "-h") {
|
|
555
|
-
process.stdout.write(CRON_HELP);
|
|
556
|
-
return sub ? 0 : 1;
|
|
557
|
-
}
|
|
558
|
-
if (sub === "ls" || sub === "list") {
|
|
559
|
-
const st = loadState();
|
|
560
|
-
const entries = loadStore().entries.filter((e) => e.source === "cron");
|
|
561
|
-
if (!entries.length) console.log("no reminders (ay cron help)");
|
|
562
|
-
printEntries(entries, st, false);
|
|
563
|
-
if (entries.length && !daemonPid()) console.log(`\nwarning: the scheduler daemon is not running (ay schedules daemon install)`);
|
|
564
|
-
return 0;
|
|
565
|
-
}
|
|
566
|
-
if (sub === "rm" || sub === "remove") {
|
|
567
|
-
const id = rest[0];
|
|
568
|
-
const store = loadStore();
|
|
569
|
-
const hit = store.entries.find((e) => e.name === id && e.source === "cron");
|
|
570
|
-
if (!id || !hit) {
|
|
571
|
-
console.error(`ay cron rm: no reminder "${id ?? ""}" (see ay cron ls)`);
|
|
572
|
-
return 2;
|
|
573
|
-
}
|
|
574
|
-
saveStore({
|
|
575
|
-
...store,
|
|
576
|
-
entries: store.entries.filter((e) => e !== hit)
|
|
577
|
-
});
|
|
578
|
-
console.log(`removed ${id}`);
|
|
579
|
-
return 0;
|
|
580
|
-
}
|
|
581
|
-
const p = parseCronAdd(argv);
|
|
582
|
-
if (!p.ok) {
|
|
583
|
-
console.error(`ay cron: ${p.error}`);
|
|
584
|
-
return 1;
|
|
585
|
-
}
|
|
586
|
-
const store = loadStore();
|
|
587
|
-
const name = nextCronId(store);
|
|
588
|
-
const entry = {
|
|
589
|
-
name,
|
|
590
|
-
cron: p.cron,
|
|
591
|
-
tz: p.tz,
|
|
592
|
-
target: p.target,
|
|
593
|
-
message: p.message,
|
|
594
|
-
enabled: true,
|
|
595
|
-
missed: "skip",
|
|
596
|
-
source: "cron",
|
|
597
|
-
since: Date.now()
|
|
598
|
-
};
|
|
599
|
-
saveStore({
|
|
600
|
-
...store,
|
|
601
|
-
entries: [...store.entries, entry]
|
|
602
|
-
});
|
|
603
|
-
const next = nextRuns(p.cron, p.tz, Date.now(), 1)[0];
|
|
604
|
-
console.log(`added ${name}: "${p.cron}" ${p.tz} -> ${p.target}; next ${next ? fmtTime(next, p.tz) : "-"}`);
|
|
605
|
-
if (!await ensureDaemon(false)) console.log(`warning: could not start the scheduler daemon (ay schedules daemon install)`);
|
|
606
|
-
return 0;
|
|
607
|
-
}
|
|
608
|
-
const pidFile = () => path.join(path.dirname(stateFile()), "schedulerd.pid");
|
|
609
|
-
function alive(pid) {
|
|
610
|
-
try {
|
|
611
|
-
process.kill(pid, 0);
|
|
612
|
-
return true;
|
|
613
|
-
} catch (e) {
|
|
614
|
-
return e.code === "EPERM";
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
function daemonPid() {
|
|
618
|
-
const pid = Number(readText(pidFile())?.trim());
|
|
619
|
-
return pid && alive(pid) ? pid : null;
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Foreground loop, one per machine (pid file). Sleeps until the earliest next fire
|
|
623
|
-
* (capped at MAX_SLEEP_MS), re-arms after each fire, and wakes at once when the
|
|
624
|
-
* store file changes (fs.watch on its directory: writes are atomic renames).
|
|
625
|
-
*/
|
|
626
|
-
async function daemonRun() {
|
|
627
|
-
const other = daemonPid();
|
|
628
|
-
if (other && other !== process.pid) {
|
|
629
|
-
console.error(`${DAEMON_NAME}: already running (pid ${other})`);
|
|
630
|
-
return 0;
|
|
631
|
-
}
|
|
632
|
-
writeAtomic(pidFile(), `${process.pid}\n`);
|
|
633
|
-
const sf = storeFile();
|
|
634
|
-
mkdirSync(path.dirname(sf), { recursive: true });
|
|
635
|
-
console.error(`${DAEMON_NAME}: started pid ${process.pid}, store ${sf}`);
|
|
636
|
-
let wake = null;
|
|
637
|
-
watch(path.dirname(sf), (_ev, file) => {
|
|
638
|
-
if (!file || file.toString() === path.basename(sf)) wake?.();
|
|
639
|
-
});
|
|
640
|
-
for (;;) {
|
|
641
|
-
const now = Date.now();
|
|
642
|
-
const entries = loadStore().entries;
|
|
643
|
-
const t = tick(entries, loadState(), now);
|
|
644
|
-
if (t.clockBack) console.error(`${DAEMON_NAME}: clock went backwards; resuming from ${new Date(now).toISOString()}`);
|
|
645
|
-
for (const e of t.fire) {
|
|
646
|
-
const code = fire(e, now);
|
|
647
|
-
t.state.last[e.name] = {
|
|
648
|
-
at: now,
|
|
649
|
-
ok: code === 0,
|
|
650
|
-
code
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
saveState(t.state);
|
|
654
|
-
const ms = wakeDelay(entries, Date.now());
|
|
655
|
-
await new Promise((r) => {
|
|
656
|
-
const timer = setTimeout(done, ms);
|
|
657
|
-
function done() {
|
|
658
|
-
clearTimeout(timer);
|
|
659
|
-
wake = null;
|
|
660
|
-
r();
|
|
661
|
-
}
|
|
662
|
-
wake = done;
|
|
663
|
-
});
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
function has(cmd, args) {
|
|
667
|
-
return spawnSync(cmd, args, { stdio: "ignore" }).status === 0;
|
|
668
|
-
}
|
|
669
|
-
function which(cmd) {
|
|
670
|
-
const r = spawnSync(process.platform === "win32" ? "where" : "which", [cmd], { encoding: "utf8" });
|
|
671
|
-
return r.status === 0 ? r.stdout.split(/\r?\n/)[0].trim() || null : null;
|
|
672
|
-
}
|
|
673
|
-
function autostartKind() {
|
|
674
|
-
return pickAutostart({
|
|
675
|
-
platform: process.platform,
|
|
676
|
-
hasOxmgr: !!which("oxmgr"),
|
|
677
|
-
systemdUser: process.platform === "linux" && has("systemctl", ["--user", "show-environment"])
|
|
678
|
-
});
|
|
679
|
-
}
|
|
680
|
-
/** Start the daemon and register it to stay alive. Returns whether it is running. */
|
|
681
|
-
async function ensureDaemon(verbose) {
|
|
682
|
-
if (daemonPid()) {
|
|
683
|
-
if (verbose) console.log(`${DAEMON_NAME}: already running (pid ${daemonPid()})`);
|
|
684
|
-
return true;
|
|
685
|
-
}
|
|
686
|
-
const argv = [
|
|
687
|
-
...selfCmd(),
|
|
688
|
-
"schedules",
|
|
689
|
-
"daemon",
|
|
690
|
-
"run"
|
|
691
|
-
];
|
|
692
|
-
const pathEnv = process.env.PATH || "";
|
|
693
|
-
const kind = autostartKind();
|
|
694
|
-
const home = homedir();
|
|
695
|
-
const say = (m) => verbose && console.log(m);
|
|
696
|
-
let ok = false;
|
|
697
|
-
if (kind === "oxmgr") {
|
|
698
|
-
const q = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
699
|
-
ok = has("oxmgr", [
|
|
700
|
-
"start",
|
|
701
|
-
argv.map(q).join(" "),
|
|
702
|
-
"--name",
|
|
703
|
-
DAEMON_NAME,
|
|
704
|
-
"--restart",
|
|
705
|
-
"always"
|
|
706
|
-
]);
|
|
707
|
-
if (ok) {
|
|
708
|
-
const { ensureBootAutostart } = await import("./oxmgrService-o9L3I6FX.js");
|
|
709
|
-
await ensureBootAutostart(which("oxmgr")).catch(() => false);
|
|
710
|
-
}
|
|
711
|
-
} else if (kind === "systemd") {
|
|
712
|
-
writeAtomic(path.join(home, ".config", "systemd", "user", `${DAEMON_NAME}.service`), systemdDaemonUnit(argv, pathEnv));
|
|
713
|
-
ok = has("systemctl", ["--user", "daemon-reload"]) && has("systemctl", [
|
|
714
|
-
"--user",
|
|
715
|
-
"enable",
|
|
716
|
-
"--now",
|
|
717
|
-
`${DAEMON_NAME}.service`
|
|
718
|
-
]);
|
|
719
|
-
} else if (kind === "launchd") {
|
|
720
|
-
const f = path.join(home, "Library", "LaunchAgents", "com.agent-yes.schedulerd.plist");
|
|
721
|
-
writeAtomic(f, launchdDaemonPlist(argv, pathEnv, path.join(path.dirname(stateFile()), "schedulerd.log")));
|
|
722
|
-
ok = has("launchctl", [
|
|
723
|
-
"load",
|
|
724
|
-
"-w",
|
|
725
|
-
f
|
|
726
|
-
]);
|
|
727
|
-
} else if (kind === "schtasks") ok = has("schtasks", windowsTaskArgs(argv)) && has("schtasks", [
|
|
728
|
-
"/Run",
|
|
729
|
-
"/TN",
|
|
730
|
-
DAEMON_NAME
|
|
731
|
-
]);
|
|
732
|
-
if (!ok) {
|
|
733
|
-
const f = path.join(path.dirname(stateFile()), "schedulerd.log");
|
|
734
|
-
mkdirSync(path.dirname(f), { recursive: true });
|
|
735
|
-
const out = openSync(f, "a");
|
|
736
|
-
const child = spawn(argv[0], argv.slice(1), {
|
|
737
|
-
detached: true,
|
|
738
|
-
stdio: [
|
|
739
|
-
"ignore",
|
|
740
|
-
out,
|
|
741
|
-
out
|
|
742
|
-
]
|
|
743
|
-
});
|
|
744
|
-
child.unref();
|
|
745
|
-
say(`${DAEMON_NAME}: ${kind === "nohup" ? "" : `${kind} failed; `}started detached (pid ${child.pid}); not restarted on reboot`);
|
|
746
|
-
} else say(`${DAEMON_NAME}: installed via ${kind}`);
|
|
747
|
-
for (let i = 0; i < 20 && !daemonPid(); i++) await new Promise((r) => setTimeout(r, 250));
|
|
748
|
-
return !!daemonPid();
|
|
749
|
-
}
|
|
750
|
-
async function cmdSchedules(argv) {
|
|
751
|
-
const [sub, ...rest] = argv;
|
|
752
|
-
if (!sub || sub === "help" || sub === "--help" || sub === "-h") {
|
|
753
|
-
process.stdout.write(SCHEDULES_HELP);
|
|
754
|
-
return sub ? 0 : 1;
|
|
755
|
-
}
|
|
756
|
-
if (sub === "ls" || sub === "list") {
|
|
757
|
-
printEntries(loadStore().entries, loadState(), true);
|
|
758
|
-
const ox = which("oxmgr");
|
|
759
|
-
if (ox) {
|
|
760
|
-
const r = spawnSync(ox, ["list"], { encoding: "utf8" });
|
|
761
|
-
for (const row of legacyRows(r.stdout || "")) console.log(`source=schedule ${row.trim()}`);
|
|
762
|
-
}
|
|
763
|
-
const pid = daemonPid();
|
|
764
|
-
console.log(`\ndaemon: ${pid ? `running (pid ${pid})` : "not running (ay schedules daemon install)"}`);
|
|
765
|
-
return 0;
|
|
766
|
-
}
|
|
767
|
-
if (sub === "daemon") {
|
|
768
|
-
const what = rest[0] ?? "status";
|
|
769
|
-
if (what === "run") return daemonRun();
|
|
770
|
-
if (what === "install" || what === "start") return await ensureDaemon(true) ? 0 : 1;
|
|
771
|
-
if (what === "status") {
|
|
772
|
-
const pid = daemonPid();
|
|
773
|
-
console.log(pid ? `running (pid ${pid}); autostart: ${autostartKind()}` : `not running; autostart would use ${autostartKind()}`);
|
|
774
|
-
return pid ? 0 : 3;
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
console.error(`ay schedules: unknown command "${argv.join(" ")}"\n`);
|
|
778
|
-
process.stdout.write(SCHEDULES_HELP);
|
|
779
|
-
return 1;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
//#endregion
|
|
783
|
-
export { cmdCron, cmdSchedules };
|
|
784
|
-
//# sourceMappingURL=schedulesCli-CLqieyQZ.js.map
|
|
446
|
+
export { storeFile as _, launchdDaemonPlist as a, wakeDelay as b, nextCronId as c, parseState as d, parseStore as f, stateFile as g, resolveTarget as h, fmtTime as i, nextRuns as l, renderMessage as m, DAEMON_NAME as n, legacyRows as o, pickAutostart as p, SCHEDULES_HELP as r, logFile as s, CRON_HELP as t, parseCronAdd as u, systemdDaemonUnit as v, windowsTaskArgs as x, tick as y };
|
|
447
|
+
//# sourceMappingURL=schedules-BvwTGXU2.js.map
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { _ as storeFile, a as launchdDaemonPlist, b as wakeDelay, c as nextCronId, d as parseState, f as parseStore, g as stateFile, h as resolveTarget, i as fmtTime, l as nextRuns, m as renderMessage, n as DAEMON_NAME, o as legacyRows, p as pickAutostart, r as SCHEDULES_HELP, s as logFile, t as CRON_HELP, u as parseCronAdd, v as systemdDaemonUnit, x as windowsTaskArgs, y as tick } from "./schedules-BvwTGXU2.js";
|
|
2
|
+
import { spawn, spawnSync } from "child_process";
|
|
3
|
+
import { appendFileSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, watch, writeFileSync } from "fs";
|
|
4
|
+
import { homedir } from "os";
|
|
5
|
+
import path from "path";
|
|
6
|
+
|
|
7
|
+
//#region ts/schedulesCli.ts
|
|
8
|
+
function selfCmd() {
|
|
9
|
+
const argv1 = process.argv[1];
|
|
10
|
+
return argv1 && /\.(c|m)?[jt]s$/.test(argv1) ? [process.execPath, argv1] : [argv1 || "ay"];
|
|
11
|
+
}
|
|
12
|
+
function runSelf(args) {
|
|
13
|
+
const [cmd, ...pre] = selfCmd();
|
|
14
|
+
return spawnSync(cmd, [...pre, ...args], {
|
|
15
|
+
encoding: "utf8",
|
|
16
|
+
stdio: [
|
|
17
|
+
"ignore",
|
|
18
|
+
"pipe",
|
|
19
|
+
"pipe"
|
|
20
|
+
],
|
|
21
|
+
maxBuffer: 64 * 1024 * 1024
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function readText(f) {
|
|
25
|
+
return existsSync(f) ? readFileSync(f, "utf8") : null;
|
|
26
|
+
}
|
|
27
|
+
function writeAtomic(f, text) {
|
|
28
|
+
mkdirSync(path.dirname(f), { recursive: true });
|
|
29
|
+
const tmp = `${f}.${process.pid}.tmp`;
|
|
30
|
+
writeFileSync(tmp, text);
|
|
31
|
+
renameSync(tmp, f);
|
|
32
|
+
}
|
|
33
|
+
const loadStore = () => parseStore(readText(storeFile()));
|
|
34
|
+
const saveStore = (s) => writeAtomic(storeFile(), JSON.stringify(s, null, 2) + "\n");
|
|
35
|
+
const loadState = () => parseState(readText(stateFile()));
|
|
36
|
+
const saveState = (s) => writeAtomic(stateFile(), JSON.stringify(s) + "\n");
|
|
37
|
+
function log(e) {
|
|
38
|
+
const f = logFile();
|
|
39
|
+
mkdirSync(path.dirname(f), { recursive: true });
|
|
40
|
+
appendFileSync(f, JSON.stringify(e) + "\n");
|
|
41
|
+
}
|
|
42
|
+
/** Resolve the target and `ay send --force`. Returns the exit code (3 = target not live). */
|
|
43
|
+
function fire(e, now, manual = false) {
|
|
44
|
+
let live = [];
|
|
45
|
+
try {
|
|
46
|
+
live = JSON.parse(runSelf(["ls", "--json"]).stdout || "[]");
|
|
47
|
+
} catch {}
|
|
48
|
+
const r = resolveTarget(e.target, live, (lane) => {
|
|
49
|
+
const x = runSelf([
|
|
50
|
+
"lanes",
|
|
51
|
+
"id",
|
|
52
|
+
lane
|
|
53
|
+
]);
|
|
54
|
+
return {
|
|
55
|
+
ok: x.status === 0,
|
|
56
|
+
out: x.status === 0 ? x.stdout : (x.stderr || "").trim().split("\n")[0] || ""
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const base = {
|
|
60
|
+
ts: new Date(now).toISOString(),
|
|
61
|
+
name: e.name,
|
|
62
|
+
target: e.target,
|
|
63
|
+
...manual ? { manual } : {}
|
|
64
|
+
};
|
|
65
|
+
if (!r.ok) {
|
|
66
|
+
log({
|
|
67
|
+
...base,
|
|
68
|
+
ok: false,
|
|
69
|
+
code: 3,
|
|
70
|
+
error: r.error
|
|
71
|
+
});
|
|
72
|
+
console.error(`ay cron ${e.name}: ${r.error}; nothing sent`);
|
|
73
|
+
return 3;
|
|
74
|
+
}
|
|
75
|
+
const s = runSelf([
|
|
76
|
+
"send",
|
|
77
|
+
r.agentId,
|
|
78
|
+
renderMessage(e, now),
|
|
79
|
+
"--force"
|
|
80
|
+
]);
|
|
81
|
+
const code = s.status ?? 1;
|
|
82
|
+
log({
|
|
83
|
+
...base,
|
|
84
|
+
agent_id: r.agentId,
|
|
85
|
+
ok: code === 0,
|
|
86
|
+
code,
|
|
87
|
+
...code ? { error: (s.stderr || "").trim().slice(0, 500) } : {}
|
|
88
|
+
});
|
|
89
|
+
return code;
|
|
90
|
+
}
|
|
91
|
+
function lastStr(st, name, tz) {
|
|
92
|
+
const l = st.last[name];
|
|
93
|
+
return l ? `${fmtTime(l.at, tz)} ${l.ok ? "ok" : `exit ${l.code}`}` : "-";
|
|
94
|
+
}
|
|
95
|
+
function printEntries(entries, st, withSource) {
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
for (const e of entries) {
|
|
98
|
+
const next = e.enabled ? nextRuns(e.cron, e.tz, now, 1)[0] : void 0;
|
|
99
|
+
console.log([
|
|
100
|
+
e.name.padEnd(6),
|
|
101
|
+
...withSource ? [`source=${e.source}`] : [],
|
|
102
|
+
`"${e.cron}" ${e.tz}`,
|
|
103
|
+
e.target,
|
|
104
|
+
e.enabled ? "" : "(disabled)",
|
|
105
|
+
`next ${next ? fmtTime(next, e.tz) : "-"}`,
|
|
106
|
+
`last ${lastStr(st, e.name, e.tz)}`,
|
|
107
|
+
`: ${e.message.length > 60 ? e.message.slice(0, 59) + "…" : e.message}`
|
|
108
|
+
].filter(Boolean).join(" "));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function cmdCron(argv) {
|
|
112
|
+
const [sub, ...rest] = argv;
|
|
113
|
+
if (!sub || sub === "help" || sub === "--help" || sub === "-h") {
|
|
114
|
+
process.stdout.write(CRON_HELP);
|
|
115
|
+
return sub ? 0 : 1;
|
|
116
|
+
}
|
|
117
|
+
if (sub === "ls" || sub === "list") {
|
|
118
|
+
const st = loadState();
|
|
119
|
+
const entries = loadStore().entries.filter((e) => e.source === "cron");
|
|
120
|
+
if (!entries.length) console.log("no reminders (ay cron help)");
|
|
121
|
+
printEntries(entries, st, false);
|
|
122
|
+
if (entries.length && !daemonPid()) console.log(`\nwarning: the scheduler daemon is not running (ay schedules daemon install)`);
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
if (sub === "rm" || sub === "remove") {
|
|
126
|
+
const id = rest[0];
|
|
127
|
+
const store = loadStore();
|
|
128
|
+
const hit = store.entries.find((e) => e.name === id && e.source === "cron");
|
|
129
|
+
if (!id || !hit) {
|
|
130
|
+
console.error(`ay cron rm: no reminder "${id ?? ""}" (see ay cron ls)`);
|
|
131
|
+
return 2;
|
|
132
|
+
}
|
|
133
|
+
saveStore({
|
|
134
|
+
...store,
|
|
135
|
+
entries: store.entries.filter((e) => e !== hit)
|
|
136
|
+
});
|
|
137
|
+
console.log(`removed ${id}`);
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
const p = parseCronAdd(argv);
|
|
141
|
+
if (!p.ok) {
|
|
142
|
+
console.error(`ay cron: ${p.error}`);
|
|
143
|
+
return 1;
|
|
144
|
+
}
|
|
145
|
+
const store = loadStore();
|
|
146
|
+
const name = nextCronId(store);
|
|
147
|
+
const entry = {
|
|
148
|
+
name,
|
|
149
|
+
cron: p.cron,
|
|
150
|
+
tz: p.tz,
|
|
151
|
+
target: p.target,
|
|
152
|
+
message: p.message,
|
|
153
|
+
enabled: true,
|
|
154
|
+
missed: "skip",
|
|
155
|
+
source: "cron",
|
|
156
|
+
since: Date.now()
|
|
157
|
+
};
|
|
158
|
+
saveStore({
|
|
159
|
+
...store,
|
|
160
|
+
entries: [...store.entries, entry]
|
|
161
|
+
});
|
|
162
|
+
const next = nextRuns(p.cron, p.tz, Date.now(), 1)[0];
|
|
163
|
+
console.log(`added ${name}: "${p.cron}" ${p.tz} -> ${p.target}; next ${next ? fmtTime(next, p.tz) : "-"}`);
|
|
164
|
+
if (!await ensureDaemon(false)) console.log(`warning: could not start the scheduler daemon (ay schedules daemon install)`);
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
const pidFile = () => path.join(path.dirname(stateFile()), "schedulerd.pid");
|
|
168
|
+
function alive(pid) {
|
|
169
|
+
try {
|
|
170
|
+
process.kill(pid, 0);
|
|
171
|
+
return true;
|
|
172
|
+
} catch (e) {
|
|
173
|
+
return e.code === "EPERM";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function daemonPid() {
|
|
177
|
+
const pid = Number(readText(pidFile())?.trim());
|
|
178
|
+
return pid && alive(pid) ? pid : null;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Foreground loop, one per machine (pid file). Sleeps until the earliest next fire
|
|
182
|
+
* (capped at MAX_SLEEP_MS), re-arms after each fire, and wakes at once when the
|
|
183
|
+
* store file changes (fs.watch on its directory: writes are atomic renames).
|
|
184
|
+
*/
|
|
185
|
+
async function daemonRun() {
|
|
186
|
+
const other = daemonPid();
|
|
187
|
+
if (other && other !== process.pid) {
|
|
188
|
+
console.error(`${DAEMON_NAME}: already running (pid ${other})`);
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
writeAtomic(pidFile(), `${process.pid}\n`);
|
|
192
|
+
const sf = storeFile();
|
|
193
|
+
mkdirSync(path.dirname(sf), { recursive: true });
|
|
194
|
+
console.error(`${DAEMON_NAME}: started pid ${process.pid}, store ${sf}`);
|
|
195
|
+
let wake = null;
|
|
196
|
+
watch(path.dirname(sf), (_ev, file) => {
|
|
197
|
+
if (!file || file.toString() === path.basename(sf)) wake?.();
|
|
198
|
+
});
|
|
199
|
+
for (;;) {
|
|
200
|
+
const now = Date.now();
|
|
201
|
+
const entries = loadStore().entries;
|
|
202
|
+
const t = tick(entries, loadState(), now);
|
|
203
|
+
if (t.clockBack) console.error(`${DAEMON_NAME}: clock went backwards; resuming from ${new Date(now).toISOString()}`);
|
|
204
|
+
for (const e of t.fire) {
|
|
205
|
+
const code = fire(e, now);
|
|
206
|
+
t.state.last[e.name] = {
|
|
207
|
+
at: now,
|
|
208
|
+
ok: code === 0,
|
|
209
|
+
code
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
saveState(t.state);
|
|
213
|
+
const ms = wakeDelay(entries, Date.now());
|
|
214
|
+
await new Promise((r) => {
|
|
215
|
+
const timer = setTimeout(done, ms);
|
|
216
|
+
function done() {
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
wake = null;
|
|
219
|
+
r();
|
|
220
|
+
}
|
|
221
|
+
wake = done;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function has(cmd, args) {
|
|
226
|
+
return spawnSync(cmd, args, { stdio: "ignore" }).status === 0;
|
|
227
|
+
}
|
|
228
|
+
function which(cmd) {
|
|
229
|
+
const r = spawnSync(process.platform === "win32" ? "where" : "which", [cmd], { encoding: "utf8" });
|
|
230
|
+
return r.status === 0 ? r.stdout.split(/\r?\n/)[0].trim() || null : null;
|
|
231
|
+
}
|
|
232
|
+
function autostartKind() {
|
|
233
|
+
return pickAutostart({
|
|
234
|
+
platform: process.platform,
|
|
235
|
+
hasOxmgr: !!which("oxmgr"),
|
|
236
|
+
systemdUser: process.platform === "linux" && has("systemctl", ["--user", "show-environment"])
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/** Start the daemon and register it to stay alive. Returns whether it is running. */
|
|
240
|
+
async function ensureDaemon(verbose) {
|
|
241
|
+
if (daemonPid()) {
|
|
242
|
+
if (verbose) console.log(`${DAEMON_NAME}: already running (pid ${daemonPid()})`);
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
const argv = [
|
|
246
|
+
...selfCmd(),
|
|
247
|
+
"schedules",
|
|
248
|
+
"daemon",
|
|
249
|
+
"run"
|
|
250
|
+
];
|
|
251
|
+
const pathEnv = process.env.PATH || "";
|
|
252
|
+
const kind = autostartKind();
|
|
253
|
+
const home = homedir();
|
|
254
|
+
const say = (m) => verbose && console.log(m);
|
|
255
|
+
let ok = false;
|
|
256
|
+
if (kind === "oxmgr") {
|
|
257
|
+
const q = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
258
|
+
ok = has("oxmgr", [
|
|
259
|
+
"start",
|
|
260
|
+
argv.map(q).join(" "),
|
|
261
|
+
"--name",
|
|
262
|
+
DAEMON_NAME,
|
|
263
|
+
"--restart",
|
|
264
|
+
"always"
|
|
265
|
+
]);
|
|
266
|
+
if (ok) {
|
|
267
|
+
const { ensureBootAutostart } = await import("./oxmgrService-o9L3I6FX.js");
|
|
268
|
+
await ensureBootAutostart(which("oxmgr")).catch(() => false);
|
|
269
|
+
}
|
|
270
|
+
} else if (kind === "systemd") {
|
|
271
|
+
writeAtomic(path.join(home, ".config", "systemd", "user", `${DAEMON_NAME}.service`), systemdDaemonUnit(argv, pathEnv));
|
|
272
|
+
ok = has("systemctl", ["--user", "daemon-reload"]) && has("systemctl", [
|
|
273
|
+
"--user",
|
|
274
|
+
"enable",
|
|
275
|
+
"--now",
|
|
276
|
+
`${DAEMON_NAME}.service`
|
|
277
|
+
]);
|
|
278
|
+
} else if (kind === "launchd") {
|
|
279
|
+
const f = path.join(home, "Library", "LaunchAgents", "com.agent-yes.schedulerd.plist");
|
|
280
|
+
writeAtomic(f, launchdDaemonPlist(argv, pathEnv, path.join(path.dirname(stateFile()), "schedulerd.log")));
|
|
281
|
+
ok = has("launchctl", [
|
|
282
|
+
"load",
|
|
283
|
+
"-w",
|
|
284
|
+
f
|
|
285
|
+
]);
|
|
286
|
+
} else if (kind === "schtasks") ok = has("schtasks", windowsTaskArgs(argv)) && has("schtasks", [
|
|
287
|
+
"/Run",
|
|
288
|
+
"/TN",
|
|
289
|
+
DAEMON_NAME
|
|
290
|
+
]);
|
|
291
|
+
if (!ok) {
|
|
292
|
+
const f = path.join(path.dirname(stateFile()), "schedulerd.log");
|
|
293
|
+
mkdirSync(path.dirname(f), { recursive: true });
|
|
294
|
+
const out = openSync(f, "a");
|
|
295
|
+
const child = spawn(argv[0], argv.slice(1), {
|
|
296
|
+
detached: true,
|
|
297
|
+
stdio: [
|
|
298
|
+
"ignore",
|
|
299
|
+
out,
|
|
300
|
+
out
|
|
301
|
+
]
|
|
302
|
+
});
|
|
303
|
+
child.unref();
|
|
304
|
+
say(`${DAEMON_NAME}: ${kind === "nohup" ? "" : `${kind} failed; `}started detached (pid ${child.pid}); not restarted on reboot`);
|
|
305
|
+
} else say(`${DAEMON_NAME}: installed via ${kind}`);
|
|
306
|
+
for (let i = 0; i < 20 && !daemonPid(); i++) await new Promise((r) => setTimeout(r, 250));
|
|
307
|
+
return !!daemonPid();
|
|
308
|
+
}
|
|
309
|
+
async function cmdSchedules(argv) {
|
|
310
|
+
const [sub, ...rest] = argv;
|
|
311
|
+
if (!sub || sub === "help" || sub === "--help" || sub === "-h") {
|
|
312
|
+
process.stdout.write(SCHEDULES_HELP);
|
|
313
|
+
return sub ? 0 : 1;
|
|
314
|
+
}
|
|
315
|
+
if (sub === "ls" || sub === "list") {
|
|
316
|
+
printEntries(loadStore().entries, loadState(), true);
|
|
317
|
+
const ox = which("oxmgr");
|
|
318
|
+
if (ox) {
|
|
319
|
+
const r = spawnSync(ox, ["list"], { encoding: "utf8" });
|
|
320
|
+
for (const row of legacyRows(r.stdout || "")) console.log(`source=schedule ${row.trim()}`);
|
|
321
|
+
}
|
|
322
|
+
const pid = daemonPid();
|
|
323
|
+
console.log(`\ndaemon: ${pid ? `running (pid ${pid})` : "not running (ay schedules daemon install)"}`);
|
|
324
|
+
return 0;
|
|
325
|
+
}
|
|
326
|
+
if (sub === "daemon") {
|
|
327
|
+
const what = rest[0] ?? "status";
|
|
328
|
+
if (what === "run") return daemonRun();
|
|
329
|
+
if (what === "install" || what === "start") return await ensureDaemon(true) ? 0 : 1;
|
|
330
|
+
if (what === "status") {
|
|
331
|
+
const pid = daemonPid();
|
|
332
|
+
console.log(pid ? `running (pid ${pid}); autostart: ${autostartKind()}` : `not running; autostart would use ${autostartKind()}`);
|
|
333
|
+
return pid ? 0 : 3;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
console.error(`ay schedules: unknown command "${argv.join(" ")}"\n`);
|
|
337
|
+
process.stdout.write(SCHEDULES_HELP);
|
|
338
|
+
return 1;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
//#endregion
|
|
342
|
+
export { cmdCron, cmdSchedules };
|
|
343
|
+
//# sourceMappingURL=schedulesCli-Donh0LS2.js.map
|