mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(daemon): share schtasks exec helper
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import {
|
||||
GATEWAY_SERVICE_KIND,
|
||||
GATEWAY_SERVICE_MARKER,
|
||||
@@ -9,6 +7,7 @@ import {
|
||||
resolveGatewaySystemdServiceName,
|
||||
resolveGatewayWindowsTaskName,
|
||||
} from "./constants.js";
|
||||
import { execSchtasks } from "./schtasks-exec.js";
|
||||
|
||||
export type ExtraGatewayService = {
|
||||
platform: "darwin" | "linux" | "win32";
|
||||
@@ -24,7 +23,6 @@ export type FindExtraGatewayServicesOptions = {
|
||||
};
|
||||
|
||||
const EXTRA_MARKERS = ["openclaw", "clawdbot", "moltbot"] as const;
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export function renderGatewayServiceCleanupHints(
|
||||
env: Record<string, string | undefined> = process.env as Record<string, string | undefined>,
|
||||
@@ -296,35 +294,6 @@ function parseSchtasksList(output: string): ScheduledTaskInfo[] {
|
||||
return tasks;
|
||||
}
|
||||
|
||||
async function execSchtasks(
|
||||
args: string[],
|
||||
): Promise<{ stdout: string; stderr: string; code: number }> {
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync("schtasks", args, {
|
||||
encoding: "utf8",
|
||||
windowsHide: true,
|
||||
});
|
||||
return {
|
||||
stdout: String(stdout ?? ""),
|
||||
stderr: String(stderr ?? ""),
|
||||
code: 0,
|
||||
};
|
||||
} catch (error) {
|
||||
const e = error as {
|
||||
stdout?: unknown;
|
||||
stderr?: unknown;
|
||||
code?: unknown;
|
||||
message?: unknown;
|
||||
};
|
||||
return {
|
||||
stdout: typeof e.stdout === "string" ? e.stdout : "",
|
||||
stderr:
|
||||
typeof e.stderr === "string" ? e.stderr : typeof e.message === "string" ? e.message : "",
|
||||
code: typeof e.code === "number" ? e.code : 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function findExtraGatewayServices(
|
||||
env: Record<string, string | undefined>,
|
||||
opts: FindExtraGatewayServicesOptions = {},
|
||||
|
||||
33
src/daemon/schtasks-exec.ts
Normal file
33
src/daemon/schtasks-exec.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export async function execSchtasks(
|
||||
args: string[],
|
||||
): Promise<{ stdout: string; stderr: string; code: number }> {
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync("schtasks", args, {
|
||||
encoding: "utf8",
|
||||
windowsHide: true,
|
||||
});
|
||||
return {
|
||||
stdout: String(stdout ?? ""),
|
||||
stderr: String(stderr ?? ""),
|
||||
code: 0,
|
||||
};
|
||||
} catch (error) {
|
||||
const e = error as {
|
||||
stdout?: unknown;
|
||||
stderr?: unknown;
|
||||
code?: unknown;
|
||||
message?: unknown;
|
||||
};
|
||||
return {
|
||||
stdout: typeof e.stdout === "string" ? e.stdout : "",
|
||||
stderr:
|
||||
typeof e.stderr === "string" ? e.stderr : typeof e.message === "string" ? e.message : "",
|
||||
code: typeof e.code === "number" ? e.code : 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import type { GatewayServiceRuntime } from "./service-runtime.js";
|
||||
import { colorize, isRich, theme } from "../terminal/theme.js";
|
||||
import { formatGatewayServiceDescription, resolveGatewayWindowsTaskName } from "./constants.js";
|
||||
import { resolveGatewayStateDir } from "./paths.js";
|
||||
import { parseKeyValueOutput } from "./runtime-parse.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
import { execSchtasks } from "./schtasks-exec.js";
|
||||
|
||||
const formatLine = (label: string, value: string) => {
|
||||
const rich = isRich();
|
||||
@@ -197,35 +194,6 @@ function buildTaskScript({
|
||||
return `${lines.join("\r\n")}\r\n`;
|
||||
}
|
||||
|
||||
async function execSchtasks(
|
||||
args: string[],
|
||||
): Promise<{ stdout: string; stderr: string; code: number }> {
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync("schtasks", args, {
|
||||
encoding: "utf8",
|
||||
windowsHide: true,
|
||||
});
|
||||
return {
|
||||
stdout: String(stdout ?? ""),
|
||||
stderr: String(stderr ?? ""),
|
||||
code: 0,
|
||||
};
|
||||
} catch (error) {
|
||||
const e = error as {
|
||||
stdout?: unknown;
|
||||
stderr?: unknown;
|
||||
code?: unknown;
|
||||
message?: unknown;
|
||||
};
|
||||
return {
|
||||
stdout: typeof e.stdout === "string" ? e.stdout : "",
|
||||
stderr:
|
||||
typeof e.stderr === "string" ? e.stderr : typeof e.message === "string" ? e.message : "",
|
||||
code: typeof e.code === "number" ? e.code : 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function assertSchtasksAvailable() {
|
||||
const res = await execSchtasks(["/Query"]);
|
||||
if (res.code === 0) {
|
||||
|
||||
Reference in New Issue
Block a user