feat: add init system

This commit is contained in:
2026-04-19 19:21:37 +02:00
parent fc3e68bae9
commit c6c29b9c88
2 changed files with 38 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ ENV NODE_ENV=production \
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
chromium \ chromium \
ca-certificates \ ca-certificates \
dumb-init \
fonts-liberation \ fonts-liberation \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
@@ -18,4 +19,5 @@ COPY . .
EXPOSE 3000 EXPOSE 3000
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["bun", "run", "src/index.ts"] CMD ["bun", "run", "src/index.ts"]

View File

@@ -9,7 +9,10 @@ import { getEventStartTime } from "./utils/getEventStartTime";
import { rmSync, existsSync } from "node:fs"; import { rmSync, existsSync } from "node:fs";
import { join } from "node:path"; 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"]) { for (const name of ["SingletonLock", "SingletonCookie", "SingletonSocket"]) {
const fullPath = join(sessionPath, name); const fullPath = join(sessionPath, name);
if (existsSync(fullPath)) { if (existsSync(fullPath)) {
@@ -20,7 +23,7 @@ for (const name of ["SingletonLock", "SingletonCookie", "SingletonSocket"]) {
const events = new EventEmitter(); const events = new EventEmitter();
const wa = new Client({ const wa = new Client({
authStrategy: new LocalAuth(), authStrategy: new LocalAuth({ dataPath: authDataPath }),
puppeteer: { puppeteer: {
args: args:
process.env.NODE_ENV === "production" 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 () => { wa.once("ready", async () => {
console.log("[WA] client ready!"); console.log("[WA] client ready!");
}); });
@@ -62,7 +92,10 @@ events.on("eventUpdate", async ({ changes, previousEvent, currentEvent }) => {
await wa.sendMessage(process.env.CHAT_ID!, message); 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({ Bun.serve({
port: 3000, port: 3000,