Mercurial > prosody-hg
comparison plugins/mod_cron.lua @ 13371:a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 30 Nov 2023 18:42:56 +0100 |
| parents | 9f1f1e7afdbd |
| children | 92301fa7a673 |
comparison
equal
deleted
inserted
replaced
| 13370:971fd4cb09d4 | 13371:a22e3a980178 |
|---|---|
| 4 | 4 |
| 5 local active_hosts = {} | 5 local active_hosts = {} |
| 6 | 6 |
| 7 function module.add_host(host_module) | 7 function module.add_host(host_module) |
| 8 | 8 |
| 9 local last_run_times = host_module:open_store("cron", "map"); | 9 local last_run_times = host_module:open_store("cron", "map"); |
| 10 active_hosts[host_module.host] = true; | 10 active_hosts[host_module.host] = true; |
| 11 | 11 |
| 12 local function save_task(task, started_at) | 12 local function save_task(task, started_at) last_run_times:set(nil, task.id, started_at); end |
| 13 last_run_times:set(nil, task.id, started_at); | |
| 14 end | |
| 15 | 13 |
| 16 local function restore_task(task) | 14 local function restore_task(task) if task.last == nil then task.last = last_run_times:get(nil, task.id); end end |
| 17 if task.last == nil then | |
| 18 task.last = last_run_times:get(nil, task.id); | |
| 19 end | |
| 20 end | |
| 21 | 15 |
| 22 local function task_added(event) | 16 local function task_added(event) |
| 23 local task = event.item; | 17 local task = event.item; |
| 24 if task.name == nil then task.name = task.when; end | 18 if task.name == nil then task.name = task.when; end |
| 25 if task.id == nil then | 19 if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end |
| 26 task.id = event.source.name .. "/" .. | 20 task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53); |
| 27 task.name:gsub("%W", "_"):lower(); | 21 task.restore = restore_task; |
| 28 end | 22 task.save = save_task; |
| 29 task.period = host_module:get_option_period( | 23 module:log("debug", "%s task %s added", task.when, task.id); |
| 30 task.id:gsub("/", "_") .. "_period", "1" .. task.when, | 24 return true |
| 31 60, 86400 * 7 * 53); | 25 end |
| 32 task.restore = restore_task; | |
| 33 task.save = save_task; | |
| 34 module:log("debug", "%s task %s added", task.when, task.id); | |
| 35 return true | |
| 36 end | |
| 37 | 26 |
| 38 local function task_removed(event) | 27 local function task_removed(event) |
| 39 local task = event.item; | 28 local task = event.item; |
| 40 host_module:log("debug", "Task %s removed", task.id); | 29 host_module:log("debug", "Task %s removed", task.id); |
| 41 return true | 30 return true |
| 42 end | 31 end |
| 43 | 32 |
| 44 host_module:handle_items("task", task_added, task_removed, true); | 33 host_module:handle_items("task", task_added, task_removed, true); |
| 45 | 34 |
| 46 function host_module.unload() active_hosts[host_module.host] = nil; end | 35 function host_module.unload() active_hosts[host_module.host] = nil; end |
| 47 end | 36 end |
| 48 | 37 |
| 49 local function should_run(task, last) | 38 local function should_run(task, last) return not last or last + task.period * 0.995 <= os.time() end |
| 50 return not last or last + task.period * 0.995 <= os.time() | |
| 51 end | |
| 52 | 39 |
| 53 local function run_task(task) | 40 local function run_task(task) |
| 54 task:restore(); | 41 task:restore(); |
| 55 if not should_run(task, task.last) then return end | 42 if not should_run(task, task.last) then return end |
| 56 local started_at = os.time(); | 43 local started_at = os.time(); |
| 57 task:run(started_at); | 44 task:run(started_at); |
| 58 task.last = started_at; | 45 task.last = started_at; |
| 59 task:save(started_at); | 46 task:save(started_at); |
| 60 end | 47 end |
| 61 | 48 |
| 62 local task_runner = async.runner(run_task); | 49 local task_runner = async.runner(run_task); |
| 63 scheduled = module:add_timer(1, function() | 50 scheduled = module:add_timer(1, function() |
| 64 module:log("info", "Running periodic tasks"); | 51 module:log("info", "Running periodic tasks"); |
| 65 local delay = 3600; | 52 local delay = 3600; |
| 66 for host in pairs(active_hosts) do | 53 for host in pairs(active_hosts) do |
| 67 module:log("debug", "Running periodic tasks for host %s", host); | 54 module:log("debug", "Running periodic tasks for host %s", host); |
| 68 for _, task in ipairs(module:context(host):get_host_items("task")) do | 55 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end |
| 69 task_runner:run(task); | 56 end |
| 70 end | 57 module:log("debug", "Wait %ds", delay); |
| 71 end | 58 return delay |
| 72 module:log("debug", "Wait %ds", delay); | |
| 73 return delay | |
| 74 end); | 59 end); |
| 75 | 60 |
| 76 module:add_item("shell-command", { | 61 module:add_item("shell-command", { |
| 77 section = "cron", | 62 section = "cron"; |
| 78 section_desc = "View and manage recurring tasks", | 63 section_desc = "View and manage recurring tasks"; |
| 79 name = "tasks", | 64 name = "tasks"; |
| 80 desc = "View registered tasks", | 65 desc = "View registered tasks"; |
| 81 args = {}, | 66 args = {}; |
| 82 handler = function(self, filter_host) | 67 handler = function(self, filter_host) |
| 83 local format_table = require("prosody.util.human.io").table; | 68 local format_table = require("prosody.util.human.io").table; |
| 84 local it = require("util.iterators"); | 69 local it = require("util.iterators"); |
| 85 local row = format_table({ | 70 local row = format_table({ |
| 86 {title = "Host", width = "2p"}, {title = "Task", width = "3p"}, | 71 { title = "Host"; width = "2p" }; |
| 87 {title = "Desc", width = "3p"}, {title = "When", width = "1p"}, | 72 { title = "Task"; width = "3p" }; |
| 88 {title = "Last run", width = "20"} | 73 { title = "Desc"; width = "3p" }; |
| 89 }, self.session.width); | 74 { title = "When"; width = "1p" }; |
| 90 local print = self.session.print; | 75 { title = "Last run"; width = "20" }; |
| 91 print(row()); | 76 }, self.session.width); |
| 92 for host in it.sorted_pairs(filter_host and {[filter_host] = true} or | 77 local print = self.session.print; |
| 93 active_hosts) do | 78 print(row()); |
| 94 for _, task in ipairs(module:context(host):get_host_items("task")) do | 79 for host in it.sorted_pairs(filter_host and { [filter_host] = true } or active_hosts) do |
| 95 print(row({ | 80 for _, task in ipairs(module:context(host):get_host_items("task")) do |
| 96 host, task.id, task.name, task.when, | 81 print(row({ host; task.id; task.name; task.when; task.last and os.date("%Y-%m-%d %R:%S", task.last) or "never" })); |
| 97 task.last and os.date("%Y-%m-%d %R:%S", task.last) or | 82 end |
| 98 "never" | 83 end |
| 99 })); | 84 end; |
| 100 end | |
| 101 end | |
| 102 end | |
| 103 }); | 85 }); |
