@@ -13,7 +13,14 @@ require('./lib/feature-flags')
13
13
const { PORT , NODE_ENV } = process . env
14
14
const port = Number ( PORT ) || 4000
15
15
16
- function main ( ) {
16
+ // Start the server!
17
+ if ( NODE_ENV === 'production' ) {
18
+ clusteredMain ( )
19
+ } else {
20
+ nonClusteredMain ( )
21
+ }
22
+
23
+ function clusteredMain ( ) {
17
24
// Spin up a cluster!
18
25
throng ( {
19
26
master : setupPrimary ,
@@ -22,8 +29,40 @@ function main () {
22
29
} )
23
30
}
24
31
25
- // Start the server!
26
- main ( )
32
+ async function nonClusteredMain ( ) {
33
+ await checkPortAvailability ( )
34
+ await startServer ( )
35
+ }
36
+
37
+ async function checkPortAvailability ( ) {
38
+ // Check that the development server is not already running
39
+ const portInUse = await portUsed . check ( port )
40
+ if ( portInUse ) {
41
+ console . log ( `\n\n\nPort ${ port } is not available. You may already have a server running.` )
42
+ console . log ( 'Try running `killall node` to shut down all your running node processes.\n\n\n' )
43
+ console . log ( '\x07' ) // system 'beep' sound
44
+ process . exit ( 1 )
45
+ }
46
+ }
47
+
48
+ async function startServer ( ) {
49
+ const app = require ( './lib/app' )
50
+ const warmServer = require ( './lib/warm-server' )
51
+
52
+ // If in a deployed environment...
53
+ if ( NODE_ENV === 'production' ) {
54
+ // If in a true production environment, wait for the cache to be fully warmed.
55
+ if ( process . env . HEROKU_PRODUCTION_APP || process . env . GITHUB_ACTIONS ) {
56
+ await warmServer ( )
57
+ }
58
+ }
59
+
60
+ // Workaround for https://github.com/expressjs/express/issues/1101
61
+ const server = require ( 'http' ) . createServer ( app )
62
+ server
63
+ . listen ( port , ( ) => console . log ( `app running on http://localhost:${ port } ` ) )
64
+ . on ( 'error' , ( ) => server . close ( ) )
65
+ }
27
66
28
67
// This function will only be run in the primary process
29
68
async function setupPrimary ( ) {
@@ -34,14 +73,7 @@ async function setupPrimary () {
34
73
35
74
console . log ( 'Starting up primary...' )
36
75
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
- }
76
+ await checkPortAvailability ( )
45
77
}
46
78
47
79
// IMPORTANT: This function will be run in a separate worker process!
@@ -64,22 +96,7 @@ async function setupWorker (id, disconnect) {
64
96
console . log ( 'Starting up worker...' )
65
97
66
98
// 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 ( ) )
99
+ await startServer ( )
83
100
84
101
function shutdown ( ) {
85
102
if ( exited ) return
0 commit comments