|
1 | 1 | require('dotenv').config() |
2 | 2 |
|
| 3 | +const throng = require('throng') |
| 4 | +const os = require('os') |
| 5 | +const portUsed = require('port-used') |
| 6 | +const prefixStreamWrite = require('./lib/prefix-stream-write') |
| 7 | + |
| 8 | +// Intentionally require these for both cluster primary and workers |
3 | 9 | require('./lib/check-node-version') |
4 | 10 | require('./lib/handle-exceptions') |
5 | 11 | require('./lib/feature-flags') |
6 | 12 |
|
7 | | -const express = require('express') |
8 | | -const portUsed = require('port-used') |
9 | | -const warmServer = require('./lib/warm-server') |
10 | | -const port = Number(process.env.PORT) || 4000 |
11 | | -const app = express() |
12 | | - |
13 | | -require('./middleware')(app) |
14 | | - |
15 | | -// prevent the app from starting up during tests |
16 | | -/* istanbul ignore next */ |
17 | | -if (!module.parent) { |
18 | | - // check that the development server is not already running |
19 | | - portUsed.check(port).then(async status => { |
20 | | - if (status === false) { |
21 | | - // If in a deployed environment, warm the server at the start |
22 | | - if (process.env.NODE_ENV === 'production') { |
23 | | - // If in a true production environment, wait for the cache to be fully warmed. |
24 | | - if (process.env.HEROKU_PRODUCTION_APP || process.env.GITHUB_ACTIONS) { |
25 | | - await warmServer() |
26 | | - } |
27 | | - } |
28 | | - |
29 | | - // workaround for https://github.com/expressjs/express/issues/1101 |
30 | | - const server = require('http').createServer(app) |
31 | | - server.listen(port, () => console.log(`app running on http://localhost:${port}`)) |
32 | | - .on('error', () => server.close()) |
33 | | - } else { |
34 | | - console.log(`\n\n\nPort ${port} is not available. You may already have a server running.`) |
35 | | - console.log('Try running `killall node` to shut down all your running node processes.\n\n\n') |
36 | | - console.log('\x07') // system 'beep' sound |
37 | | - process.exit(1) |
38 | | - } |
| 13 | +const { PORT, NODE_ENV } = process.env |
| 14 | +const port = Number(PORT) || 4000 |
| 15 | + |
| 16 | +function main () { |
| 17 | + // Spin up a cluster! |
| 18 | + throng({ |
| 19 | + master: setupPrimary, |
| 20 | + worker: setupWorker, |
| 21 | + count: calculateWorkerCount() |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +// Start the server! |
| 26 | +main() |
| 27 | + |
| 28 | +// This function will only be run in the primary process |
| 29 | +async function setupPrimary () { |
| 30 | + process.on('beforeExit', () => { |
| 31 | + console.log('Shutting down primary...') |
| 32 | + console.log('Exiting!') |
| 33 | + }) |
| 34 | + |
| 35 | + console.log('Starting up primary...') |
| 36 | + |
| 37 | + // Check that the development server is not already running |
| 38 | + const portInUse = await portUsed.check(port) |
| 39 | + if (portInUse) { |
| 40 | + console.log(`\n\n\nPort ${port} is not available. You may already have a server running.`) |
| 41 | + console.log('Try running `killall node` to shut down all your running node processes.\n\n\n') |
| 42 | + console.log('\x07') // system 'beep' sound |
| 43 | + process.exit(1) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// IMPORTANT: This function will be run in a separate worker process! |
| 48 | +async function setupWorker (id, disconnect) { |
| 49 | + let exited = false |
| 50 | + |
| 51 | + // Wrap stdout and stderr to include the worker ID as a static prefix |
| 52 | + // console.log('hi') => '[worker.1]: hi' |
| 53 | + const prefix = `[worker.${id}]: ` |
| 54 | + prefixStreamWrite(process.stdout, prefix) |
| 55 | + prefixStreamWrite(process.stderr, prefix) |
| 56 | + |
| 57 | + process.on('beforeExit', () => { |
| 58 | + console.log('Exiting!') |
39 | 59 | }) |
| 60 | + |
| 61 | + process.on('SIGTERM', shutdown) |
| 62 | + process.on('SIGINT', shutdown) |
| 63 | + |
| 64 | + console.log('Starting up worker...') |
| 65 | + |
| 66 | + // Load the server in each worker process and share the port via sharding |
| 67 | + const app = require('./lib/app') |
| 68 | + const warmServer = require('./lib/warm-server') |
| 69 | + |
| 70 | + // If in a deployed environment... |
| 71 | + if (NODE_ENV === 'production') { |
| 72 | + // If in a true production environment, wait for the cache to be fully warmed. |
| 73 | + if (process.env.HEROKU_PRODUCTION_APP || process.env.GITHUB_ACTIONS) { |
| 74 | + await warmServer() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Workaround for https://github.com/expressjs/express/issues/1101 |
| 79 | + const server = require('http').createServer(app) |
| 80 | + server |
| 81 | + .listen(port, () => console.log(`app running on http://localhost:${port}`)) |
| 82 | + .on('error', () => server.close()) |
| 83 | + |
| 84 | + function shutdown () { |
| 85 | + if (exited) return |
| 86 | + exited = true |
| 87 | + |
| 88 | + console.log('Shutting down worker...') |
| 89 | + disconnect() |
| 90 | + } |
40 | 91 | } |
41 | 92 |
|
42 | | -module.exports = app |
| 93 | +function calculateWorkerCount () { |
| 94 | + // Heroku's recommended WEB_CONCURRENCY count based on the WEB_MEMORY config |
| 95 | + const { WEB_CONCURRENCY } = process.env |
| 96 | + |
| 97 | + const recommendedCount = parseInt(WEB_CONCURRENCY, 10) || 1 |
| 98 | + const cpuCount = os.cpus().length |
| 99 | + |
| 100 | + // Ensure the recommended count is AT LEAST 1 for safety |
| 101 | + let workerCount = Math.max(recommendedCount, 1) |
| 102 | + |
| 103 | + // Let's do some math... |
| 104 | + // If in a deployed environment... |
| 105 | + if (NODE_ENV === 'production') { |
| 106 | + // If WEB_MEMORY or WEB_CONCURRENCY values were configured in Heroku, use |
| 107 | + // the smaller value between their recommendation vs. the CPU count |
| 108 | + if (WEB_CONCURRENCY) { |
| 109 | + workerCount = Math.min(recommendedCount, cpuCount) |
| 110 | + } else { |
| 111 | + workerCount = cpuCount |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return workerCount |
| 116 | +} |
0 commit comments