From a493634cf1fc72feaf35e90722954d04ac85d7ab Mon Sep 17 00:00:00 2001 From: Vishnu Kyatannawar Date: Sun, 25 Jun 2017 03:04:20 -0400 Subject: [PATCH] 87: Add ability to pause/resume monitoring a service. --- lib/sentinel.js | 9 ++- lib/watchmen.js | 2 +- .../public/js/controllers/services-list.js | 20 +++++++ webserver/public/js/factories.js | 16 +++++ webserver/public/less/service-list.less | 2 +- webserver/routes/api-service-route.js | 58 +++++++++++++++++++ webserver/views/service-list.html | 12 +++- 7 files changed, 112 insertions(+), 7 deletions(-) diff --git a/lib/sentinel.js b/lib/sentinel.js index b38c1c59..2e5b1015 100644 --- a/lib/sentinel.js +++ b/lib/sentinel.js @@ -25,7 +25,8 @@ var FIELDS_WHICH_MODIFICATION_TRIGGER_SERVICE_RESTART = [ 'alertTo', 'pingServiceName', 'pingServiceOptions', - 'warningThreshold' + 'warningThreshold', + 'isPaused' ]; var timeoutId = null; @@ -34,7 +35,7 @@ Sentinel.prototype._findAdded = function (databaseServices, runningServices) { databaseServices.forEach(function (s) { if (!_.find(runningServices, function (rs) { return rs.id == s.id; - })) { + }) && !s.isPaused) { added.push(s); } }); @@ -112,7 +113,9 @@ Sentinel.prototype._tick = function () { if (modifiedServices.length) { modifiedServices.forEach(function (s) { self.watchmen.removeService(s.id); - self.watchmen.addService(s); + if (!s.isPaused) { + self.watchmen.addService(s); + } console.log('changes detected in service: ', s.name, '. Restarting...'); }); } diff --git a/lib/watchmen.js b/lib/watchmen.js index 7c522dcc..429078f7 100644 --- a/lib/watchmen.js +++ b/lib/watchmen.js @@ -186,7 +186,7 @@ WatchMen.prototype.startAll = function (options) { } this.services.forEach(function (service) { - if (!service.running) { + if (!service.running && !service.isPaused) { setTimeout(function(){ self._launch(service); service.running = true; diff --git a/webserver/public/js/controllers/services-list.js b/webserver/public/js/controllers/services-list.js index c500c276..e23b389b 100644 --- a/webserver/public/js/controllers/services-list.js +++ b/webserver/public/js/controllers/services-list.js @@ -106,6 +106,26 @@ } }; + $scope.pause = function (id) { + if (confirm('Are you sure you want to pause this service?')) { + Service.pause({id: id}, function () { + reload(function () { + }, function () { + $scope.errorLoadingServices = "Error loading data from remote server"; + }); + }); + } + }; + + $scope.resume = function (id) { + Service.resume({id: id}, function () { + reload(function () { + }, function () { + $scope.errorLoadingServices = "Error loading data from remote server"; + }); + }); + }; + reload(scheduleNextTick, loadServicesErrHandler); }); diff --git a/webserver/public/js/factories.js b/webserver/public/js/factories.js index 3f4e573e..927a35f2 100644 --- a/webserver/public/js/factories.js +++ b/webserver/public/js/factories.js @@ -41,6 +41,22 @@ reset: { method: 'POST', url: '/api/services/:id/reset' + }, + + /** + * Pause service + */ + pause: { + method: 'POST', + url: '/api/services/:id/pause' + }, + + /** + * Resume service + */ + resume: { + method: 'POST', + url: '/api/services/:id/resume' } }); diff --git a/webserver/public/less/service-list.less b/webserver/public/less/service-list.less index 22120ed3..849591f1 100644 --- a/webserver/public/less/service-list.less +++ b/webserver/public/less/service-list.less @@ -54,7 +54,7 @@ .dropdown-menu{ padding:10px; - width:250px; + width:370px; text-align: center; .btn { diff --git a/webserver/routes/api-service-route.js b/webserver/routes/api-service-route.js index a6342fca..1f31bcee 100644 --- a/webserver/routes/api-service-route.js +++ b/webserver/routes/api-service-route.js @@ -168,5 +168,63 @@ module.exports.getRoutes = function (storage) { }); }); + /** + * Pause service + */ + + router.post('/services/:id/pause', requireAdmin, function (req, res) { + var id = req.params.id; + if (!id) { + return res.status(404).json({error: 'ID parameter not found'}); + } + storage.getService(id, function (err, service) { + if (err) { + return res.status(500).json({error: err}); + } + + if (!service) { + return res.status(404).json({error: 'service not found'}); + } + + service.isPaused = 1; + + storage.updateService(service, function (err, service) { + if (err) { + return res.status(500).json({error: err}); + } + return res.json(service); + }); + }); + }); + + /** + * Resume service + */ + + router.post('/services/:id/resume', requireAdmin, function (req, res) { + var id = req.params.id; + if (!id) { + return res.status(404).json({error: 'ID parameter not found'}); + } + storage.getService(id, function (err, service) { + if (err) { + return res.status(500).json({error: err}); + } + + if (!service) { + return res.status(404).json({error: 'service not found'}); + } + + service.isPaused = 0; + + storage.updateService(service, function (err, service) { + if (err) { + return res.status(500).json({error: err}); + } + return res.json(service); + }); + }); + }); + return router; }; diff --git a/webserver/views/service-list.html b/webserver/views/service-list.html index 3cc5b17d..c9f47e78 100644 --- a/webserver/views/service-list.html +++ b/webserver/views/service-list.html @@ -41,6 +41,13 @@ edit + + + @@ -53,8 +60,9 @@ <% } %> - offline - online + offline + online + Paused