Files
Moltbot/src/daemon/service.ts
Peter Steinberger c379191f80 chore: migrate to oxlint and oxfmt
Co-authored-by: Christoph Nakazawa <christoph.pojer@gmail.com>
2026-01-14 15:02:19 +00:00

164 lines
4.5 KiB
TypeScript

import {
installLaunchAgent,
isLaunchAgentLoaded,
readLaunchAgentProgramArguments,
readLaunchAgentRuntime,
restartLaunchAgent,
stopLaunchAgent,
uninstallLaunchAgent,
} from "./launchd.js";
import {
installScheduledTask,
isScheduledTaskInstalled,
readScheduledTaskCommand,
readScheduledTaskRuntime,
restartScheduledTask,
stopScheduledTask,
uninstallScheduledTask,
} from "./schtasks.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import {
installSystemdService,
isSystemdServiceEnabled,
readSystemdServiceExecStart,
readSystemdServiceRuntime,
restartSystemdService,
stopSystemdService,
uninstallSystemdService,
} from "./systemd.js";
export type GatewayServiceInstallArgs = {
env: Record<string, string | undefined>;
stdout: NodeJS.WritableStream;
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string | undefined>;
};
export type GatewayService = {
label: string;
loadedText: string;
notLoadedText: string;
install: (args: GatewayServiceInstallArgs) => Promise<void>;
uninstall: (args: {
env: Record<string, string | undefined>;
stdout: NodeJS.WritableStream;
}) => Promise<void>;
stop: (args: {
env?: Record<string, string | undefined>;
profile?: string;
stdout: NodeJS.WritableStream;
}) => Promise<void>;
restart: (args: {
env?: Record<string, string | undefined>;
profile?: string;
stdout: NodeJS.WritableStream;
}) => Promise<void>;
isLoaded: (args: {
env?: Record<string, string | undefined>;
profile?: string;
}) => Promise<boolean>;
readCommand: (env: Record<string, string | undefined>) => Promise<{
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string>;
sourcePath?: string;
} | null>;
readRuntime: (env: Record<string, string | undefined>) => Promise<GatewayServiceRuntime>;
};
export function resolveGatewayService(): GatewayService {
if (process.platform === "darwin") {
return {
label: "LaunchAgent",
loadedText: "loaded",
notLoadedText: "not loaded",
install: async (args) => {
await installLaunchAgent(args);
},
uninstall: async (args) => {
await uninstallLaunchAgent(args);
},
stop: async (args) => {
await stopLaunchAgent({
stdout: args.stdout,
profile: args.profile,
env: args.env,
});
},
restart: async (args) => {
await restartLaunchAgent({
stdout: args.stdout,
profile: args.profile,
env: args.env,
});
},
isLoaded: async (args) => isLaunchAgentLoaded({ profile: args.profile, env: args.env }),
readCommand: readLaunchAgentProgramArguments,
readRuntime: readLaunchAgentRuntime,
};
}
if (process.platform === "linux") {
return {
label: "systemd",
loadedText: "enabled",
notLoadedText: "disabled",
install: async (args) => {
await installSystemdService(args);
},
uninstall: async (args) => {
await uninstallSystemdService(args);
},
stop: async (args) => {
await stopSystemdService({
stdout: args.stdout,
profile: args.profile,
env: args.env,
});
},
restart: async (args) => {
await restartSystemdService({
stdout: args.stdout,
profile: args.profile,
env: args.env,
});
},
isLoaded: async (args) => isSystemdServiceEnabled({ profile: args.profile, env: args.env }),
readCommand: readSystemdServiceExecStart,
readRuntime: async (env) => await readSystemdServiceRuntime(env),
};
}
if (process.platform === "win32") {
return {
label: "Scheduled Task",
loadedText: "registered",
notLoadedText: "missing",
install: async (args) => {
await installScheduledTask(args);
},
uninstall: async (args) => {
await uninstallScheduledTask(args);
},
stop: async (args) => {
await stopScheduledTask({
stdout: args.stdout,
profile: args.profile,
});
},
restart: async (args) => {
await restartScheduledTask({
stdout: args.stdout,
profile: args.profile,
});
},
isLoaded: async (args) => isScheduledTaskInstalled(args.profile),
readCommand: readScheduledTaskCommand,
readRuntime: async (env) => await readScheduledTaskRuntime(env),
};
}
throw new Error(`Gateway service install not supported on ${process.platform}`);
}