@@ -2,37 +2,20 @@ import dotenv from 'dotenv'
2
2
import './lib/feature-flags.js'
3
3
import './lib/check-node-version.js'
4
4
import './lib/handle-exceptions.js'
5
- import throng from 'throng'
6
- import os from 'os'
7
5
import portUsed from 'port-used'
8
- import prefixStreamWrite from './lib/prefix-stream-write.js'
9
6
import createApp from './lib/app.js'
10
7
import warmServer from './lib/warm-server.js'
11
8
import http from 'http'
12
9
dotenv . config ( )
13
- // Intentionally require these for both cluster primary and workers
14
10
15
11
const { PORT , NODE_ENV } = process . env
16
12
const port = Number ( PORT ) || 4000
17
13
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
+ }
33
18
34
- async function nonClusteredMain ( ) {
35
- await checkPortAvailability ( )
36
19
await startServer ( )
37
20
}
38
21
@@ -69,68 +52,4 @@ async function startServer() {
69
52
. on ( 'error' , ( ) => server . close ( ) )
70
53
}
71
54
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