@@ -7,6 +7,9 @@ var bitcore = require('bitcore');
77var _ = bitcore . deps . _ ;
88var $ = bitcore . util . preconditions ;
99var log = index . log ;
10+ var child_process = require ( 'child_process' ) ;
11+ var fs = require ( 'fs' ) ;
12+
1013log . debug = function ( ) { } ;
1114
1215/**
@@ -111,10 +114,10 @@ function registerSyncHandlers(node, delay) {
111114/**
112115 * Will register event handlers to stop the node for `process` events
113116 * `uncaughtException` and `SIGINT`.
114- * @param {Object } proc - The Node.js process
117+ * @param {Object } _process - The Node.js process
115118 * @param {Node } node
116119 */
117- function registerExitHandlers ( proc , node ) {
120+ function registerExitHandlers ( _process , node ) {
118121
119122 function exitHandler ( options , err ) {
120123 if ( err ) {
@@ -126,27 +129,27 @@ function registerExitHandlers(proc, node) {
126129 if ( err ) {
127130 log . error ( 'Failed to stop services: ' + err ) ;
128131 }
129- proc . exit ( - 1 ) ;
132+ _process . exit ( - 1 ) ;
130133 } ) ;
131134 }
132135 if ( options . sigint ) {
133136 node . stop ( function ( err ) {
134137 if ( err ) {
135138 log . error ( 'Failed to stop services: ' + err ) ;
136- return proc . exit ( 1 ) ;
139+ return _process . exit ( 1 ) ;
137140 }
138141
139142 log . info ( 'Halted' ) ;
140- proc . exit ( 0 ) ;
143+ _process . exit ( 0 ) ;
141144 } ) ;
142145 }
143146 }
144147
145148 //catches uncaught exceptions
146- proc . on ( 'uncaughtException' , exitHandler . bind ( null , { exit :true } ) ) ;
149+ _process . on ( 'uncaughtException' , exitHandler . bind ( null , { exit :true } ) ) ;
147150
148151 //catches ctrl+c event
149- proc . on ( 'SIGINT' , exitHandler . bind ( null , { sigint :true } ) ) ;
152+ _process . on ( 'SIGINT' , exitHandler . bind ( null , { sigint :true } ) ) ;
150153}
151154
152155/**
@@ -164,16 +167,20 @@ function registerExitHandlers(proc, node) {
164167function start ( options ) {
165168
166169 var fullConfig = _ . clone ( options . config ) ;
167- fullConfig . services = setupServices ( require , options . path , options . config ) ;
170+ fullConfig . services = start . setupServices ( require , options . path , options . config ) ;
168171 fullConfig . datadir = path . resolve ( options . path , options . config . datadir ) ;
169172
173+ if ( fullConfig . daemon ) {
174+ start . spawnChildProcess ( fullConfig . datadir , process ) ;
175+ }
176+
170177 var node = new BitcoreNode ( fullConfig ) ;
171178
172179 // set up the event handlers for logging sync information
173- registerSyncHandlers ( node ) ;
180+ start . registerSyncHandlers ( node ) ;
174181
175182 // setup handlers for uncaught exceptions and ctrl+c
176- registerExitHandlers ( process , node ) ;
183+ start . registerExitHandlers ( process , node ) ;
177184
178185 node . on ( 'ready' , function ( ) {
179186 log . info ( 'Bitcore Node ready' ) ;
@@ -187,7 +194,44 @@ function start(options) {
187194
188195}
189196
197+ /**
198+ * This function will fork the passed in process and exit the parent process
199+ * in order to daemonize the process. If there is already a daemon for this pid (process),
200+ * then the function just returns. Stdout and stderr both append to one file, 'bitcore-node.log'
201+ * located in the datadir.
202+ * @param {String } datadir - The data directory where the bitcoin blockchain and config live.
203+ * @param {Object } _process - The process that needs to fork a child and then, itself, exit.
204+ */
205+ function spawnChildProcess ( datadir , _process ) {
206+
207+ if ( _process . env . __bitcore_node ) {
208+ return _process . pid ;
209+ }
210+
211+ var args = [ ] . concat ( _process . argv ) ;
212+ args . shift ( ) ;
213+ var script = args . shift ( ) ;
214+ var env = _process . env ;
215+ var cwd = _process . cwd ( ) ;
216+ env . __bitcore_node = true ;
217+
218+ var stderr = fs . openSync ( datadir + '/bitcore-node.log' , 'a+' ) ;
219+ var stdout = stderr ;
220+
221+ var cp_opt = {
222+ stdio : [ 'ignore' , stdout , stderr ] ,
223+ env : env ,
224+ cwd : cwd ,
225+ detached : true
226+ } ;
227+
228+ var child = child_process . spawn ( _process . execPath , [ script ] . concat ( args ) , cp_opt ) ;
229+ child . unref ( ) ;
230+ return _process . exit ( ) ;
231+ }
232+
190233module . exports = start ;
191234module . exports . registerExitHandlers = registerExitHandlers ;
192235module . exports . registerSyncHandlers = registerSyncHandlers ;
193236module . exports . setupServices = setupServices ;
237+ module . exports . spawnChildProcess = spawnChildProcess ;
0 commit comments