From b23a28f9c3fcf701b3b4f657b9569bd3760d8c78 Mon Sep 17 00:00:00 2001 From: Bruno Perel Date: Sat, 7 Sep 2024 19:39:54 +0200 Subject: [PATCH] api: Use pm2 --- apps/whattheduck/package.json | 1 - packages/api/Dockerfile | 4 +- packages/api/index.ts | 121 +++++++++++++++++++--------------- 3 files changed, 70 insertions(+), 56 deletions(-) diff --git a/apps/whattheduck/package.json b/apps/whattheduck/package.json index 63a79a6d7..3cf0772ae 100644 --- a/apps/whattheduck/package.json +++ b/apps/whattheduck/package.json @@ -10,7 +10,6 @@ "dev": "concurrently -n vite,vue-tsc \"vite --port 8003 --host\" \"vue-tsc --noEmit --watch\"", "build": "vite build && vue-tsc --noEmit", "build-capgo": "capgo bundle zip", - "i-next": "for dep in $(jq -r '.dependencies | to_entries[] | select(.value == \"next\") | .key' package.json); do pnpm install \"${dep}@latest\"; done", "ionic:build": "npm run build", "preview": "vite preview", "test:e2e": "cypress run", diff --git a/packages/api/Dockerfile b/packages/api/Dockerfile index 7b5781e8c..998a4536d 100644 --- a/packages/api/Dockerfile +++ b/packages/api/Dockerfile @@ -9,5 +9,7 @@ COPY packages/prisma-schemas/client_dm/libquery_engine-debian-openssl-3.0.x.so.n COPY packages/api/.env /app/ COPY packages/api/bundle.mjs /app/ +RUN npm install pm2@latest -g + EXPOSE 3000 -CMD ["node", "bundle.mjs"] \ No newline at end of file +CMD ["pm2", "start", "--no-daemon", "-i", "max", "bundle.mjs"] \ No newline at end of file diff --git a/packages/api/index.ts b/packages/api/index.ts index c9b5c5818..82c04af19 100644 --- a/packages/api/index.ts +++ b/packages/api/index.ts @@ -3,6 +3,8 @@ import { instrument } from "@socket.io/admin-ui"; import dotenv from "dotenv"; import { createServer } from "http"; import { Server } from "socket.io"; +import cluster from "cluster"; +import { cpus } from "os"; import type { SessionUser } from "~dm-types/SessionUser"; @@ -84,63 +86,74 @@ const httpServer = createServer(async (req, res) => { res.end(); }); -httpServer.listen(3000); -console.log("WebSocket open on port 3000"); - -const io = new ServerWithUser(httpServer, { - cors: { - origin: true, - }, -}); - -instrument(io, { - auth: false, -}); +if (cluster.isPrimary) { + for (let i = 0; i < cpus().length; i++) { + cluster.fork(); + } -io.use(OptionalAuthMiddleware); -io.use((_socket, next) => { - process.on("unhandledRejection", (reason: Error) => { - console.error(reason); - next(reason); + cluster.on("exit", (worker) => { + console.log(`Worker ${worker.process.pid} died, starting a new one`); + cluster.fork(); }); - - process.on("uncaughtException", (error: Error) => { - console.error(error); - next(error); +} else { + httpServer.listen(3000); + console.log("WebSocket open on port 3000"); + + const io = new ServerWithUser(httpServer, { + cors: { + origin: true, + }, }); - next(); - // app.all( - // /^\/(edgecreator\/(publish|edgesprites)|notifications)|(edges\/(published))|(\/demo\/reset)|(bookstores\/(approve|refuse))|(presentation-text\/(approve|refuse))/, - // [checkUserIsAdmin] - // ); - - // app.all(/^\/edgecreator\/(.+)/, [ - // authenticateToken, - // checkUserIsEdgeCreatorEditor, - // ]); - - // app.all(/^\/global-stats\/user\/list$/, [ - // authenticateToken, - // checkUserIsEdgeCreatorEditor, - // ]); + instrument(io, { + auth: false, + }); - // app.all(/^\/collection\/(.+)/, authenticateToken); - // app.all("/global-stats/user/collection/rarity", authenticateToken); -}); + io.use(OptionalAuthMiddleware); + io.use((_socket, next) => { + process.on("unhandledRejection", (reason: Error) => { + console.error(reason); + next(reason); + }); + + process.on("uncaughtException", (error: Error) => { + console.error(error); + next(error); + }); + next(); + + // app.all( + // /^\/(edgecreator\/(publish|edgesprites)|notifications)|(edges\/(published))|(\/demo\/reset)|(bookstores\/(approve|refuse))|(presentation-text\/(approve|refuse))/, + // [checkUserIsAdmin] + // ); + + // app.all(/^\/edgecreator\/(.+)/, [ + // authenticateToken, + // checkUserIsEdgeCreatorEditor, + // ]); + + // app.all(/^\/global-stats\/user\/list$/, [ + // authenticateToken, + // checkUserIsEdgeCreatorEditor, + // ]); + + // app.all(/^\/collection\/(.+)/, authenticateToken); + // app.all("/global-stats/user/collection/rarity", authenticateToken); + }); -auth(io); -bookcase(io); -bookstores(io); -coa(io); -collection(io); -coverId(io); -edgecreator(io); -edges(io); -events(io); -feedback(io); -globalStats(io); -login(io); -presentationText(io); -publicCollection(io); -stats(io); + auth(io); + bookcase(io); + bookstores(io); + coa(io); + collection(io); + coverId(io); + edgecreator(io); + edges(io); + events(io); + feedback(io); + globalStats(io); + login(io); + presentationText(io); + publicCollection(io); + stats(io); +}