Attemp to clusterize client
This commit is contained in:
+34
-14
@@ -1,4 +1,6 @@
|
||||
import * as dotenv from 'dotenv'
|
||||
import cluster from 'cluster'
|
||||
import os from 'os'
|
||||
dotenv.config()
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
@@ -7,25 +9,43 @@ import { dbReady } from './db/db'
|
||||
import processAll from './process/process'
|
||||
import listenKills from './process/onKill'
|
||||
|
||||
const app = express()
|
||||
const cCPUs = os.cpus().length;
|
||||
|
||||
const port = 3000
|
||||
|
||||
app.use(express.json())
|
||||
if (cluster.isPrimary) {
|
||||
// Create a worker for each CPU
|
||||
for (let i = 0; i < cCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
cluster.on('online', function (worker) {
|
||||
console.log('Worker ' + worker.process.pid + ' is online.');
|
||||
});
|
||||
cluster.on('exit', function (worker, code, signal) {
|
||||
console.log('worker ' + worker.process.pid + ' died.');
|
||||
});
|
||||
}
|
||||
|
||||
app.use(cors())
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Tone client API online')
|
||||
})
|
||||
|
||||
app.use('/', client)
|
||||
|
||||
export default
|
||||
new Promise(async (resolve, reject) => {
|
||||
await dbReady()
|
||||
await processAll()
|
||||
await listenKills()
|
||||
const listenServer = app.listen(port, '0.0.0.0', () => {
|
||||
console.log(`Tone client api listening on port ${port}`)
|
||||
resolve(listenServer)
|
||||
})
|
||||
if (!cluster.isPrimary) {
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
app.use(cors())
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Tone client API online')
|
||||
})
|
||||
|
||||
app.use('/', client)
|
||||
|
||||
await dbReady()
|
||||
await processAll()
|
||||
await listenKills()
|
||||
const listenServer = app.listen(port, '0.0.0.0', () => {
|
||||
console.log(`Tone client api listening on port ${port}`)
|
||||
resolve(listenServer)
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user