Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[dist] EE first draft not functional yet but it's the aproach that we…

… are going to be in use
  • Loading branch information...
commit 0487e4b4156c1895f868aba0dedabc189f7e73dc 1 parent f0144ea
@alejandro alejandro authored
Showing with 52 additions and 14 deletions.
  1. +52 −14 lib/watcher.js
View
66 lib/watcher.js
@@ -6,15 +6,27 @@
* @name: watcher.js
* @licence: GNU Affero
*/
-var watcher = function (options) {
- var bolt = require('bolt');
- var exec = require('child_process').exec;
- var mesh = new bolt.Node({
+var exec = require('child_process').exec;
+var bolt = require('bolt');
+var ee = require('events').EventEmmiter;
+var utils = require('util');
+
+var Watcher = function (cfg) {
+ console.log('App in audit mode by Watcher...')
+ this.mesh = new bolt.Node({
delimiter:'::',
- host: options.host || 'localhost',
- port: options.port || 6379,
- auth: options.auth || ''
+ host: cfg.host || 'localhost',
+ port: cfg.port || 6379,
+ auth: cfg.auth || '',
+ silent:true
});
+ return this;
+};
+
+utils.inherits(Watcher,ee);
+
+Watcher.prototype.emitter = function(options){
+ var mesh = this.mesh;
mesh.start()
options = options || {};
process.maxMemory = options.maxMemory ? options.maxMemory*1024*1024:-1;
@@ -27,7 +39,7 @@ var watcher = function (options) {
if (options.maxMemory != -1) {
var memory = process.memoryUsage().rss;
if (memory >= options.maxMemory) {
- kill('You reached '+Math.round((options.maxMemory/1024/1024)+'Mbs',101);
+ kill('You reached '+options.maxMemory+'Mbs',101);
}
}
if (options.checkSpace){
@@ -37,9 +49,10 @@ var watcher = function (options) {
var mem =row.split(' ')[0]
return !isNaN(parseInt(mem)) ? parseInt(mem):0;
});
+ // Evil eval is evil, too lazy right now to make the map|forEach|filter function
var totalSize =eval(size.join('+')));
if (totalSize>=options.sizeSpace){
- kill('There is no space left in for this device, max allowed: '+(options.sizeSpace/1024),911)
+ kill('There is no space left in for this app, max allowed: '+(options.sizeSpace/1024),911)
}
}
});
@@ -50,13 +63,38 @@ var watcher = function (options) {
console.log('process error: ' + code);
console.log('app emiting restart signal && dieing...')
mesh.emit('nodester::dieing',{PID:process.pid, execPath: process.cwd(), port: process.env['app_port'],code:code});
+ // Let's have some time to emit the deing event
setTimeout(function(){
process.exit(0);
},500)
}
- mesh.on('nodester::kill', function(data){
- console.log('Receiving kill signal by nodester, dieing without restart')
- process.exit(0);
+ mesh.on('nodester::kill', function(PID){
+ if (PID === process.pid){
+ console.log('Receiving kill signal by nodester, dieing without restart')
+ process.exit(0);
+ }
+ });
+ setInterval(smash, options.interval);
+}
+Watcher.prototype.listener = function(){
+ var mesh = this.mesh;
+ mesh.on('nodester::dieing', function(data){
+ this.emit('killProcess',data);
});
- setInterval(checkConditions, options.interval);
-}
+}
+Watcher.prototype.listener.prototype.kill = function(PID){
+ this.mesh.emit('nodester::kill',PID);
+}
+
+Object.defineProperty(Watcher, 'codes', {
+ get: function(){
+ var code = {
+ 911:'Quota Exceeded :: Disk Space'
+ , 101:'Quota Exceeded :: Memory abuse'
+ , 102:'Audit by Master :: Kill signal inmminent'
+ }
+ return code;
+ }
+})
+
+module.exports = watcher;
Please sign in to comment.
Something went wrong with that request. Please try again.