@@ -2,37 +2,20 @@ import dotenv from 'dotenv'
22import './lib/feature-flags.js'
33import './lib/check-node-version.js'
44import './lib/handle-exceptions.js'
5- import throng from 'throng'
6- import os from 'os'
75import portUsed from 'port-used'
8- import prefixStreamWrite from './lib/prefix-stream-write.js'
96import createApp from './lib/app.js'
107import warmServer from './lib/warm-server.js'
118import http from 'http'
129dotenv . config ( )
13- // Intentionally require these for both cluster primary and workers
1410
1511const { PORT , NODE_ENV } = process . env
1612const port = Number ( PORT ) || 4000
1713
18- // Start the server!
19- if ( NODE_ENV === 'production' ) {
20- clusteredMain ( )
21- } else {
22- nonClusteredMain ( )
23- }
24-
25- function clusteredMain ( ) {
26- // Spin up a cluster!
27- throng ( {
28- master : setupPrimary ,
29- worker : setupWorker ,
30- count : calculateWorkerCount ( ) ,
31- } )
32- }
14+ async function main ( ) {
15+ if ( NODE_ENV !== 'production' ) {
16+ await checkPortAvailability ( )
17+ }
3318
34- async function nonClusteredMain ( ) {
35- await checkPortAvailability ( )
3619 await startServer ( )
3720}
3821
@@ -69,68 +52,4 @@ async function startServer() {
6952 . on ( 'error' , ( ) => server . close ( ) )
7053}
7154
72- // This function will only be run in the primary process
73- async function setupPrimary ( ) {
74- process . on ( 'beforeExit' , ( ) => {
75- console . log ( 'Shutting down primary...' )
76- console . log ( 'Exiting!' )
77- } )
78-
79- console . log ( 'Starting up primary...' )
80-
81- await checkPortAvailability ( )
82- }
83-
84- // IMPORTANT: This function will be run in a separate worker process!
85- async function setupWorker ( id , disconnect ) {
86- let exited = false
87-
88- // Wrap stdout and stderr to include the worker ID as a static prefix
89- // console.log('hi') => '[worker.1]: hi'
90- const prefix = `[worker.${ id } ]: `
91- prefixStreamWrite ( process . stdout , prefix )
92- prefixStreamWrite ( process . stderr , prefix )
93-
94- process . on ( 'beforeExit' , ( ) => {
95- console . log ( 'Exiting!' )
96- } )
97-
98- process . on ( 'SIGTERM' , shutdown )
99- process . on ( 'SIGINT' , shutdown )
100-
101- console . log ( 'Starting up worker...' )
102-
103- // Load the server in each worker process and share the port via sharding
104- await startServer ( )
105-
106- function shutdown ( ) {
107- if ( exited ) return
108- exited = true
109-
110- console . log ( 'Shutting down worker...' )
111- disconnect ( )
112- }
113- }
114-
115- function calculateWorkerCount ( ) {
116- // Heroku's recommended WEB_CONCURRENCY count based on the WEB_MEMORY config,
117- // or explicitly configured by us
118- const { WEB_CONCURRENCY } = process . env
119-
120- const recommendedCount = parseInt ( WEB_CONCURRENCY , 10 )
121- const cpuCount = os . cpus ( ) . length
122-
123- // Ensure the recommended count is AT LEAST 1 for safety
124- let workerCount = Math . max ( recommendedCount || 1 , 1 )
125-
126- // If WEB_CONCURRENCY value was configured to a valid number...
127- if ( recommendedCount > 0 ) {
128- // Use the smaller value between the recommendation vs. the CPU count
129- workerCount = Math . min ( workerCount , cpuCount )
130- } else if ( NODE_ENV === 'production' ) {
131- // Else if in a deployed environment, default to the CPU count
132- workerCount = cpuCount
133- }
134-
135- return workerCount
136- }
55+ main ( )
0 commit comments