//= require active_scaffold //= require js-routes function TasksScheduler() { } TasksScheduler.Status = function () { }; // Shortcut var _S = TasksScheduler.Status; _S.initialized = false; _S.init = function (url, interval_max) { if (!_S.initialized) { _S.initialized = true; _S.url = url; _S.interval_max = interval_max; _S.update(); } }; _S.content = function () { return $('#TaskScheduler_Status_Content'); }; _S.status = function () { return $('#TaskScheduler_Status_Status'); }; _S.update_status = function () { _S.status().html( "Updating in " + _S.interval + " seconds..." ); }; _S.check = function () { if (_S.interval <= 0) { _S.update(); } else { _S.interval--; _S.update_status(); setTimeout(_S.check, 1000); } }; _S.update = function () { $.ajax({ url: _S.url, success: function (result) { _S.content().html(result); }, complete: function (result) { _S.interval = _S.interval_max + 1; _S.last_update = new Date(); _S.check(); } }); }; TasksScheduler.Alert = function () { }; _A = TasksScheduler.Alert; _A.DEFAULT_REFRESH_INTERVAL = 5000; _A.DEFAULT_ELEMENT_SELECTOR = '#tasks_scheduler_alert'; _A.CSS_CLASSES_PREFIX = 'alert_'; _A.url = Routes.status_tasks_scheduler_daemon_path(); _A.init = function (options) { options = typeof options !== 'undefined' ? options : {}; $(document).ready(function () { _A.options = options; if (!_A.options.refresh_interval) { _A.options.refresh_interval = _A.DEFAULT_REFRESH_INTERVAL; } if (!_A.options.element_selector) { _A.options.element_selector = _A.DEFAULT_ELEMENT_SELECTOR; } _A.refresh(); }); }; _A.setNextRefresh = function () { setTimeout(_A.refresh, _A.options.refresh_interval); }; _A.refresh = function () { $.ajax({ url: _A.url, success: function (result) { var alert = $(_A.options.element_selector); var pattern = new RegExp('(^|\\s)' + _A.CSS_CLASSES_PREFIX + "\\S+", 'g'); alert.removeClass (function (index, className) { return (className.match (pattern) || []).join(' '); }); alert.addClass(_A.resultToCssClass(result)); }, complete: function (result) { _A.setNextRefresh(); } }); }; _A.resultToCssClass = function(result) { var suffix = "ok" if (!result.daemon_running) { suffix = "daemon_stopped"; } else if (!result.tasks_all_ok) { suffix = "task_failed"; } return _A.CSS_CLASSES_PREFIX + suffix; };