Skip to content

Commit d405edc

Browse files
author
Braydon Fuller
committed
Merge pull request #174 from kleetus/daemon
Daemonize
2 parents 9aec734 + 94c3451 commit d405edc

3 files changed

Lines changed: 158 additions & 10 deletions

File tree

cli/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function main() {
4646
.command('start')
4747
.description('Start the current node')
4848
.option('-c, --config <dir>', 'Specify the directory with Bitcore Node configuration')
49+
.option('-d, --daemon', 'Make bitcore-node a daemon (running in the background)')
4950
.action(function(cmd){
5051
if (cmd.config) {
5152
cmd.config = path.resolve(process.cwd(), cmd.config);
@@ -54,6 +55,9 @@ function main() {
5455
if (!configInfo) {
5556
configInfo = defaultConfig();
5657
}
58+
if(cmd.daemon) {
59+
configInfo.config.daemon = true;
60+
}
5761
start(configInfo);
5862
});
5963

lib/scaffold/start.js

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var bitcore = require('bitcore');
77
var _ = bitcore.deps._;
88
var $ = bitcore.util.preconditions;
99
var log = index.log;
10+
var child_process = require('child_process');
11+
var fs = require('fs');
12+
1013
log.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) {
164167
function 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+
190233
module.exports = start;
191234
module.exports.registerExitHandlers = registerExitHandlers;
192235
module.exports.registerSyncHandlers = registerSyncHandlers;
193236
module.exports.setupServices = setupServices;
237+
module.exports.spawnChildProcess = spawnChildProcess;

test/scaffold/start.unit.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,104 @@ describe('#start', function() {
163163
});
164164
});
165165
});
166+
describe('#spawnChildProcess', function() {
167+
168+
it('should build the appropriate arguments to spawn a child process', function() {
169+
var child = {
170+
unref: function() {}
171+
};
172+
var _process = {
173+
exit: function() {},
174+
env: {
175+
__bitcore_node: false
176+
},
177+
argv: [
178+
'node',
179+
'bitcore-node'
180+
],
181+
cwd: function(){return ''},
182+
pid: 999,
183+
execPath: '/tmp'
184+
};
185+
var fd = {};
186+
var spawn = sinon.stub().returns(child);
187+
var openSync = sinon.stub().returns(fd);
188+
var spawnChildProcess = proxyquire('../../lib/scaffold/start', {
189+
fs: {
190+
openSync: openSync
191+
},
192+
child_process: {
193+
spawn: spawn
194+
}
195+
}).spawnChildProcess;
196+
197+
spawnChildProcess('/tmp', _process);
198+
199+
spawn.callCount.should.equal(1);
200+
spawn.args[0][0].should.equal(_process.execPath);
201+
var expected = [].concat(_process.argv);
202+
expected.shift();
203+
spawn.args[0][1].should.deep.equal(expected);
204+
var cp_opt = {
205+
stdio: ['ignore', fd, fd],
206+
env: _process.env,
207+
cwd: '',
208+
detached: true
209+
};
210+
spawn.args[0][2].should.deep.equal(cp_opt);
211+
openSync.callCount.should.equal(1);
212+
openSync.args[0][0].should.equal('/tmp/bitcore-node.log');
213+
openSync.args[0][1].should.equal('a+');
214+
});
215+
it('should not spawn a new child process if there is already a daemon running', function() {
216+
var _process = {
217+
exit: function() {},
218+
env: {
219+
__bitcore_node: true
220+
},
221+
argv: [
222+
'node',
223+
'bitcore-node'
224+
],
225+
cwd: 'cwd',
226+
pid: 999,
227+
execPath: '/tmp'
228+
};
229+
var spawnChildProcess = proxyquire('../../lib/scaffold/start', {}).spawnChildProcess;
230+
spawnChildProcess('/tmp', _process).should.equal(999);
231+
});
232+
});
233+
describe('daemon', function() {
234+
var sandbox;
235+
var spawn;
236+
var setup;
237+
var registerSync;
238+
var registerExit;
239+
var start = require('../../lib/scaffold/start');
240+
var options = {
241+
config: {
242+
datadir: '/tmp',
243+
daemon: true
244+
}
245+
}
246+
beforeEach(function() {
247+
sandbox = sinon.sandbox.create();
248+
spawn = sandbox.stub(start, 'spawnChildProcess', function() {});
249+
setup = sandbox.stub(start, 'setupServices', function() {});
250+
registerSync = sandbox.stub(start, 'registerSyncHandlers', function() {});
251+
registerExit = sandbox.stub(start, 'registerExitHandlers', function() {});
252+
});
253+
afterEach(function() {
254+
sandbox.restore();
255+
});
256+
it('call spawnChildProcess if there is a config option to do so', function() {
257+
start(options);
258+
spawn.callCount.should.equal(1);
259+
});
260+
it('not call spawnChildProcess if there is not an option to do so', function() {
261+
options.config.daemon = false;
262+
start(options);
263+
spawn.callCount.should.equal(0);
264+
});
265+
});
166266
});

0 commit comments

Comments
 (0)