Job to automatically delete nodes after 100 days.

This commit is contained in:
baldo 2016-07-29 23:09:43 +02:00
parent cacc3faeb5
commit 6aac4af80f
4 changed files with 74 additions and 34 deletions

View file

@ -1,17 +0,0 @@
'use strict';
angular.module('ffffng').factory('NodeInformationCleanupJob', function (MonitoringService, Logger) {
return {
description: 'Cleanup monitoring status entries for nodes no longer having monitoring enabled.',
run: function (callback) {
MonitoringService.cleanupNodeInformation(function (err) {
if (err) {
Logger.tag('monitoring', 'information-cleanup').error('Error cleaning up node data:', err);
}
callback();
});
}
};
});

View file

@ -0,0 +1,17 @@
'use strict';
angular.module('ffffng').factory('OfflineNodesDeletionJob', function (MonitoringService, Logger) {
return {
description: 'Delete nodes that are offline for more than 100 days.',
run: function (callback) {
MonitoringService.deleteOfflineNodes(function (err) {
if (err) {
Logger.tag('nodes', 'delete-offline').error('Error deleting offline nodes:', err);
}
callback();
});
}
};
});

View file

@ -76,7 +76,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
if (config.client.monitoring.enabled) { if (config.client.monitoring.enabled) {
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');
schedule('45 */5 * * * *', 'MonitoringMailsSendingJob'); schedule('45 */5 * * * *', 'MonitoringMailsSendingJob');
schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00 schedule('0 0 3 * * *', 'OfflineNodesDeletionJob'); // every night at 3:00
} }
} }
catch (error) { catch (error) {

View file

@ -27,6 +27,10 @@ angular.module('ffffng')
2: { amount: 1, unit: 'days' }, 2: { amount: 1, unit: 'days' },
3: { amount: 7, unit: 'days' } 3: { amount: 7, unit: 'days' }
}; };
var DELETE_OFFLINE_NODES_AFTER_DURATION = {
amount: 100,
unit: 'days'
};
var previousImportTimestamp = null; var previousImportTimestamp = null;
@ -663,26 +667,62 @@ angular.module('ffffng')
}); });
}, },
cleanupNodeInformation: function (callback) { deleteOfflineNodes: function (callback) {
var daysBeforeCleanup = 30;
Logger Logger
.tag('monitoring', 'information-cleanup') .tag('nodes', 'delete-offline')
.debug('Cleaning up node data not updated for %s days...', daysBeforeCleanup); .info(
Database.run( 'Deleting offline nodes older than ' +
'DELETE FROM node_state WHERE modified_at < ?', DELETE_OFFLINE_NODES_AFTER_DURATION.amount + ' ' +
[moment().subtract(daysBeforeCleanup, 'days').unix()], DELETE_OFFLINE_NODES_AFTER_DURATION.unit
function (err) { );
Database.all(
'SELECT * FROM node_state WHERE state = ? AND last_seen < ?',
[
'OFFLINE',
moment().subtract(
DELETE_OFFLINE_NODES_AFTER_DURATION.amount,
DELETE_OFFLINE_NODES_AFTER_DURATION.unit
).unix()
],
function (err, rows) {
async.eachSeries(
rows,
function (row, nodeCallback) {
var mac = row.mac;
Logger.tag('nodes', 'delete-offline').info('Deleting node ' + mac);
NodeService.getNodeDataByMac(mac, function (err, node) {
if (err) { if (err) {
return callback(err); Logger.tag('nodes', 'delete-offline').error('Error getting node ' + mac, err);
return nodeCallback(err);
} }
Logger NodeService.deleteNode(node.token, function (err) {
.tag('monitoring', 'information-retrieval') if (err) {
.debug('Node data cleanup done.'); Logger.tag('nodes', 'delete-offline').error('Error deleting node ' + mac, err);
return nodeCallback(err);
}
callback(); Database.run(
'DELETE FROM node_state WHERE mac = ? AND state = ?',
[mac, 'OFFLINE'],
function (err) {
if (err) {
Logger
.tag('nodes', 'delete-offline')
.error('Error deleting monitoring data for node ' + mac, err);
return nodeCallback(err);
}
nodeCallback(null);
}
);
});
});
},
callback
);
} }
); );
} }
}; }
}); });