From c6c29b9c88d9102f0ab1bf4766340f5e3d869399 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Sun, 19 Apr 2026 19:21:37 +0200 Subject: [PATCH] feat: add init system --- Dockerfile | 2 ++ src/index.ts | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index ff3d267..e2460f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ ENV NODE_ENV=production \ RUN apt-get update && apt-get install -y --no-install-recommends \ chromium \ ca-certificates \ + dumb-init \ fonts-liberation \ && rm -rf /var/lib/apt/lists/* @@ -18,4 +19,5 @@ COPY . . EXPOSE 3000 +ENTRYPOINT ["/usr/bin/dumb-init", "--"] CMD ["bun", "run", "src/index.ts"] diff --git a/src/index.ts b/src/index.ts index 375bb7c..5284f1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,10 @@ import { getEventStartTime } from "./utils/getEventStartTime"; import { rmSync, existsSync } from "node:fs"; import { join } from "node:path"; -const sessionPath = join(process.cwd(), ".wwebjs_auth", "session"); +const authDataPath = join(process.cwd(), ".wwebjs_auth"); +const sessionPath = join(authDataPath, "session"); + +// remove singleton lock files that might be left from a previous run to prevent auth issues for (const name of ["SingletonLock", "SingletonCookie", "SingletonSocket"]) { const fullPath = join(sessionPath, name); if (existsSync(fullPath)) { @@ -20,7 +23,7 @@ for (const name of ["SingletonLock", "SingletonCookie", "SingletonSocket"]) { const events = new EventEmitter(); const wa = new Client({ - authStrategy: new LocalAuth(), + authStrategy: new LocalAuth({ dataPath: authDataPath }), puppeteer: { args: process.env.NODE_ENV === "production" @@ -29,6 +32,33 @@ const wa = new Client({ }, }); +let isShuttingDown = false; + +const shutdown = async (signal: NodeJS.Signals) => { + if (isShuttingDown) { + return; + } + + isShuttingDown = true; + console.log(`[APP] received ${signal}, shutting down`); + + try { + await wa.destroy(); + } catch (error) { + console.error("[WA] failed to destroy client", error); + } finally { + process.exit(0); + } +}; + +process.once("SIGINT", () => { + void shutdown("SIGINT"); +}); + +process.once("SIGTERM", () => { + void shutdown("SIGTERM"); +}); + wa.once("ready", async () => { console.log("[WA] client ready!"); }); @@ -62,7 +92,10 @@ events.on("eventUpdate", async ({ changes, previousEvent, currentEvent }) => { await wa.sendMessage(process.env.CHAT_ID!, message); }); -wa.initialize(); +wa.initialize().catch((error) => { + console.error("[WA] failed to initialize client", error); + process.exit(1); +}); Bun.serve({ port: 3000,