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 || os . cpus ( ) . length ;
36+
37+ assert ( this . distPath || this . downloader , "FastBootAppServer must be provided with either a distPath or a downloader option." ) ;
38+ assert ( ! ( this . distPath && this . downloader ) , "FastBootAppServer must be provided with either a distPath or a downloader option, but not both." ) ;
39+ }
2840 }
2941
3042 start ( ) {
43+ if ( cluster . isWorker ) { return ; }
44+
3145 return this . initializeApp ( )
3246 . then ( ( ) => this . subscribeToNotifier ( ) )
3347 . then ( ( ) => this . forkWorkers ( ) )
@@ -44,6 +58,7 @@ class FastBootAppServer {
4458 propagateUI ( ) {
4559 if ( this . downloader ) { this . downloader . ui = this . ui ; }
4660 if ( this . notifier ) { this . notifier . ui = this . ui ; }
61+ if ( this . cache ) { this . cache . ui = this . ui ; }
4762 }
4863
4964 initializeApp ( ) {
@@ -118,10 +133,6 @@ class FastBootAppServer {
118133 }
119134
120135 forkWorker ( ) {
121- cluster . setupMaster ( {
122- exec : WORKER_PATH
123- } ) ;
124-
125136 let env = this . buildWorkerEnv ( ) ;
126137 let worker = cluster . fork ( env ) ;
127138
0 commit comments