From 6aac4af80ffcab7fcb107c6fe52ed97efbe96204 Mon Sep 17 00:00:00 2001 From: baldo Date: Fri, 29 Jul 2016 23:09:43 +0200 Subject: [PATCH] Job to automatically delete nodes after 100 days. --- server/jobs/nodeInformationCleanupJob.js | 17 ------ server/jobs/offlineNodesDeletionJob.js | 17 ++++++ server/jobs/scheduler.js | 2 +- server/services/monitoringService.js | 72 ++++++++++++++++++------ 4 files changed, 74 insertions(+), 34 deletions(-) delete mode 100644 server/jobs/nodeInformationCleanupJob.js create mode 100644 server/jobs/offlineNodesDeletionJob.js diff --git a/server/jobs/nodeInformationCleanupJob.js b/server/jobs/nodeInformationCleanupJob.js deleted file mode 100644 index 5bb9e55..0000000 --- a/server/jobs/nodeInformationCleanupJob.js +++ /dev/null @@ -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(); - }); - } - }; -}); diff --git a/server/jobs/offlineNodesDeletionJob.js b/server/jobs/offlineNodesDeletionJob.js new file mode 100644 index 0000000..3d68797 --- /dev/null +++ b/server/jobs/offlineNodesDeletionJob.js @@ -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(); + }); + } + }; +}); diff --git a/server/jobs/scheduler.js b/server/jobs/scheduler.js index e4efbc1..0519b97 100644 --- a/server/jobs/scheduler.js +++ b/server/jobs/scheduler.js @@ -76,7 +76,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi if (config.client.monitoring.enabled) { schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); 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) { diff --git a/server/services/monitoringService.js b/server/services/monitoringService.js index 8f651de..e962460 100644 --- a/server/services/monitoringService.js +++ b/server/services/monitoringService.js @@ -27,6 +27,10 @@ angular.module('ffffng') 2: { amount: 1, unit: 'days' }, 3: { amount: 7, unit: 'days' } }; + var DELETE_OFFLINE_NODES_AFTER_DURATION = { + amount: 100, + unit: 'days' + }; var previousImportTimestamp = null; @@ -663,26 +667,62 @@ angular.module('ffffng') }); }, - cleanupNodeInformation: function (callback) { - var daysBeforeCleanup = 30; + deleteOfflineNodes: function (callback) { Logger - .tag('monitoring', 'information-cleanup') - .debug('Cleaning up node data not updated for %s days...', daysBeforeCleanup); - Database.run( - 'DELETE FROM node_state WHERE modified_at < ?', - [moment().subtract(daysBeforeCleanup, 'days').unix()], - function (err) { - if (err) { - return callback(err); - } + .tag('nodes', 'delete-offline') + .info( + 'Deleting offline nodes older than ' + + DELETE_OFFLINE_NODES_AFTER_DURATION.amount + ' ' + + DELETE_OFFLINE_NODES_AFTER_DURATION.unit + ); - Logger - .tag('monitoring', 'information-retrieval') - .debug('Node data cleanup done.'); + 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) { + Logger.tag('nodes', 'delete-offline').error('Error getting node ' + mac, err); + return nodeCallback(err); + } - callback(); + NodeService.deleteNode(node.token, function (err) { + if (err) { + Logger.tag('nodes', 'delete-offline').error('Error deleting node ' + mac, err); + return nodeCallback(err); + } + + 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 + ); } ); } - }; + } });