Skip to content

Commit

Permalink
run multiple server threads
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Nov 23, 2024
1 parent 9447077 commit 6bf18c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 4 additions & 3 deletions entrypoint.spec.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
25 changes: 23 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cluster from 'cluster'
import { availableParallelism } from 'os'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
Expand Down Expand Up @@ -60,6 +62,25 @@ if (fs.existsSync(legacySecretsPath)) {
)
process.exit(1)
}
export const server = new Server(config)

await server.start()
async function startServer() {
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`)
}
}

0 comments on commit 6bf18c8

Please sign in to comment.