From f70f4766d98c40d7c5400b072679cba8143e54d4 Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Sat, 11 Apr 2015 21:42:22 +0300 Subject: [PATCH] Add tripwire module for restarting process if memory limit exeeds --- .gitignore | 1 + bin/index.js | 26 +++++++++++++++----------- config/tripwire.json | 8 ++++++++ package.json | 8 +++++--- 4 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 config/tripwire.json diff --git a/.gitignore b/.gitignore index e885836..b980aa7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ cache config/_bse.json config/_config.json +config/_tripwire.json diff --git a/bin/index.js b/bin/index.js index 65368e8..6708ef2 100644 --- a/bin/index.js +++ b/bin/index.js @@ -1,8 +1,11 @@ var fs = require('fs'), + path = require('path'), inherit = require('inherit'), - SnapshotMaster = require('../lib/master/index'), + fsExtra = require('fs-extra'), bseAdmin = require('bse-admin'), + Tripwire = require('memory-tripwire'), logger = require('bem-site-logger').createLogger(module), + SnapshotMaster = require('../lib/master/index'), DataBuilder = inherit(SnapshotMaster, { @@ -35,15 +38,16 @@ var fs = require('fs'), .then(function () { this['setIdle'](); }, this) .fail(function () { this['setIdle'](); }, this); } - }); + }), + tripWireConfig = fsExtra.readJSONFileSync('./config/_tripwire.json'), + appConfig = fsExtra.readJSONFileSync('./config/_config.json'), + tripwire = new Tripwire(tripWireConfig), + builder = new DataBuilder(appConfig); -fs.readFile('./config/_config.json', { encoding: 'utf-8' }, function (error, config) { - if (error) { - logger.error('Can\'t read configuration file for snapshot-master tool'); - throw error; - } - var builder = new DataBuilder(JSON.parse(config)); - builder.loadConfig(function () { - builder.start(); - }); +tripwire.start(); +tripwire.on('bomb', function () { + logger.warn('||| --- MEMORY LIMIT EXCEED. PROCESS WILL BE RESTARTED --- ||| '); +}); +builder.loadConfig(function () { + builder.start(); }); diff --git a/config/tripwire.json b/config/tripwire.json new file mode 100644 index 0000000..ddd4f3b --- /dev/null +++ b/config/tripwire.json @@ -0,0 +1,8 @@ +{ + "warning": "900mb", + "critical": "1000mb", + "interval": "1s", + "cycle": 3, + "exitTime": "5s", + "disconnectTime": "3s" +} diff --git a/package.json b/package.json index 762eb2d..5acc985 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "fstream": "^1.0.x", "inherit": "^2.2.x", "lodash": "^3.1.x", + "memory-tripwire": "^1.0.0", "moment": "^2.9.0", "tar": "^2.0.x", "trtd": "^0.0.x", @@ -42,9 +43,10 @@ "jscs": "^1.11.x" }, "scripts": { - "_app_config": "cp config/config.json config/_config.json", - "_bse_config": "cp config/bse.json config/_bse.json", - "config": "npm run _app_config && npm run _bse_config", + "_app_config": "cp -i config/config.json config/_config.json", + "_bse_config": "cp -i config/bse.json config/_bse.json", + "_tripwire_config": "cp -i config/tripwire.json config/_tripwire.json", + "config": "npm run _app_config && npm run _bse_config && npm run _tripwire_config", "mocha": "NODE_ENV=testing node_modules/.bin/mocha", "istanbul": "istanbul cover ./node_modules/mocha/bin/_mocha", "codestyle": "node_modules/.bin/jshint . && node_modules/.bin/jscs -c .jscs.js .",