fix: "start" should wait for "stop" to remove the pid file(using Promise) (#1220)

This commit is contained in:
cloudstone
2017-09-05 11:04:14 +08:00
committed by fengmk2
parent 6c019de514
commit 43ffa995cb

View File

@@ -48,7 +48,9 @@ program.parse(process.argv);
function start(options) {
stop(options);
stop(options)
// wait for "stop" method to remove the pid file
.then(function () {
var dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
mkdirp.sync(dataDir);
@@ -88,12 +90,17 @@ function start(options) {
});
fs.writeFileSync(path.join(dataDir, 'pid'), process.pid + '');
});
}
function stop(options) {
var dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
var pidfile = path.join(dataDir, 'pid');
if (fs.existsSync(pidfile)) {
return new Promise(function (resolve) {
if (!fs.existsSync(pidfile)) {
resolve();
return;
}
var pid = Number(fs.readFileSync(pidfile, 'utf8'));
treekill(pid, function (err) {
if (err) {
@@ -102,8 +109,9 @@ function stop(options) {
}
console.log('cnpmjs.org server:%d stop', pid);
fs.unlinkSync(pidfile);
resolve();
});
});
}
}
function initDatabase(callback) {