Skip to content

Commit

Permalink
Add: Workers support
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKoerner committed Mar 10, 2024
1 parent 4e0a783 commit b5ae662
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const config: Config = {
port: Number(process.env.PORT ?? 3000),
host: process.env.HOST ?? '0.0.0.0',
logger: Boolean(Number(process.env.LOGGER) ?? 0),
workers: Number(process.env.WORKERS ?? 1),
png: {
enabled: Boolean(Number(process.env.PNG ?? 1)),
size: {
Expand Down
23 changes: 21 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import cluster from "node:cluster";
import { config } from './config.js';
import { app } from './app.js';

(async () => {
const useCluster = config.workers > 1;

if (cluster.isPrimary && useCluster) {
for (let i = 0; i < config.workers; i++) {
cluster.fork();
}

cluster.on(
"exit",
(worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);

// Fork a new worker
cluster.fork();
},
);
} else {
console.log(`Worker ${process.pid} started`);

const server = await app();

server.listen(
Expand All @@ -18,4 +37,4 @@ import { app } from './app.js';
console.info(`Server listening at http://${config.host}:${config.port}`);
}
);
})();
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Config = {
port: number;
host: string;
logger: boolean;
workers: number,
versions: number[];
png: {
enabled: boolean;
Expand Down

0 comments on commit b5ae662

Please sign in to comment.