2018-07-27 17:33:50 -04:00
const ProcessLauncher = require ( '../../core/processes/processLauncher' ) ;
2018-07-20 15:21:23 +03:00
const utils = require ( '../../utils/utils.js' ) ;
const constants = require ( '../../constants' ) ;
2018-05-22 17:34:54 -04:00
2018-05-23 08:40:09 -04:00
class BlockchainProcessLauncher {
2018-05-22 17:34:54 -04:00
constructor ( options ) {
this . events = options . events ;
this . logger = options . logger ;
this . normalizeInput = options . normalizeInput ;
this . blockchainConfig = options . blockchainConfig ;
this . locale = options . locale ;
2018-05-23 11:52:07 -04:00
this . isDev = options . isDev ;
2018-08-02 12:45:59 -04:00
this . client = options . client ;
this . embark = options . embark ;
2018-05-22 17:34:54 -04:00
}
2018-05-23 11:06:58 -04:00
processEnded ( code ) {
2018-10-05 19:16:08 +02:00
this . logger . error ( _ _ ( 'Blockchain process ended before the end of this process. Try running blockchain in a separate process using `$ embark blockchain`. Code: %s' , code ) ) ;
2018-05-23 11:06:58 -04:00
}
2018-05-22 17:34:54 -04:00
startBlockchainNode ( ) {
2018-05-28 11:54:31 -04:00
this . logger . info ( _ _ ( 'Starting Blockchain node in another process' ) . cyan ) ;
2018-05-22 17:34:54 -04:00
this . blockchainProcess = new ProcessLauncher ( {
2018-08-02 12:45:59 -04:00
name : 'blockchain' ,
2018-07-20 17:12:57 +03:00
modulePath : utils . joinPath ( _ _dirname , './blockchainProcess.js' ) ,
2018-05-22 17:34:54 -04:00
logger : this . logger ,
events : this . events ,
2018-08-02 12:45:59 -04:00
embark : this . embark ,
2018-05-25 13:42:18 +10:00
silent : this . logger . logLevel !== 'trace' ,
2018-05-23 11:06:58 -04:00
exitCallback : this . processEnded . bind ( this )
2018-05-22 17:34:54 -04:00
} ) ;
this . blockchainProcess . send ( {
action : constants . blockchain . init , options : {
blockchainConfig : this . blockchainConfig ,
2018-10-06 18:05:37 +02:00
client : this . client ,
2018-05-22 17:34:54 -04:00
env : this . env ,
2018-05-23 11:52:07 -04:00
isDev : this . isDev ,
2018-05-22 17:34:54 -04:00
locale : this . locale
}
} ) ;
this . blockchainProcess . once ( 'result' , constants . blockchain . blockchainReady , ( ) => {
2018-05-28 11:54:31 -04:00
this . logger . info ( _ _ ( 'Blockchain node is ready' ) . cyan ) ;
2018-05-23 08:40:09 -04:00
this . events . emit ( constants . blockchain . blockchainReady ) ;
2018-05-22 17:34:54 -04:00
} ) ;
2018-05-30 10:52:15 -04:00
2018-06-22 22:52:15 +10:00
this . blockchainProcess . once ( 'result' , constants . blockchain . blockchainExit , ( ) => {
2018-08-02 12:45:59 -04:00
// tell everyone that our blockchain process (ie geth) died
2018-06-22 22:52:15 +10:00
this . events . emit ( constants . blockchain . blockchainExit ) ;
// then kill off the blockchain process
this . blockchainProcess . kill ( ) ;
} ) ;
2018-09-16 00:18:53 +05:30
this . events . on ( 'logs:ethereum:enable' , ( ) => {
this . blockchainProcess . silent = false ;
} ) ;
this . events . on ( 'logs:ethereum:disable' , ( ) => {
this . blockchainProcess . silent = true ;
} ) ;
2018-05-30 10:52:15 -04:00
this . events . on ( 'exit' , ( ) => {
this . blockchainProcess . send ( 'exit' ) ;
} ) ;
2018-05-22 17:34:54 -04:00
}
}
2018-05-23 08:40:09 -04:00
module . exports = BlockchainProcessLauncher ;