Merge pull request #122 from cubedro/develop

Added cleanup for old inactive nodes
This commit is contained in:
Marian OANCΞA 2015-05-12 02:24:10 +03:00
commit 5f1691a83c
3 changed files with 57 additions and 0 deletions

15
app.js
View File

@ -185,6 +185,21 @@ var latencyTimeout = setInterval( function ()
client.write({ action: 'client-ping' }); client.write({ action: 'client-ping' });
}, 5000); }, 5000);
// Cleanup old inactive nodes
var nodeCleanupTimeout = setInterval( function ()
{
client.write({
action: 'init',
data: Nodes.all()
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}, 1000*60*60);
// view engine setup // view engine setup
app.set('views', path.join(__dirname, 'views')); app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); app.set('view engine', 'jade');

View File

@ -115,9 +115,34 @@ Collection.prototype.getNodeOrNew = function(search, data)
Collection.prototype.all = function() Collection.prototype.all = function()
{ {
this.removeOldNodes();
return this._items; return this._items;
} }
Collection.prototype.removeOldNodes = function()
{
var deleteList = []
for(var i = this._items.length - 1; i >= 0; i--)
{
var node = this._items[i];
if( node.isInactiveAndOld() )
{
deleteList.push(i);
}
}
if(deleteList.length > 0)
{
for(var i = 0; i < deleteList.length; i++)
{
this._items.splice(deleteList[i], 1);
}
}
}
Collection.prototype.blockPropagationChart = function() Collection.prototype.blockPropagationChart = function()
{ {
return this._blockchain.getBlockPropagation(); return this._blockchain.getBlockPropagation();

View File

@ -2,6 +2,7 @@ var geoip = require('geoip-lite');
var _ = require('lodash'); var _ = require('lodash');
var MAX_HISTORY = 40; var MAX_HISTORY = 40;
var MAX_INACTIVE_TIME = 1000*60*60*4;
var Node = function Node(data) var Node = function Node(data)
{ {
@ -180,4 +181,20 @@ Node.prototype.canUpdate = function()
return this.info.canUpdateHistory || false; return this.info.canUpdateHistory || false;
} }
Node.prototype.isInactiveAndOld = function()
{
if(this.stats.active)
return false;
var lastState = this.uptime.history[this.uptime.history.length -1];
if( !_.isUndefined(lastState) && !_.isUndefined(lastState.status) && !_.isUndefined(lastState.time) )
{
if( lastState.status === 'down' && (_.now() - lastState.time) > MAX_INACTIVE_TIME )
return true;
}
return false;
}
module.exports = Node; module.exports = Node;