Skip to content

Commit

Permalink
use forever-monitor to autostart MG
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandan Sridhar committed Sep 25, 2017
1 parent b42f5e4 commit 48626e6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Kevin Swiber <[email protected]> (https://github.com/kevinswiber)
Mitch Fierro (https://github.com/F1ERRO)
Matt Dobson <[email protected]> (https://github.com/mdobson)
Frank Greco (https://github.com/frankgreco)
Srinandan Sridhar <[email protected]> (https://github.com/srinandan)
46 changes: 44 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

var request = require('request');
var url = require('url');
var fs = require('fs');
var run = require('./cli/lib/gateway')();
var portastic = require('portastic');
const os = require('os');

const options = {};
Expand All @@ -9,8 +13,46 @@ options.env = process.env.EDGEMICRO_ENV;
options.key = process.env.EDGEMICRO_KEY;
options.secret = process.env.EDGEMICRO_SECRET;
options.org = process.env.EDGEMICRO_ORG;
options.configDir = process.env.EDGEMICRO_CONFIG_DIR;
options.configDir = process.env.EDGEMICRO_CONFIG_DIR || os.homedir()+"/.edgemicro";
options.processes = process.env.EDGEMICRO_PROCESSES || os.cpus().length;
options.port = process.env.PORT || 8000;

run.start(options);
if (!options.key ) { console.log('key is required'); process.exit(1);}
if (!options.secret ) { console.log('secret is required'); process.exit(1);}
if (!options.org ) { console.log('org is required'); process.exit(1);}
if (!options.env ) { console.log('env is required'); process.exit(1);}
if (options.port) {
portastic.test(options.port)
.then(function(isAvailable){
if(!isAvailable) {
console.error('port is not available.');
process.exit(1);
}
});
}
if (process.env.EDGEMICRO_CONFIG_URL) {
var fileName = options.org+"-"+options.env+"-config.yaml";
var filePath = os.homedir() + "/.edgemicro/" + fileName;

var parsedUrl = url.parse(process.env.EDGEMICRO_CONFIG_URL, true);
if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") {
request.get(process.env.EDGEMICRO_CONFIG_URL, function(error, response, body) {
if (error) {
console.error("config file did not download");
process.exit(1);
}
try {
fs.writeFileSync(filePath, body, 'utf8');
run.start(options);
} catch (err) {
console.error("config file could not be written: " + err);
process.exit(1);
}
});
} else {
console.error("url protocol not supported");
process.exit(1);
}
} else {
run.start(options);
}
2 changes: 1 addition & 1 deletion cli/edgemicro
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'use strict';
var edgemicroVersion = require('../package.json').version;
const version = process.version;
const lts_version = "v4.2.0";
const lts_version = "v4.8.4";
console.log('current nodejs version is %s',version)
console.log('current edgemicro version is %s',edgemicroVersion);
if(version < lts_version){
Expand Down
7 changes: 6 additions & 1 deletion cli/lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const util = require('util')
const fs = require('fs')
const assert = require('assert');
const configLocations = require('../../config/locations');

const BUFFERSIZE = 10000;
const BATCHSIZE = 500;
const FLUSHINTERVAL = 5000;
var defaultConfig ;

var certLib = require('./cert-lib')
Expand Down Expand Up @@ -184,6 +186,9 @@ function configureEdgemicroWithCreds(options, cb) {
}

agentConfig['analytics']['uri'] = bootstrapUri.replace('bootstrap', 'axpublisher');
agentConfig['analytics']['bufferSize'] = BUFFERSIZE;
agentConfig['analytics']['batchSize'] = BATCHSIZE;
agentConfig['analytics']['flushInterval'] = FLUSHINTERVAL;
}

console.log();
Expand Down
9 changes: 2 additions & 7 deletions cli/lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,7 @@ Gateway.prototype.start = (options) => {
console.error(err);
debug('Caught Unhandled Exception:');
debug(err);
if (config.edgemicro.autostart) {
debug("Reloading edgemicro...Restart Count: "+restartCount);
reloadOnConfigChange(config, cache,{source: source, keys:keys});
} else {
debug('enable autostart to reload MG automatically');
process.exit(0);
}
process.exit(0);
});

var shouldNotPoll = config.edgemicro.disable_config_poll_interval || false;
Expand Down Expand Up @@ -266,3 +260,4 @@ function hasConfigChanged(oldConfig, newConfig) {
// This may not be the best way to do the check. But it works for now.
return JSON.stringify(oldConfig) != JSON.stringify(newConfig);
}

14 changes: 14 additions & 0 deletions forever.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var forever = require('forever-monitor');
try {
var foreverOptions = require(__dirname+'/forever.json');
} catch (err) {
console.error(err);
console.log("using default forever options");
var foreverOptions = { max: 3, silent: false, killTree: true, minUptime: 2000 };
}

var child = new (forever.Monitor)(__dirname+'/app.js', foreverOptions);

child.start();
6 changes: 6 additions & 0 deletions forever.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"silent": false,
"killTree": true,
"minUptime": 2000,
"max": 3
}
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edgemicro",
"version": "2.5.4-beta",
"version": "2.5.6-beta",
"description": "Apigee Edge Microgateway",
"main": "index.js",
"license": "Apache-2.0",
Expand All @@ -16,6 +16,7 @@
"config": "^1.13.0",
"cpr": "^1.0.0",
"debug": "^2.2.0",
"forever-monitor": "^1.7.1",
"fs-extra": "^0.20.1",
"js-yaml": "^3.5.4",
"jsonwebtoken": "^5.7.0",
Expand All @@ -26,12 +27,12 @@
"microgateway-edgeauth": "^2.3.0-beta",
"microgateway-plugins": "^2.3.12-beta",
"pem": "^1.8.1",
"portastic": "^1.0.1",
"request": "^2.67.0",
"rimraf": "^2.4.3",
"tmp": "0.0.28",
"uuid": "^2.0.1",
"xml2js": "^0.4.16",
"portastic": "^1.0.1"
"xml2js": "^0.4.16"
},
"devDependencies": {
"gulp": "^3.8.11",
Expand Down Expand Up @@ -81,12 +82,16 @@
"Shawn Feldman (https://github.com/shawnfeldman)",
"Kevin Swiber <[email protected]> (https://github.com/kevinswiber)",
"Matt Dobson <[email protected]> (https://github.com/mdobson)",
"Mitch Fierro <[email protected]> (https://github.com/f1erro)"
"Mitch Fierro <[email protected]> (https://github.com/f1erro)",
"Srinandan Sridhar <[email protected]> (https://github.com/srinandan)"
],
"author": "Kevin Swiber <[email protected]>",
"engines" : {
"node" : ">= 4.8.2",
"npm": ">=3.10.8"
"engines": {
"node": ">= 4.8.4",
"npm": ">=3.10.8"
},
"cpu" : [ "!arm", "!mips" ]
"cpu": [
"!arm",
"!mips"
]
}

0 comments on commit 48626e6

Please sign in to comment.