Skip to content

Commit

Permalink
unlink pid
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Mar 15, 2018
1 parent 5c4aada commit 12ad4b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 21 additions & 6 deletions cli/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,28 @@ const setup = function setup() {
};
var child = new(forever.Monitor)(path.join(__dirname, '..', 'app.js'), foreverOptions);
if (options.action == "start") {
child.start();
fs.appendFileSync(pidpath, process.pid+'|');
try {
fs.appendFileSync(pidpath, process.pid+'|');
child.start();
} catch (piderr) {
console.error('failed to start microgateway: ' + piderr);
process.exit(1);
}
} else {
var pids = fs.readFileSync(pidpath,'utf8').split('|');
process.kill(pids[1], 'SIGINT');
fs.unlinkSync(pidpath);
process.kill(pids[0], 'SIGINT');
try {
var pids = fs.readFileSync(pidpath,'utf8').split('|');
if (pids) {
pids.forEach(function(pid){
process.kill(parseInt(pid), 'SIGINT');
});
fs.unlinkSync(pidpath);
} else {
console.log('pid file not found. please run this command from the folder where microgateway was started.')
}
} catch (piderr) {
console.error('failed to stop microgateway: ' + piderr);
process.exit(1);
}
}
});

Expand Down
4 changes: 3 additions & 1 deletion cli/lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const JsonSocket = require('./json-socket');
const configLocations = require('../../config/locations');
const isWin = /^win/.test(process.platform);
const ipcPath = configLocations.getIPCFilePath();
const pidPath = configLocations.getPIDFilePath();
const defaultPollInterval = 600;
const uuid = require('uuid/v1');
const debug = require('debug')('microgateway');
Expand Down Expand Up @@ -118,12 +119,13 @@ Gateway.prototype.start = (options) => {

mgCluster.run();
console.log('PROCESS PID : ' + process.pid);
fs.appendFileSync(configLocations.getPIDFilePath(), process.pid);
fs.appendFileSync(pidPath, process.pid);

process.on('exit', () => {
if (!isWin) {
console.log('Removing the socket file as part of cleanup');
fs.unlinkSync(ipcPath);
fs.unlinkSync(pidPath)
}
});

Expand Down

0 comments on commit 12ad4b0

Please sign in to comment.