1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-22 10:52:10 +00:00

feat: setup clustering (#530)

This commit is contained in:
Florian Bouillon 2024-08-30 12:10:05 +02:00 committed by GitHub
parent 5928a1dd25
commit ae6ed3cdaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,29 @@
import express, { type Response } from 'express' import express, { type Response } from 'express'
import jsonEndpoints from './V2/endpoints/jsonEndpoints' import cluster from 'node:cluster'
import graphql from './V2/graphql' import { availableParallelism } from "node:os"
import { Errors, sendError } from './libs/Errors' import { Errors, sendError } from './libs/Errors'
import status from './status' import status from './status'
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
import graphql from './V2/graphql'
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
const parallelism = availableParallelism()
console.log(`creating ${parallelism} workers...`)
for (let i = 0; i < parallelism; i++) {
cluster.fork();
}
cluster.on('online', (worker) => {
console.log('Worker', worker.id, 'online')
})
cluster.on("exit", (worker, code, _signal) => {
console.log(`Worker ${worker.id} exited with code ${code}`);
})
console.log('🚀 Server ready at localhost:3000');
} else {
// Current API version // Current API version
const VERSION = 2 const VERSION = 2
@ -94,4 +115,4 @@ server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
// Start server // Start server
server.listen(3000) server.listen(3000)
console.log('🚀 Server ready at localhost:3000'); }