33const assert = require ( 'assert' ) ;
44const cluster = require ( 'cluster' ) ;
55const os = require ( 'os' ) ;
6- const path = require ( 'path' ) ;
76
8- const WORKER_PATH = path . join ( __dirname , './worker.js ' ) ;
7+ const Worker = require ( './worker' ) ;
98
109class FastBootAppServer {
1110 constructor ( options ) {
1211 options = options || { } ;
1312
14- this . downloader = options . downloader ;
1513 this . distPath = options . distPath ;
14+ this . downloader = options . downloader ;
1615 this . notifier = options . notifier ;
17- this . workerCount = options . workerCount || os . cpus ( ) . length ;
16+ this . cache = options . cache ;
1817 this . ui = options . ui ;
1918
2019 if ( ! this . ui ) {
2120 let UI = require ( './ui' ) ;
2221 this . ui = new UI ( ) ;
23- this . propagateUI ( ) ;
2422 }
2523
26- assert ( this . distPath || this . downloader , "FastBootAppServer must be provided with either a distPath or a downloader option." ) ;
27- assert ( ! ( this . distPath && this . downloader ) , "FastBootAppServer must be provided with either a distPath or a downloader option, but not both." ) ;
24+ this . propagateUI ( ) ;
25+
26+ if ( cluster . isWorker ) {
27+ this . worker = new Worker ( {
28+ ui : this . ui ,
29+ distPath : this . distPath || process . env . FASTBOOT_DIST_PATH ,
30+ cache : this . cache
31+ } ) ;
32+
33+ this . worker . start ( ) ;
34+ } else {
35+ this . workerCount = options . workerCount ||
36+ ( process . env . NODE_ENV === 'test' ? 1 : null ) ||
37+ os . cpus ( ) . length ;
38+
39+ assert ( this . distPath || this . downloader , "FastBootAppServer must be provided with either a distPath or a downloader option." ) ;
40+ assert ( ! ( this . distPath && this . downloader ) , "FastBootAppServer must be provided with either a distPath or a downloader option, but not both." ) ;
41+ }
2842 }
2943
3044 start ( ) {
45+ if ( cluster . isWorker ) { return ; }
46+
3147 return this . initializeApp ( )
3248 . then ( ( ) => this . subscribeToNotifier ( ) )
3349 . then ( ( ) => this . forkWorkers ( ) )
@@ -44,6 +60,7 @@ class FastBootAppServer {
4460 propagateUI ( ) {
4561 if ( this . downloader ) { this . downloader . ui = this . ui ; }
4662 if ( this . notifier ) { this . notifier . ui = this . ui ; }
63+ if ( this . cache ) { this . cache . ui = this . ui ; }
4764 }
4865
4966 initializeApp ( ) {
@@ -118,10 +135,6 @@ class FastBootAppServer {
118135 }
119136
120137 forkWorker ( ) {
121- cluster . setupMaster ( {
122- exec : WORKER_PATH
123- } ) ;
124-
125138 let env = this . buildWorkerEnv ( ) ;
126139 let worker = cluster . fork ( env ) ;
127140
0 commit comments