Skip to content

Commit

Permalink
Merge pull request #158 from srinandan/master
Browse files Browse the repository at this point in the history
update uuid & stop forever
  • Loading branch information
srinandan authored Mar 15, 2018
2 parents 64bbf24 + 12ad4b0 commit 4c19d3c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
42 changes: 37 additions & 5 deletions cli/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -249,8 +251,7 @@ const setup = function setup() {
}
});
}
}
else run.reload(options);
} else run.reload(options);
});

commander
Expand All @@ -270,10 +271,18 @@ const setup = function setup() {
commander
.command('forever')
.option('-f, --file <file>', 'forever-monitor options file')
.option('-a,--action <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,
Expand All @@ -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
Expand Down Expand Up @@ -435,4 +467,4 @@ function promptForPassword(options, cb) {
}


module.exports = setup;
module.exports = setup;
10 changes: 7 additions & 3 deletions cli/lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
});

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4c19d3c

Please sign in to comment.