From e03be8bf51ec17524f363f51683d67676424479f Mon Sep 17 00:00:00 2001 From: baldo Date: Tue, 24 May 2016 20:39:51 +0200 Subject: [PATCH] Job to clean up outdated monitoring data. --- server/jobs/nodeInformationCleanupJob.js | 13 +++++++++++++ server/jobs/scheduler.js | 2 +- server/services/monitoringService.js | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 server/jobs/nodeInformationCleanupJob.js diff --git a/server/jobs/nodeInformationCleanupJob.js b/server/jobs/nodeInformationCleanupJob.js new file mode 100644 index 0000000..df31230 --- /dev/null +++ b/server/jobs/nodeInformationCleanupJob.js @@ -0,0 +1,13 @@ +'use strict'; + +angular.module('ffffng').factory('NodeInformationCleanupJob', function (MonitoringService, Logger) { + return { + run: function () { + MonitoringService.cleanupNodeInformation(function (err) { + if (err) { + Logger.tag('monitoring', 'information-cleanup').error('Error cleaning up node data:', err); + } + }); + } + }; +}); diff --git a/server/jobs/scheduler.js b/server/jobs/scheduler.js index 12bc2a4..c2cd1a4 100644 --- a/server/jobs/scheduler.js +++ b/server/jobs/scheduler.js @@ -32,7 +32,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi if (config.client.monitoring.enabled) { schedule('30 */5 * * * *', 'NodeInformationRetrievalJob'); - // schedule('0 */1 * * * *', 'NodeInformationCleanupJob'); + schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00 } } catch (error) { diff --git a/server/services/monitoringService.js b/server/services/monitoringService.js index 1d07aea..722cddf 100644 --- a/server/services/monitoringService.js +++ b/server/services/monitoringService.js @@ -307,6 +307,28 @@ angular.module('ffffng') ); }); }); + }, + + cleanupNodeInformation: function (callback) { + var daysBeforeCleanup = 30; + Logger + .tag('monitoring', 'information-cleanup') + .info('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); + } + + Logger + .tag('monitoring', 'information-retrieval') + .info('Node data cleanup done.'); + + callback(); + } + ); } }; });