diff --git a/cli/cmd.js b/cli/cmd.js index 600ccd0e1..5e96f8c7e 100644 --- a/cli/cmd.js +++ b/cli/cmd.js @@ -14,10 +14,12 @@ const rotatekey = require('./lib/rotate-key')(); const verify = require('./lib/verify')(); const run = require('./lib/gateway')(); const keyGenerator = require('./lib/key-gen')(); +const configLocations = require('../config/locations'); const prompt = require('cli-prompt'); const init = require('./lib/init'); var foreverOptions = require('../forever.json'); const forever = require('forever-monitor'); +const pidpath = configLocations.getPIDFilePath(); var portastic = require('portastic'); const setup = function setup() { @@ -249,8 +251,7 @@ const setup = function setup() { } }); } - } - else run.reload(options); + } else run.reload(options); }); commander @@ -270,10 +271,18 @@ const setup = function setup() { commander .command('forever') .option('-f, --file ', 'forever-monitor options file') + .option('-a,--action ', 'action can be start or stop; default is start') .description('Start microgateway using forever-monitor') .action((options) => { + options.action = options.action || "start"; + options.error = optionError; if (options.file) { - foreverOptions = JSON.parse(fs.readFileSync(options.file, {encoding: 'utf8'})); + foreverOptions = JSON.parse(fs.readFileSync(options.file, { + encoding: 'utf8' + })); + } + if (options.action !== "start" && options.action !== "stop") { + return options.error('action must be start or stop'); } foreverOptions ? foreverOptions : { max: 3, @@ -282,7 +291,30 @@ const setup = function setup() { minUptime: 2000 }; var child = new(forever.Monitor)(path.join(__dirname, '..', 'app.js'), foreverOptions); - child.start(); + if (options.action == "start") { + try { + fs.appendFileSync(pidpath, process.pid+'|'); + child.start(); + } catch (piderr) { + console.error('failed to start microgateway: ' + piderr); + process.exit(1); + } + } else { + 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); + } + } }); commander @@ -435,4 +467,4 @@ function promptForPassword(options, cb) { } -module.exports = setup; +module.exports = setup; \ No newline at end of file diff --git a/cli/lib/gateway.js b/cli/lib/gateway.js index 94f93c380..0b0f77f64 100644 --- a/cli/lib/gateway.js +++ b/cli/lib/gateway.js @@ -10,8 +10,9 @@ 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'); +const uuid = require('uuid/v1'); const debug = require('debug')('microgateway'); const jsdiff = require('diff'); @@ -32,6 +33,7 @@ Gateway.prototype.start = (options) => { } catch (e) { // Socket does not exist // so ignore and proceed + debug(e); } const source = configLocations.getSourcePath(options.org, options.env, options.configDir); @@ -74,7 +76,7 @@ Gateway.prototype.start = (options) => { edgeconfig.save(config, cache); } - config.uid = uuid.v1(); + config.uid = uuid(); var logger = gateway.Logging.init(config); var opt = {}; delete args.keys; @@ -117,11 +119,13 @@ Gateway.prototype.start = (options) => { mgCluster.run(); console.log('PROCESS PID : ' + 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) } }); @@ -133,7 +137,7 @@ Gateway.prototype.start = (options) => { process.exit(0); }); - process.on('uncaughtException', () => { + process.on('uncaughtException',(err) => { console.error(err); debug('Caught Unhandled Exception:'); debug(err); diff --git a/package.json b/package.json index 2a8874fd1..06920925e 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "request": "^2.67.0", "rimraf": "^2.4.3", "tmp": "0.0.28", - "uuid": "^2.0.1", + "uuid": "^3.2.1", "volos-cache-memory": "^0.10.1", "volos-spikearrest-common": "^0.10.3", "volos-spikearrest-memory": "^0.10.1",