mirror of
https://github.com/bewcloud/bewcloud.git
synced 2026-03-11 08:54:49 +00:00
Implement different approach for systemd-notification reporting
I went with a slightly different option, given I was struggling to lose a lot of flexibility in the original listening log (because it's started with some server state parameters) or allow so much unnecessary/duplicate complexity from Fresh in `fresh.config.ts` because I'll probably eventually ditch it (given #99). Some relevant references in the original `fresh` code: -ab14d1044c/docs/1.x/concepts/server-configuration.md (L207-L208)-d9764e2005/src/server/config.ts (L95)-d9764e2005/src/server/mod.ts (L115-L118)-d9764e2005/src/server/boot.ts (L52-L57)
This commit is contained in:
parent
addaf3af62
commit
fbee0f3774
1 changed files with 10 additions and 47 deletions
|
|
@ -5,64 +5,27 @@ import { startCrons } from '/crons/index.ts';
|
|||
|
||||
const isBuildMode = Deno.args.includes('build');
|
||||
|
||||
if (!isBuildMode) {
|
||||
await startCrons();
|
||||
}
|
||||
|
||||
async function notifyServiceManagerReady(message) {
|
||||
const socketAddress = Deno.env.get("NOTIFY_SOCKET");
|
||||
if (typeof(socketAddress) !== "string") {
|
||||
return; // Service manager doesn’t expect any messages
|
||||
async function notifyServiceManagerReady() {
|
||||
const socketAddress = Deno.env.get('NOTIFY_SOCKET');
|
||||
if (typeof socketAddress !== 'string') {
|
||||
return; // Service manager doesn’t expect any messages
|
||||
}
|
||||
|
||||
// Send message using `systemd-notify` util until the native Deno APIs become stable
|
||||
const result = await (new Deno.Command("systemd-notify", {
|
||||
args: ["--ready", `MESSAGE=${message.replace("\n", " ")}`]
|
||||
const result = await (new Deno.Command('systemd-notify', {
|
||||
args: ['--ready', `MESSAGE=bewCloud is ready`],
|
||||
})).output();
|
||||
if (!result.success) {
|
||||
const output = new TextDecoder().decode(result.stderr);
|
||||
throw new Deno.errors.NotCapable(`Failed to execute “systemd-notify”: ${output} (code ${result.code})`);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Map socket path syntax
|
||||
if (socketAddress[0] === "@") {
|
||||
socketAddress = `\0${socketAddress.slice(1)}`;
|
||||
} else if (socketAddress[0] !== "/") {
|
||||
throw `Invalid NOTIFY_SOCKET address format: ${socketAddress}`;
|
||||
}
|
||||
|
||||
// Send message to service manager
|
||||
//
|
||||
// Blockers:
|
||||
// * https://github.com/denoland/deno/pull/31681
|
||||
// * Deno stabilization of `Deno.listenDatagram` (`--unstable-net`)
|
||||
const connection = Deno.listenDatagram({ transport: "unixpacket" });
|
||||
await connection.send(
|
||||
new TextEncoder().encode(`READY=1\nSTATUS=${message.replace("\n", " ")}`),
|
||||
{ transport: "unixpacket", path: socketAddress }
|
||||
);
|
||||
await connection.close();
|
||||
*/
|
||||
if (!isBuildMode) {
|
||||
await startCrons();
|
||||
await notifyServiceManagerReady();
|
||||
}
|
||||
|
||||
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);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue