From 53a0ecd3668afb0e1595aa2e05a23bef52c7631c Mon Sep 17 00:00:00 2001 From: baldo Date: Sun, 5 Jun 2016 12:18:37 +0200 Subject: [PATCH] Added possibility to enable / disable tasks. --- admin/index.html | 11 +++++ admin/js/config.js | 22 ++++++--- admin/js/taskActionButton.js | 9 ++-- server/jobs/scheduler.js | 9 ++-- server/jobs/testJob.js | 18 ------- server/resources/taskResource.js | 84 ++++++++++++++++++++++++++------ server/router.js | 2 + 7 files changed, 109 insertions(+), 46 deletions(-) delete mode 100644 server/jobs/testJob.js diff --git a/admin/index.html b/admin/index.html index c562c22..da1456f 100644 --- a/admin/index.html +++ b/admin/index.html @@ -3,6 +3,17 @@ Knotenverwaltung - Admin-Panel + +
diff --git a/admin/js/config.js b/admin/js/config.js index 91ad6c0..8c585d8 100644 --- a/admin/js/config.js +++ b/admin/js/config.js @@ -14,6 +14,13 @@ myApp.config(['NgAdminConfigurationProvider', function(NgAdminConfigurationProvi .baseApiUrl('/internal/api/') .debug(true); + function taskClasses(task) { + if (!task) { + return; + } + return task.values.enabled ? 'task-enabled' : 'task-disabled'; + } + var tasks = nga.entity('tasks').label('Background-Jobs'); tasks .listView() @@ -22,14 +29,17 @@ myApp.config(['NgAdminConfigurationProvider', function(NgAdminConfigurationProvi .batchActions([]) .exportFields([]) .fields([ - nga.field('id'), - nga.field('name'), - nga.field('schedule'), - nga.field('runningSince').map(formatMoment), - nga.field('lastRunStarted').map(formatMoment) + nga.field('id').cssClasses(taskClasses), + nga.field('name').cssClasses(taskClasses), + nga.field('schedule').cssClasses(taskClasses), + nga.field('state').cssClasses(taskClasses), + nga.field('runningSince').map(formatMoment).cssClasses(taskClasses), + nga.field('lastRunStarted').map(formatMoment).cssClasses(taskClasses) ]) .listActions( - '' + ' ' + + ' ' + + '' ) ; diff --git a/admin/js/taskActionButton.js b/admin/js/taskActionButton.js index 4e1582a..e93f3cc 100644 --- a/admin/js/taskActionButton.js +++ b/admin/js/taskActionButton.js @@ -5,6 +5,7 @@ angular.module('ffffngAdmin') var link = function (scope) { scope.label = scope.label || 'ACTION'; scope.icon = scope.icon || 'play'; + scope.button = scope.button || 'default'; scope.perform = function () { var task = scope.task(); @@ -28,12 +29,14 @@ angular.module('ffffngAdmin') 'icon': '@', 'task': '&', 'size': '@', - 'label': '@' + 'label': '@', + 'button': '@', + 'disabled': '=' }, 'template': - '' + + '' }; }); diff --git a/server/jobs/scheduler.js b/server/jobs/scheduler.js index 84ce8b6..b5c8843 100644 --- a/server/jobs/scheduler.js +++ b/server/jobs/scheduler.js @@ -35,20 +35,24 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi schedule: expr, job: job, runningSince: false, - lastRunStarted: false + lastRunStarted: false, + state: 'idle', + enabled: true }; task.run = function () { - if (task.runningSince) { + if (task.runningSince || !task.enabled) { // job is still running, skip execution return; } task.runningSince = moment(); task.lastRunStarted = task.runningSince; + task.state = 'running'; job.run(function () { task.runningSince = false; + task.state = 'idle'; }); }; @@ -65,7 +69,6 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi schedule('0 */1 * * * *', 'MailQueueJob'); if (config.client.monitoring.enabled) { - schedule('* * * * * *', 'TestJob'); schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); schedule('45 */5 * * * *', 'MonitoringMailsSendingJob'); schedule('0 0 3 * * *', 'NodeInformationCleanupJob'); // every night at 3:00 diff --git a/server/jobs/testJob.js b/server/jobs/testJob.js deleted file mode 100644 index e14a280..0000000 --- a/server/jobs/testJob.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -angular.module('ffffng').factory('TestJob', function (Logger) { - var i = 1; - - return { - run: function (callback) { - var j = i; - i += 1; - - Logger.tag('test').info('Start test job... ' + j); - setTimeout(function () { - Logger.tag('test').info('Done test job... ' + j); - callback(); - }, 2000); - } - }; -}); diff --git a/server/resources/taskResource.js b/server/resources/taskResource.js index 305121d..10b944a 100644 --- a/server/resources/taskResource.js +++ b/server/resources/taskResource.js @@ -17,10 +17,61 @@ angular.module('ffffng').factory('TaskResource', function ( name: task.name, schedule: task.schedule, runningSince: task.runningSince && task.runningSince.unix(), - lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix() + lastRunStarted: task.lastRunStarted && task.lastRunStarted.unix(), + state: task.state, + enabled: task.enabled }; } + function withValidTaskId(req, res, callback) { + var id = Strings.normalizeString(Resources.getData(req).id); + + if (!isValidId(id)) { + return callback({data: 'Invalid task id.', type: ErrorTypes.badRequest}); + } + + callback(null, id); + } + + function getTask(id, callback) { + var tasks = Scheduler.getTasks(); + var task = tasks[id]; + + if (!task) { + return callback({data: 'Task not found.', type: ErrorTypes.notFound}); + } + + callback(null, task); + } + + function withTask(req, res, callback) { + withValidTaskId(req, res, function (err, id) { + if (err) { + return callback(err); + } + + getTask(id, function (err, task) { + if (err) { + return callback(err); + } + + callback(null, id, task); + }); + }); + } + + function setTaskEnabled(req, res, enable) { + withTask(req, res, function (err, id, task) { + if (err) { + return Resources.error(res, err); + } + + task.enabled = !!enable; // ensure boolean + + return Resources.success(res, toExternalTask(task, id)); + }); + } + return { getAll: function (req, res) { var tasks = Scheduler.getTasks(); @@ -28,26 +79,27 @@ angular.module('ffffng').factory('TaskResource', function ( }, run: function (req, res) { - var id = Strings.normalizeString(Resources.getData(req).id); + withTask(req, res, function (err, id, task) { + if (err) { + return Resources.error(res, err); + } - if (!isValidId(id)) { - return Resources.error(res, {data: 'Invalid task id.', type: ErrorTypes.badRequest}); - } + if (task.runningSince) { + return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict}); + } - var tasks = Scheduler.getTasks(); - var task = tasks[id]; + task.run(); - if (!task) { - return Resources.error(res, {data: 'Task not found.', type: ErrorTypes.notFound}); - } + return Resources.success(res, toExternalTask(task, id)); + }); + }, - if (task.runningSince) { - return Resources.error(res, {data: 'Task already running.', type: ErrorTypes.conflict}); - } + enable: function (req, res) { + setTaskEnabled(req, res, true); + }, - task.run(); - - return Resources.success(res, toExternalTask(task, id)); + disable: function (req, res) { + setTaskEnabled(req, res, false); } }; }); diff --git a/server/router.js b/server/router.js index 5bb4bbf..497f7db 100644 --- a/server/router.js +++ b/server/router.js @@ -18,6 +18,8 @@ angular.module('ffffng').factory('Router', function ( app.get('/internal/api/tasks', TaskResource.getAll); app.put('/internal/api/tasks/run/:id', TaskResource.run); + app.put('/internal/api/tasks/enable/:id', TaskResource.enable); + app.put('/internal/api/tasks/disable/:id', TaskResource.disable); } }; });