New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复『全局命令cnpmjs.org stop不稳定』问题 #1220

Merged
merged 1 commit into from Sep 5, 2017
Jump to file or symbol
Failed to load files and symbols.
+45 −37
Diff settings

Always

Just for now

fix: method "start" should wait for method "stop" to remove the pid f…

…ile(using Promise)
  • Loading branch information...
chenshidong
chenshidong committed Sep 1, 2017
commit 17df5f045430e34f915c43929d173ce58157e51f
View
@@ -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) {
@@ -102,8 +109,9 @@ function stop(options) {
}
console.log('cnpmjs.org server:%d stop', pid);
fs.unlinkSync(pidfile);
resolve();
});
}
});
}
function initDatabase(callback) {
ProTip! Use n and p to navigate between commits in a pull request.