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