fix: add gateway stop/restart commands
This commit is contained in:
@@ -307,6 +307,35 @@ export async function uninstallLaunchAgent({
|
||||
}
|
||||
}
|
||||
|
||||
function isLaunchctlNotLoaded(res: {
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
code: number;
|
||||
}): boolean {
|
||||
const detail = `${res.stderr || res.stdout}`.toLowerCase();
|
||||
return (
|
||||
detail.includes("no such process") ||
|
||||
detail.includes("could not find service") ||
|
||||
detail.includes("not found")
|
||||
);
|
||||
}
|
||||
|
||||
export async function stopLaunchAgent({
|
||||
stdout,
|
||||
}: {
|
||||
stdout: NodeJS.WritableStream;
|
||||
}): Promise<void> {
|
||||
const domain = resolveGuiDomain();
|
||||
const label = GATEWAY_LAUNCH_AGENT_LABEL;
|
||||
const res = await execLaunchctl(["bootout", `${domain}/${label}`]);
|
||||
if (res.code !== 0 && !isLaunchctlNotLoaded(res)) {
|
||||
throw new Error(
|
||||
`launchctl bootout failed: ${res.stderr || res.stdout}`.trim(),
|
||||
);
|
||||
}
|
||||
stdout.write(`Stopped LaunchAgent: ${domain}/${label}\n`);
|
||||
}
|
||||
|
||||
export async function installLaunchAgent({
|
||||
env,
|
||||
stdout,
|
||||
|
||||
@@ -233,6 +233,28 @@ export async function uninstallScheduledTask({
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}: {
|
||||
stdout: NodeJS.WritableStream;
|
||||
}): Promise<void> {
|
||||
await assertSchtasksAvailable();
|
||||
const res = await execSchtasks(["/End", "/TN", GATEWAY_WINDOWS_TASK_NAME]);
|
||||
if (res.code !== 0 && !isTaskNotRunning(res)) {
|
||||
throw new Error(`schtasks end failed: ${res.stderr || res.stdout}`.trim());
|
||||
}
|
||||
stdout.write(`Stopped Scheduled Task: ${GATEWAY_WINDOWS_TASK_NAME}\n`);
|
||||
}
|
||||
|
||||
export async function restartScheduledTask({
|
||||
stdout,
|
||||
}: {
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
isLaunchAgentLoaded,
|
||||
readLaunchAgentProgramArguments,
|
||||
restartLaunchAgent,
|
||||
stopLaunchAgent,
|
||||
uninstallLaunchAgent,
|
||||
} from "./launchd.js";
|
||||
import {
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
isScheduledTaskInstalled,
|
||||
readScheduledTaskCommand,
|
||||
restartScheduledTask,
|
||||
stopScheduledTask,
|
||||
uninstallScheduledTask,
|
||||
} from "./schtasks.js";
|
||||
import {
|
||||
@@ -17,6 +19,7 @@ import {
|
||||
isSystemdServiceEnabled,
|
||||
readSystemdServiceExecStart,
|
||||
restartSystemdService,
|
||||
stopSystemdService,
|
||||
uninstallSystemdService,
|
||||
} from "./systemd.js";
|
||||
|
||||
@@ -37,6 +40,7 @@ export type GatewayService = {
|
||||
env: Record<string, string | undefined>;
|
||||
stdout: NodeJS.WritableStream;
|
||||
}) => Promise<void>;
|
||||
stop: (args: { stdout: NodeJS.WritableStream }) => Promise<void>;
|
||||
restart: (args: { stdout: NodeJS.WritableStream }) => Promise<void>;
|
||||
isLoaded: (args: {
|
||||
env: Record<string, string | undefined>;
|
||||
@@ -59,6 +63,9 @@ export function resolveGatewayService(): GatewayService {
|
||||
uninstall: async (args) => {
|
||||
await uninstallLaunchAgent(args);
|
||||
},
|
||||
stop: async (args) => {
|
||||
await stopLaunchAgent(args);
|
||||
},
|
||||
restart: async (args) => {
|
||||
await restartLaunchAgent(args);
|
||||
},
|
||||
@@ -78,6 +85,9 @@ export function resolveGatewayService(): GatewayService {
|
||||
uninstall: async (args) => {
|
||||
await uninstallSystemdService(args);
|
||||
},
|
||||
stop: async (args) => {
|
||||
await stopSystemdService(args);
|
||||
},
|
||||
restart: async (args) => {
|
||||
await restartSystemdService(args);
|
||||
},
|
||||
@@ -97,6 +107,9 @@ export function resolveGatewayService(): GatewayService {
|
||||
uninstall: async (args) => {
|
||||
await uninstallScheduledTask(args);
|
||||
},
|
||||
stop: async (args) => {
|
||||
await stopScheduledTask(args);
|
||||
},
|
||||
restart: async (args) => {
|
||||
await restartScheduledTask(args);
|
||||
},
|
||||
|
||||
@@ -331,6 +331,22 @@ export async function uninstallSystemdService({
|
||||
}
|
||||
}
|
||||
|
||||
export async function stopSystemdService({
|
||||
stdout,
|
||||
}: {
|
||||
stdout: NodeJS.WritableStream;
|
||||
}): Promise<void> {
|
||||
await assertSystemdAvailable();
|
||||
const unitName = `${GATEWAY_SYSTEMD_SERVICE_NAME}.service`;
|
||||
const res = await execSystemctl(["--user", "stop", unitName]);
|
||||
if (res.code !== 0) {
|
||||
throw new Error(
|
||||
`systemctl stop failed: ${res.stderr || res.stdout}`.trim(),
|
||||
);
|
||||
}
|
||||
stdout.write(`Stopped systemd service: ${unitName}\n`);
|
||||
}
|
||||
|
||||
export async function restartSystemdService({
|
||||
stdout,
|
||||
}: {
|
||||
|
||||
Reference in New Issue
Block a user