-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fyllut implementing Node.js Cluster for improved performance
- Loading branch information
Showing
6 changed files
with
62 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import correlator from 'express-correlation-id'; | ||
import nodeProcess from 'node:process'; | ||
import { createLogger, format, transports } from 'winston'; | ||
|
||
const correlationIdFormat = format((info) => { | ||
const commonFormat = format((info) => { | ||
info.correlation_id = correlator.getId(); | ||
info.node_worker_pid = nodeProcess.pid; | ||
return info; | ||
}); | ||
|
||
export const logger = createLogger({ | ||
level: process.env.FYLLUT_BACKEND_LOGLEVEL || (process.env.NODE_ENV === 'test' ? 'warning' : 'info'), | ||
format: format.combine(correlationIdFormat(), format.json()), | ||
format: format.combine(commonFormat(), format.json()), | ||
transports: [new transports.Console()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import express from 'express'; | ||
import { AggregatorRegistry } from 'prom-client'; | ||
|
||
const metricsServer = express(); | ||
const aggregatorRegistry = new AggregatorRegistry(); | ||
|
||
metricsServer.get('/node-cluster-metrics', async (req, res) => { | ||
try { | ||
const metrics = await aggregatorRegistry.clusterMetrics(); | ||
res.set('Content-Type', aggregatorRegistry.contentType); | ||
res.send(metrics); | ||
} catch (ex: any) { | ||
res.statusCode = 500; | ||
res.send(ex.message); | ||
} | ||
}); | ||
|
||
export default metricsServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
import nodeCluster from 'node:cluster'; | ||
import nodeOs from 'node:os'; | ||
import nodeProcess from 'node:process'; | ||
import { createApp } from './app'; | ||
import { logger } from './logger.js'; | ||
import metricsServer from './metrics-server'; | ||
import './utils/errorToJson.js'; | ||
|
||
const setupNodeCluster = !!process.env.NAIS_CLUSTER_NAME || process.env.ENABLE_CLUSTER === 'true'; | ||
const numCPUs = nodeOs.availableParallelism(); | ||
const port = parseInt(process.env.PORT || '8080'); | ||
const metricsPort = parseInt(process.env.METRICS_PORT || '4001'); | ||
|
||
const app = createApp(); | ||
let viteApp; | ||
|
||
logger.info(`serving on ${port}`); | ||
if (import.meta.env.PROD) { | ||
app.listen(port); | ||
if (setupNodeCluster && nodeCluster.isPrimary) { | ||
logger.info(`Primary ${nodeProcess.pid} is running, will spawn ${numCPUs} workers`); | ||
|
||
// Start server for aggregated metrics | ||
metricsServer.listen(metricsPort); | ||
logger.info(`Metrics server for node cluster listening to ${metricsPort}`); | ||
|
||
// Fork workers | ||
for (let i = 0; i < numCPUs; i++) { | ||
nodeCluster.fork(); | ||
} | ||
|
||
nodeCluster.on('exit', (_worker, code, signal) => { | ||
logger.info(`Worker ${nodeProcess.pid} died`, { signal, code }); | ||
}); | ||
} else { | ||
const app = createApp(); | ||
if (import.meta.env.PROD) { | ||
app.listen(port); | ||
logger.info(`Worker ${process.pid} started, listening to ${port}`); | ||
} else { | ||
logger.info(`serving on ${port}`); | ||
} | ||
viteApp = app; | ||
} | ||
|
||
//Play nice with nais, force node to delay quiting to ensure no traffic is incoming | ||
process.on('SIGTERM', () => setTimeout(() => logger.debug('Har sovet i 30 sekunder'), 30000)); | ||
|
||
export const viteNodeApp = app; | ||
export const viteNodeApp = viteApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters