Mercurial > prosody-hg
comparison plugins/mod_cron.lua @ 13884:8b05340a94d3
mod_cron: Move changes to Teal source
Yes, it's annoying to have generated output in version control, but not
sure yet if we want to keep Teal. It seems to do breaking syntax changes
somewhat often.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 09 May 2025 00:53:56 +0200 |
| parents | 1aa7efabeacb |
| children |
comparison
equal
deleted
inserted
replaced
| 13883:b713da7527d2 | 13884:8b05340a94d3 |
|---|---|
| 1 -- This file is generated from teal-src/prosody/plugins/mod_cron.tl | |
| 1 module:set_global(); | 2 module:set_global(); |
| 2 | 3 |
| 3 local async = require("prosody.util.async"); | 4 local async = require("prosody.util.async"); |
| 4 | 5 |
| 5 local cron_initial_delay = module:get_option_number("cron_initial_delay", 1); | 6 local cron_initial_delay = module:get_option_number("cron_initial_delay", 1); |
| 7 local cron_spread_factor = module:get_option_number("cron_spread_factor", 0); | 8 local cron_spread_factor = module:get_option_number("cron_spread_factor", 0); |
| 8 | 9 |
| 9 local active_hosts = {} | 10 local active_hosts = {} |
| 10 | 11 |
| 11 if prosody.process_type == "prosodyctl" then | 12 if prosody.process_type == "prosodyctl" then |
| 12 return; -- Yes, it happens... | 13 return |
| 13 end | 14 end |
| 14 | 15 |
| 15 function module.add_host(host_module) | 16 function module.add_host(host_module) |
| 16 | 17 |
| 17 local last_run_times = host_module:open_store("cron", "map"); | 18 local last_run_times = host_module:open_store("cron", "map"); |
| 18 active_hosts[host_module.host] = true; | 19 active_hosts[host_module.host] = true; |
| 19 | 20 |
| 20 local function save_task(task, started_at) last_run_times:set(nil, task.id, started_at); end | 21 local function save_task(task, started_at) |
| 22 last_run_times:set(nil, task.id, started_at); | |
| 23 end | |
| 21 | 24 |
| 22 local function restore_task(task) if task.last == nil then task.last = last_run_times:get(nil, task.id); end end | 25 local function restore_task(task) |
| 26 if task.last == nil then | |
| 27 task.last = last_run_times:get(nil, task.id); | |
| 28 end | |
| 29 end | |
| 23 | 30 |
| 24 local function task_added(event) | 31 local function task_added(event) |
| 25 local task = event.item; | 32 local task = event.item; |
| 26 if task.name == nil then task.name = task.when; end | 33 if task.name == nil then |
| 27 if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end | 34 task.name = task.when; |
| 35 end | |
| 36 if task.id == nil then | |
| 37 task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); | |
| 38 end | |
| 28 task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53); | 39 task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53); |
| 29 task.restore = restore_task; | 40 task.restore = restore_task; |
| 30 task.save = save_task; | 41 task.save = save_task; |
| 31 module:log("debug", "%s task %s added", task.when, task.id); | 42 module:log("debug", "%s task %s added", task.when, task.id); |
| 32 return true | 43 return true |
| 38 return true | 49 return true |
| 39 end | 50 end |
| 40 | 51 |
| 41 host_module:handle_items("task", task_added, task_removed, true); | 52 host_module:handle_items("task", task_added, task_removed, true); |
| 42 | 53 |
| 43 function host_module.unload() active_hosts[host_module.host] = nil; end | 54 function host_module.unload() |
| 55 active_hosts[host_module.host] = nil; | |
| 56 end | |
| 44 end | 57 end |
| 45 | 58 |
| 46 local function should_run(task, last) return not last or last + task.period * 0.995 <= os.time() end | 59 local function should_run(task, last) |
| 60 return not last or last + task.period * 0.995 <= os.time() | |
| 61 end | |
| 47 | 62 |
| 48 local function run_task(task) | 63 local function run_task(task) |
| 49 task:restore(); | 64 task:restore(); |
| 50 if not should_run(task, task.last) then return end | 65 if not should_run(task, task.last) then |
| 66 return | |
| 67 end | |
| 51 local started_at = os.time(); | 68 local started_at = os.time(); |
| 52 task:run(started_at); | 69 task:run(started_at); |
| 53 task.last = started_at; | 70 task.last = started_at; |
| 54 task:save(started_at); | 71 task:save(started_at); |
| 55 end | 72 end |
| 56 | 73 |
| 57 local function spread(t, factor) | 74 local function spread(t, factor) |
| 58 return t * (1 - factor + 2*factor*math.random()); | 75 return t * (1 - factor + 2 * factor * math.random()) |
| 59 end | 76 end |
| 60 | 77 |
| 61 local task_runner = async.runner(run_task); | 78 local task_runner = async.runner(run_task); |
| 62 scheduled = module:add_timer(cron_initial_delay, function() | 79 scheduled = module:add_timer(cron_initial_delay, function() |
| 63 module:log("info", "Running periodic tasks"); | 80 module:log("info", "Running periodic tasks"); |
| 64 local delay = spread(cron_check_delay, cron_spread_factor); | 81 local delay = spread(cron_check_delay, cron_spread_factor); |
| 65 for host in pairs(active_hosts) do | 82 for host in pairs(active_hosts) do |
| 66 module:log("debug", "Running periodic tasks for host %s", host); | 83 module:log("debug", "Running periodic tasks for host %s", host); |
| 67 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end | 84 for _, task in ipairs(module:context(host):get_host_items("task")) do |
| 85 task_runner:run(task); | |
| 86 end | |
| 68 end | 87 end |
| 69 module:log("debug", "Wait %gs", delay); | 88 module:log("debug", "Wait %gs", delay); |
| 70 return delay | 89 return delay |
| 71 end); | 90 end); |
| 72 | 91 |
