diff --git a/entrypoint.spec.js b/entrypoint.spec.js index 6246d70a91474..4086c9065270f 100644 --- a/entrypoint.spec.js +++ b/entrypoint.spec.js @@ -1,18 +1,19 @@ import { expect } from 'chai' import isSvg from 'is-svg' import got from './core/got-test-client.js' +import startServer from './server.js' -let serverModule +let server before(async function () { this.timeout('30s') // remove args coming from mocha // https://github.com/badges/shields/issues/3365 process.argv = [] - serverModule = await import('./server.js') + server = await startServer() }) after('shut down the server', async function () { - await serverModule.server.stop() + await server.stop() }) it('should render a badge', async function () { diff --git a/server.js b/server.js index 4495db717bc96..73068e012c002 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,5 @@ +import cluster from 'cluster' +import { availableParallelism } from 'os' import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' @@ -25,11 +27,13 @@ Sentry.init({ }, }) -if (+process.argv[2]) { - config.public.bind.port = +process.argv[2] -} -if (process.argv[3]) { - config.public.bind.address = process.argv[3] +if (process.env.NODE_CONFIG_ENV !== 'test') { + if (+process.argv[2]) { + config.public.bind.port = +process.argv[2] + } + if (process.argv[3]) { + config.public.bind.address = process.argv[3] + } } console.log('Configuration:') @@ -60,6 +64,26 @@ if (fs.existsSync(legacySecretsPath)) { ) process.exit(1) } -export const server = new Server(config) -await server.start() +async function startServer() { + console.log(config) + const server = new Server(config) + await server.start() + return server +} + +export default startServer + +const numCPUs = availableParallelism() +if (process.env.NODE_CONFIG_ENV !== 'test') { + if (cluster.isPrimary) { + console.log(`Primary ${process.pid} is running`) + + for (let i = 0; i < numCPUs; i++) { + cluster.fork() + } + } else { + await startServer() + console.log(`Worker ${process.pid} started`) + } +}