Skip to content
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

update uuid & stop forever #158

Merged
merged 3 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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