From 902916a944b17256b60bb76b0983caf5a42e852c Mon Sep 17 00:00:00 2001 From: Erin of Yukis Date: Thu, 18 Dec 2025 02:37:17 +0100 Subject: [PATCH] Add systemd-compatible service manager notification after bewCloud has successfully started --- fresh.config.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/fresh.config.ts b/fresh.config.ts index 8f0c2b0..96b0a3b 100644 --- a/fresh.config.ts +++ b/fresh.config.ts @@ -1,3 +1,4 @@ +import { tmpdir } from 'node:os'; import { defineConfig } from 'fresh/server.ts'; import tailwind from 'fresh/plugins/tailwind.ts'; @@ -9,6 +10,54 @@ if (!isBuildMode) { await startCrons(); } +async function notifyServiceManagerReady(message) { + const socketAddr = Deno.env.get("NOTIFY_SOCKET"); + if(typeof(socketAddr) !== "string") { + return; // Service manager doesn’t expect any messages + } + + // Alternative using systemd CLI (systemd specific, requires CLI utils in $PATH) + /*await (new Deno.Command("systemd-notify", { + args: ["--ready", `MESSAGE=${message.replace("\n", " ")}`] + })).spawn();*/ + + // Map socket path syntax + if(socketAddr[0] === "@") { + socketAddr = `\0${socketAddr.slice(1)}`; + } else if(socketAddr[0] !== "/") { + return; // Invalid path + } + + // Send message to service manager (requires Deno `--unstable-net`) + // + // Passing a valid `path` here is required, even though it is not used (Deno bug/limitation). + let senderAddr = `${tmpdir()}/bewcloud-notify-${crypto.randomUUID()}.sock`; + let connection = Deno.listenDatagram({ transport: "unixpacket", path: senderAddr }); + await Deno.remove(senderAddr); + await connection.send( + new TextEncoder().encode(`READY=1\nSTATUS=${message.replace("\n", " ")}`), + { transport: "unixpacket", path: socketAddr } + ); + await connection.close(); +} + export default defineConfig({ plugins: [tailwind()], + server: { + onListen: (params) => { + // Format listening address + const fmtHostPort = (hostname, port) => + hostname.includes(":") ? `[${hostname}]:${port}` : `${hostname}:${port}`; + + const addr = ( + params.transport === "unix" ? `http+unix://${encodeURIComponent(params.path)}` : + params.transport === "vsock" ? `http+vsock://${params.cid}:${parms.port}` : + params.transport === "tcp" ? `http://${fmtHostPort(params.hostname, params.port)}/` : null + ); + + const message = "bewCloud listening for requests" + (addr ? ` on ${addr}` : ""); + console.info(message); + notifyServiceManagerReady(message); + }, + }, });