Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
fix: "start" should wait for "stop" to remove the pid file(using Prom…
Browse files Browse the repository at this point in the history
…ise) (#1220)
  • Loading branch information
stoneChen authored and fengmk2 committed Sep 5, 2017
1 parent 6c019de commit 43ffa99
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,59 @@ program.parse(process.argv);


function start(options) {
stop(options);
var dataDir = options.dataDir || path.join(process.env.HOME, '.cnpmjs.org');
mkdirp.sync(dataDir);

var configfile = path.join(dataDir, 'config.json');
var config = {};
if (fs.existsSync(configfile)) {
try {
config = require(configfile);
} catch (err) {
console.warn('load old %s error: %s', configfile, err);
}
}
// config.enableCluster = !!options.cluster;
if (options.admins) {
config.admins = {};
for (var i = 0; i < options.admins.length; i++) {
config.admins[options.admins[i]] = options.admins[i] + '@localhost.com';
}
}
if (options.scopes) {
config.scopes = options.scopes.map(function (name) {
if (name[0] !== '@') {
name = '@' + name;
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);

var configfile = path.join(dataDir, 'config.json');
var config = {};
if (fs.existsSync(configfile)) {
try {
config = require(configfile);
} catch (err) {
console.warn('load old %s error: %s', configfile, err);
}
}
// config.enableCluster = !!options.cluster;
if (options.admins) {
config.admins = {};
for (var i = 0; i < options.admins.length; i++) {
config.admins[options.admins[i]] = options.admins[i] + '@localhost.com';
}
}
if (options.scopes) {
config.scopes = options.scopes.map(function (name) {
if (name[0] !== '@') {
name = '@' + name;
}
return name;
});
}
return name;
});
}

var configJSON = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, configJSON);
var configJSON = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, configJSON);

debug('save config %s to %s', configJSON, configfile);
debug('save config %s to %s', configJSON, configfile);

// if sqlite db file not exists, init first
initDatabase(function() {
require('../dispatch');
});
// if sqlite db file not exists, init first
initDatabase(function() {
require('../dispatch');
});

fs.writeFileSync(path.join(dataDir, 'pid'), process.pid + '');
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) {
Expand All @@ -102,8 +109,9 @@ function stop(options) {
}
console.log('cnpmjs.org server:%d stop', pid);
fs.unlinkSync(pidfile);
resolve();
});
}
});
}

function initDatabase(callback) {
Expand Down

0 comments on commit 43ffa99

Please sign in to comment.