mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-06-06 21:39:55 +00:00
feat: setup clustering (#530)
This commit is contained in:
parent
5928a1dd25
commit
ae6ed3cdaa
@ -1,8 +1,29 @@
|
||||
import express, { type Response } from 'express'
|
||||
import jsonEndpoints from './V2/endpoints/jsonEndpoints'
|
||||
import graphql from './V2/graphql'
|
||||
import cluster from 'node:cluster'
|
||||
import { availableParallelism } from "node:os"
|
||||
import { Errors, sendError } from './libs/Errors'
|
||||
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
|
||||
const VERSION = 2
|
||||
@ -94,4 +115,4 @@ server.use((err: Error, _1: unknown, res: Response, _2: unknown) => {
|
||||
|
||||
// Start server
|
||||
server.listen(3000)
|
||||
console.log('🚀 Server ready at localhost:3000');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user