File size: 24,173 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 | import { spawn, spawnSync } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import { isGatewayArgv } from "../infra/gateway-process-argv.js";
import { findVerifiedGatewayListenerPidsOnPortSync } from "../infra/gateway-processes.js";
import { inspectPortUsage } from "../infra/ports.js";
import { killProcessTree } from "../process/kill-tree.js";
import { sleep } from "../utils.js";
import { parseCmdScriptCommandLine, quoteCmdScriptArg } from "./cmd-argv.js";
import { assertNoCmdLineBreak, parseCmdSetAssignment, renderCmdSetAssignment } from "./cmd-set.js";
import { resolveGatewayServiceDescription, resolveGatewayWindowsTaskName } from "./constants.js";
import { formatLine, writeFormattedLines } from "./output.js";
import { resolveGatewayStateDir } from "./paths.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import { execSchtasks } from "./schtasks-exec.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import type {
GatewayServiceCommandConfig,
GatewayServiceControlArgs,
GatewayServiceEnv,
GatewayServiceEnvArgs,
GatewayServiceInstallArgs,
GatewayServiceManageArgs,
GatewayServiceRenderArgs,
GatewayServiceRestartResult,
} from "./service-types.js";
function resolveTaskName(env: GatewayServiceEnv): string {
const override = env.OPENCLAW_WINDOWS_TASK_NAME?.trim();
if (override) {
return override;
}
return resolveGatewayWindowsTaskName(env.OPENCLAW_PROFILE);
}
function shouldFallbackToStartupEntry(params: { code: number; detail: string }): boolean {
return (
/access is denied/i.test(params.detail) ||
params.code === 124 ||
/schtasks timed out/i.test(params.detail) ||
/schtasks produced no output/i.test(params.detail)
);
}
export function resolveTaskScriptPath(env: GatewayServiceEnv): string {
const override = env.OPENCLAW_TASK_SCRIPT?.trim();
if (override) {
return override;
}
const scriptName = env.OPENCLAW_TASK_SCRIPT_NAME?.trim() || "gateway.cmd";
const stateDir = resolveGatewayStateDir(env);
return path.join(stateDir, scriptName);
}
function resolveWindowsStartupDir(env: GatewayServiceEnv): string {
const appData = env.APPDATA?.trim();
if (appData) {
return path.join(appData, "Microsoft", "Windows", "Start Menu", "Programs", "Startup");
}
const home = env.USERPROFILE?.trim() || env.HOME?.trim();
if (!home) {
throw new Error("Windows startup folder unavailable: APPDATA/USERPROFILE not set");
}
return path.join(
home,
"AppData",
"Roaming",
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"Startup",
);
}
function sanitizeWindowsFilename(value: string): string {
return value.replace(/[<>:"/\\|?*]/g, "_").replace(/\p{Cc}/gu, "_");
}
function resolveStartupEntryPath(env: GatewayServiceEnv): string {
const taskName = resolveTaskName(env);
return path.join(resolveWindowsStartupDir(env), `${sanitizeWindowsFilename(taskName)}.cmd`);
}
// `/TR` is parsed by schtasks itself, while the generated `gateway.cmd` line is parsed by cmd.exe.
// Keep their quoting strategies separate so each parser gets the encoding it expects.
function quoteSchtasksArg(value: string): string {
if (!/[ \t"]/g.test(value)) {
return value;
}
return `"${value.replace(/"/g, '\\"')}"`;
}
function resolveTaskUser(env: GatewayServiceEnv): string | null {
const username = env.USERNAME || env.USER || env.LOGNAME;
if (!username) {
return null;
}
if (username.includes("\\")) {
return username;
}
const domain = env.USERDOMAIN;
if (domain) {
return `${domain}\\${username}`;
}
return username;
}
export async function readScheduledTaskCommand(
env: GatewayServiceEnv,
): Promise<GatewayServiceCommandConfig | null> {
const scriptPath = resolveTaskScriptPath(env);
try {
const content = await fs.readFile(scriptPath, "utf8");
let workingDirectory = "";
let commandLine = "";
const environment: Record<string, string> = {};
for (const rawLine of content.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line) {
continue;
}
const lower = line.toLowerCase();
if (line.startsWith("@echo")) {
continue;
}
if (lower.startsWith("rem ")) {
continue;
}
if (lower.startsWith("set ")) {
const assignment = parseCmdSetAssignment(line.slice(4));
if (assignment) {
environment[assignment.key] = assignment.value;
}
continue;
}
if (lower.startsWith("cd /d ")) {
workingDirectory = line.slice("cd /d ".length).trim().replace(/^"|"$/g, "");
continue;
}
commandLine = line;
break;
}
if (!commandLine) {
return null;
}
return {
programArguments: parseCmdScriptCommandLine(commandLine),
...(workingDirectory ? { workingDirectory } : {}),
...(Object.keys(environment).length > 0 ? { environment } : {}),
sourcePath: scriptPath,
};
} catch {
return null;
}
}
export type ScheduledTaskInfo = {
status?: string;
lastRunTime?: string;
lastRunResult?: string;
};
function hasListenerPid<T extends { pid?: number | null }>(
listener: T,
): listener is T & { pid: number } {
return typeof listener.pid === "number";
}
export function parseSchtasksQuery(output: string): ScheduledTaskInfo {
const entries = parseKeyValueOutput(output, ":");
const info: ScheduledTaskInfo = {};
const status = entries.status;
if (status) {
info.status = status;
}
const lastRunTime = entries["last run time"];
if (lastRunTime) {
info.lastRunTime = lastRunTime;
}
const lastRunResult = entries["last run result"];
if (lastRunResult) {
info.lastRunResult = lastRunResult;
}
return info;
}
function normalizeTaskResultCode(value?: string): string | null {
if (!value) {
return null;
}
const raw = value.trim().toLowerCase();
if (!raw) {
return null;
}
if (/^0x[0-9a-f]+$/.test(raw)) {
return `0x${raw.slice(2).replace(/^0+/, "") || "0"}`;
}
if (/^\d+$/.test(raw)) {
const numeric = Number.parseInt(raw, 10);
if (Number.isFinite(numeric)) {
return `0x${numeric.toString(16)}`;
}
}
return null;
}
const RUNNING_RESULT_CODES = new Set(["0x41301"]);
const UNKNOWN_STATUS_DETAIL =
"Task status is locale-dependent and no numeric Last Run Result was available.";
export function deriveScheduledTaskRuntimeStatus(parsed: ScheduledTaskInfo): {
status: GatewayServiceRuntime["status"];
detail?: string;
} {
const normalizedResult = normalizeTaskResultCode(parsed.lastRunResult);
if (normalizedResult != null) {
if (RUNNING_RESULT_CODES.has(normalizedResult)) {
return { status: "running" };
}
return {
status: "stopped",
detail: `Task Last Run Result=${parsed.lastRunResult}; treating as not running.`,
};
}
if (parsed.status?.trim()) {
return { status: "unknown", detail: UNKNOWN_STATUS_DETAIL };
}
return { status: "unknown" };
}
function buildTaskScript({
description,
programArguments,
workingDirectory,
environment,
}: GatewayServiceRenderArgs): string {
const lines: string[] = ["@echo off"];
const trimmedDescription = description?.trim();
if (trimmedDescription) {
assertNoCmdLineBreak(trimmedDescription, "Task description");
lines.push(`rem ${trimmedDescription}`);
}
if (workingDirectory) {
lines.push(`cd /d ${quoteCmdScriptArg(workingDirectory)}`);
}
if (environment) {
for (const [key, value] of Object.entries(environment)) {
if (!value) {
continue;
}
if (key.toUpperCase() === "PATH") {
continue;
}
lines.push(renderCmdSetAssignment(key, value));
}
}
const command = programArguments.map(quoteCmdScriptArg).join(" ");
lines.push(command);
return `${lines.join("\r\n")}\r\n`;
}
function buildStartupLauncherScript(params: { description?: string; scriptPath: string }): string {
const lines = ["@echo off"];
const trimmedDescription = params.description?.trim();
if (trimmedDescription) {
assertNoCmdLineBreak(trimmedDescription, "Startup launcher description");
lines.push(`rem ${trimmedDescription}`);
}
lines.push(`start "" /min cmd.exe /d /c ${quoteCmdScriptArg(params.scriptPath)}`);
return `${lines.join("\r\n")}\r\n`;
}
async function assertSchtasksAvailable() {
const res = await execSchtasks(["/Query"]);
if (res.code === 0) {
return;
}
const detail = res.stderr || res.stdout;
throw new Error(`schtasks unavailable: ${detail || "unknown error"}`.trim());
}
async function isStartupEntryInstalled(env: GatewayServiceEnv): Promise<boolean> {
try {
await fs.access(resolveStartupEntryPath(env));
return true;
} catch {
return false;
}
}
async function isRegisteredScheduledTask(env: GatewayServiceEnv): Promise<boolean> {
const taskName = resolveTaskName(env);
const res = await execSchtasks(["/Query", "/TN", taskName]).catch(() => ({
code: 1,
stdout: "",
stderr: "",
}));
return res.code === 0;
}
function launchFallbackTaskScript(scriptPath: string): void {
const child = spawn("cmd.exe", ["/d", "/s", "/c", quoteCmdScriptArg(scriptPath)], {
detached: true,
stdio: "ignore",
windowsHide: true,
});
child.unref();
}
function resolveConfiguredGatewayPort(env: GatewayServiceEnv): number | null {
const raw = env.OPENCLAW_GATEWAY_PORT?.trim();
if (!raw) {
return null;
}
const parsed = Number.parseInt(raw, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
}
function parsePositivePort(raw: string | undefined): number | null {
const value = raw?.trim();
if (!value) {
return null;
}
if (!/^\d+$/.test(value)) {
return null;
}
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed > 0 && parsed <= 65535 ? parsed : null;
}
function parsePortFromProgramArguments(programArguments?: string[]): number | null {
if (!programArguments?.length) {
return null;
}
for (let i = 0; i < programArguments.length; i += 1) {
const arg = programArguments[i];
if (!arg) {
continue;
}
const inlineMatch = arg.match(/^--port=(\d+)$/);
if (inlineMatch) {
return parsePositivePort(inlineMatch[1]);
}
if (arg === "--port") {
return parsePositivePort(programArguments[i + 1]);
}
}
return null;
}
async function resolveScheduledTaskPort(env: GatewayServiceEnv): Promise<number | null> {
const command = await readScheduledTaskCommand(env).catch(() => null);
return (
parsePortFromProgramArguments(command?.programArguments) ??
parsePositivePort(command?.environment?.OPENCLAW_GATEWAY_PORT) ??
resolveConfiguredGatewayPort(env)
);
}
async function resolveScheduledTaskGatewayListenerPids(port: number): Promise<number[]> {
const verified = findVerifiedGatewayListenerPidsOnPortSync(port);
if (verified.length > 0) {
return verified;
}
const diagnostics = await inspectPortUsage(port).catch(() => null);
if (diagnostics?.status !== "busy") {
return [];
}
const matchedGatewayPids = Array.from(
new Set(
diagnostics.listeners
.filter(
(listener) =>
typeof listener.pid === "number" &&
listener.commandLine &&
isGatewayArgv(parseCmdScriptCommandLine(listener.commandLine), {
allowGatewayBinary: true,
}),
)
.map((listener) => listener.pid as number),
),
);
if (matchedGatewayPids.length > 0) {
return matchedGatewayPids;
}
return Array.from(
new Set(
diagnostics.listeners
.map((listener) => listener.pid)
.filter((pid): pid is number => typeof pid === "number" && Number.isFinite(pid) && pid > 0),
),
);
}
async function terminateScheduledTaskGatewayListeners(env: GatewayServiceEnv): Promise<number[]> {
const port = await resolveScheduledTaskPort(env);
if (!port) {
return [];
}
const pids = await resolveScheduledTaskGatewayListenerPids(port);
for (const pid of pids) {
await terminateGatewayProcessTree(pid, 300);
}
return pids;
}
function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
async function waitForProcessExit(pid: number, timeoutMs: number): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (!isProcessAlive(pid)) {
return true;
}
await sleep(100);
}
return !isProcessAlive(pid);
}
async function terminateGatewayProcessTree(pid: number, graceMs: number): Promise<void> {
if (process.platform !== "win32") {
killProcessTree(pid, { graceMs });
return;
}
const taskkillPath = path.join(
process.env.SystemRoot ?? "C:\\Windows",
"System32",
"taskkill.exe",
);
spawnSync(taskkillPath, ["/T", "/PID", String(pid)], {
stdio: "ignore",
timeout: 5_000,
windowsHide: true,
});
if (await waitForProcessExit(pid, graceMs)) {
return;
}
spawnSync(taskkillPath, ["/F", "/T", "/PID", String(pid)], {
stdio: "ignore",
timeout: 5_000,
windowsHide: true,
});
await waitForProcessExit(pid, 5_000);
}
async function waitForGatewayPortRelease(port: number, timeoutMs = 5_000): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const diagnostics = await inspectPortUsage(port).catch(() => null);
if (diagnostics?.status === "free") {
return true;
}
await sleep(250);
}
return false;
}
async function terminateBusyPortListeners(port: number): Promise<number[]> {
const diagnostics = await inspectPortUsage(port).catch(() => null);
if (diagnostics?.status !== "busy") {
return [];
}
const pids = Array.from(
new Set(
diagnostics.listeners
.map((listener) => listener.pid)
.filter((pid): pid is number => typeof pid === "number" && Number.isFinite(pid) && pid > 0),
),
);
for (const pid of pids) {
await terminateGatewayProcessTree(pid, 300);
}
return pids;
}
async function resolveFallbackRuntime(env: GatewayServiceEnv): Promise<GatewayServiceRuntime> {
const port = (await resolveScheduledTaskPort(env)) ?? resolveConfiguredGatewayPort(env);
if (!port) {
return {
status: "unknown",
detail: "Startup-folder login item installed; gateway port unknown.",
};
}
const diagnostics = await inspectPortUsage(port).catch(() => null);
if (!diagnostics) {
return {
status: "unknown",
detail: `Startup-folder login item installed; could not inspect port ${port}.`,
};
}
const listener = diagnostics.listeners.find(hasListenerPid);
return {
status: diagnostics.status === "busy" ? "running" : "stopped",
...(listener?.pid ? { pid: listener.pid } : {}),
detail:
diagnostics.status === "busy"
? `Startup-folder login item installed; listener detected on port ${port}.`
: `Startup-folder login item installed; no listener detected on port ${port}.`,
};
}
async function stopStartupEntry(
env: GatewayServiceEnv,
stdout: NodeJS.WritableStream,
): Promise<void> {
const runtime = await resolveFallbackRuntime(env);
if (typeof runtime.pid === "number" && runtime.pid > 0) {
await terminateGatewayProcessTree(runtime.pid, 300);
}
stdout.write(`${formatLine("Stopped Windows login item", resolveTaskName(env))}\n`);
}
async function terminateInstalledStartupRuntime(env: GatewayServiceEnv): Promise<void> {
if (!(await isStartupEntryInstalled(env))) {
return;
}
const runtime = await resolveFallbackRuntime(env);
if (typeof runtime.pid === "number" && runtime.pid > 0) {
await terminateGatewayProcessTree(runtime.pid, 300);
}
}
async function restartStartupEntry(
env: GatewayServiceEnv,
stdout: NodeJS.WritableStream,
): Promise<GatewayServiceRestartResult> {
const runtime = await resolveFallbackRuntime(env);
if (typeof runtime.pid === "number" && runtime.pid > 0) {
await terminateGatewayProcessTree(runtime.pid, 300);
}
launchFallbackTaskScript(resolveTaskScriptPath(env));
stdout.write(`${formatLine("Restarted Windows login item", resolveTaskName(env))}\n`);
return { outcome: "completed" };
}
export async function installScheduledTask({
env,
stdout,
programArguments,
workingDirectory,
environment,
description,
}: GatewayServiceInstallArgs): Promise<{ scriptPath: string }> {
await assertSchtasksAvailable();
const scriptPath = resolveTaskScriptPath(env);
await fs.mkdir(path.dirname(scriptPath), { recursive: true });
const taskDescription = resolveGatewayServiceDescription({ env, environment, description });
const script = buildTaskScript({
description: taskDescription,
programArguments,
workingDirectory,
environment,
});
await fs.writeFile(scriptPath, script, "utf8");
const taskName = resolveTaskName(env);
const quotedScript = quoteSchtasksArg(scriptPath);
const baseArgs = [
"/Create",
"/F",
"/SC",
"ONLOGON",
"/RL",
"LIMITED",
"/TN",
taskName,
"/TR",
quotedScript,
];
const taskUser = resolveTaskUser(env);
let create = await execSchtasks(
taskUser ? [...baseArgs, "/RU", taskUser, "/NP", "/IT"] : baseArgs,
);
if (create.code !== 0 && taskUser) {
create = await execSchtasks(baseArgs);
}
if (create.code !== 0) {
const detail = create.stderr || create.stdout;
if (shouldFallbackToStartupEntry({ code: create.code, detail })) {
const startupEntryPath = resolveStartupEntryPath(env);
await fs.mkdir(path.dirname(startupEntryPath), { recursive: true });
const launcher = buildStartupLauncherScript({ description: taskDescription, scriptPath });
await fs.writeFile(startupEntryPath, launcher, "utf8");
launchFallbackTaskScript(scriptPath);
writeFormattedLines(
stdout,
[
{ label: "Installed Windows login item", value: startupEntryPath },
{ label: "Task script", value: scriptPath },
],
{ leadingBlankLine: true },
);
return { scriptPath };
}
throw new Error(`schtasks create failed: ${detail}`.trim());
}
await execSchtasks(["/Run", "/TN", taskName]);
// Ensure we don't end up writing to a clack spinner line (wizards show progress without a newline).
writeFormattedLines(
stdout,
[
{ label: "Installed Scheduled Task", value: taskName },
{ label: "Task script", value: scriptPath },
],
{ leadingBlankLine: true },
);
return { scriptPath };
}
export async function uninstallScheduledTask({
env,
stdout,
}: GatewayServiceManageArgs): Promise<void> {
await assertSchtasksAvailable();
const taskName = resolveTaskName(env);
const taskInstalled = await isRegisteredScheduledTask(env).catch(() => false);
if (taskInstalled) {
await execSchtasks(["/Delete", "/F", "/TN", taskName]);
}
const startupEntryPath = resolveStartupEntryPath(env);
try {
await fs.unlink(startupEntryPath);
stdout.write(`${formatLine("Removed Windows login item", startupEntryPath)}\n`);
} catch {}
const scriptPath = resolveTaskScriptPath(env);
try {
await fs.unlink(scriptPath);
stdout.write(`${formatLine("Removed task script", scriptPath)}\n`);
} catch {
stdout.write(`Task script not found at ${scriptPath}\n`);
}
}
function isTaskNotRunning(res: { stdout: string; stderr: string; code: number }): boolean {
const detail = (res.stderr || res.stdout).toLowerCase();
return detail.includes("not running");
}
export async function stopScheduledTask({ stdout, env }: GatewayServiceControlArgs): Promise<void> {
const effectiveEnv = env ?? (process.env as GatewayServiceEnv);
try {
await assertSchtasksAvailable();
} catch (err) {
if (await isStartupEntryInstalled(effectiveEnv)) {
await stopStartupEntry(effectiveEnv, stdout);
return;
}
throw err;
}
if (!(await isRegisteredScheduledTask(effectiveEnv))) {
if (await isStartupEntryInstalled(effectiveEnv)) {
await stopStartupEntry(effectiveEnv, stdout);
return;
}
}
const taskName = resolveTaskName(effectiveEnv);
const res = await execSchtasks(["/End", "/TN", taskName]);
if (res.code !== 0 && !isTaskNotRunning(res)) {
throw new Error(`schtasks end failed: ${res.stderr || res.stdout}`.trim());
}
const stopPort = await resolveScheduledTaskPort(effectiveEnv);
await terminateScheduledTaskGatewayListeners(effectiveEnv);
await terminateInstalledStartupRuntime(effectiveEnv);
if (stopPort) {
const released = await waitForGatewayPortRelease(stopPort);
if (!released) {
await terminateBusyPortListeners(stopPort);
const releasedAfterForce = await waitForGatewayPortRelease(stopPort, 2_000);
if (!releasedAfterForce) {
throw new Error(`gateway port ${stopPort} is still busy after stop`);
}
}
}
stdout.write(`${formatLine("Stopped Scheduled Task", taskName)}\n`);
}
export async function restartScheduledTask({
stdout,
env,
}: GatewayServiceControlArgs): Promise<GatewayServiceRestartResult> {
const effectiveEnv = env ?? (process.env as GatewayServiceEnv);
try {
await assertSchtasksAvailable();
} catch (err) {
if (await isStartupEntryInstalled(effectiveEnv)) {
return await restartStartupEntry(effectiveEnv, stdout);
}
throw err;
}
if (!(await isRegisteredScheduledTask(effectiveEnv))) {
if (await isStartupEntryInstalled(effectiveEnv)) {
return await restartStartupEntry(effectiveEnv, stdout);
}
}
const taskName = resolveTaskName(effectiveEnv);
await execSchtasks(["/End", "/TN", taskName]);
const restartPort = await resolveScheduledTaskPort(effectiveEnv);
await terminateScheduledTaskGatewayListeners(effectiveEnv);
await terminateInstalledStartupRuntime(effectiveEnv);
if (restartPort) {
const released = await waitForGatewayPortRelease(restartPort);
if (!released) {
await terminateBusyPortListeners(restartPort);
const releasedAfterForce = await waitForGatewayPortRelease(restartPort, 2_000);
if (!releasedAfterForce) {
throw new Error(`gateway port ${restartPort} is still busy before restart`);
}
}
}
const res = await execSchtasks(["/Run", "/TN", taskName]);
if (res.code !== 0) {
throw new Error(`schtasks run failed: ${res.stderr || res.stdout}`.trim());
}
stdout.write(`${formatLine("Restarted Scheduled Task", taskName)}\n`);
return { outcome: "completed" };
}
export async function isScheduledTaskInstalled(args: GatewayServiceEnvArgs): Promise<boolean> {
const effectiveEnv = args.env ?? (process.env as GatewayServiceEnv);
if (await isRegisteredScheduledTask(effectiveEnv)) {
return true;
}
return await isStartupEntryInstalled(effectiveEnv);
}
export async function readScheduledTaskRuntime(
env: GatewayServiceEnv = process.env as GatewayServiceEnv,
): Promise<GatewayServiceRuntime> {
try {
await assertSchtasksAvailable();
} catch (err) {
if (await isStartupEntryInstalled(env)) {
return await resolveFallbackRuntime(env);
}
return {
status: "unknown",
detail: String(err),
};
}
const taskName = resolveTaskName(env);
const res = await execSchtasks(["/Query", "/TN", taskName, "/V", "/FO", "LIST"]);
if (res.code !== 0) {
if (await isStartupEntryInstalled(env)) {
return await resolveFallbackRuntime(env);
}
const detail = (res.stderr || res.stdout).trim();
const missing = detail.toLowerCase().includes("cannot find the file");
return {
status: missing ? "stopped" : "unknown",
detail: detail || undefined,
missingUnit: missing,
};
}
const parsed = parseSchtasksQuery(res.stdout || "");
const derived = deriveScheduledTaskRuntimeStatus(parsed);
return {
status: derived.status,
state: parsed.status,
lastRunTime: parsed.lastRunTime,
lastRunResult: parsed.lastRunResult,
...(derived.detail ? { detail: derived.detail } : {}),
};
}
|