Software / code / prosody
Comparison
plugins/mod_cron.lua @ 13421:92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Notably, it is now possible to add a randomized spread factor to the check
interval.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 20 Feb 2024 17:31:17 +0000 |
| parent | 13371:a22e3a980178 |
| child | 13443:98a6ec4ce140 |
comparison
equal
deleted
inserted
replaced
| 13420:7dc7e2e15b2a | 13421:92301fa7a673 |
|---|---|
| 1 module:set_global(); | 1 module:set_global(); |
| 2 | 2 |
| 3 local async = require("prosody.util.async"); | 3 local async = require("prosody.util.async"); |
| 4 | |
| 5 local cron_initial_delay = module:get_option_number("cron_initial_delay", 1); | |
| 6 local cron_check_delay = module:get_option_number("cron_check_delay", 3600); | |
| 7 local cron_spread_factor = module:get_option_number("cron_spread_factor", 0); | |
| 4 | 8 |
| 5 local active_hosts = {} | 9 local active_hosts = {} |
| 6 | 10 |
| 7 function module.add_host(host_module) | 11 function module.add_host(host_module) |
| 8 | 12 |
| 44 task:run(started_at); | 48 task:run(started_at); |
| 45 task.last = started_at; | 49 task.last = started_at; |
| 46 task:save(started_at); | 50 task:save(started_at); |
| 47 end | 51 end |
| 48 | 52 |
| 53 local function spread(t, factor) | |
| 54 return t * (1 - factor + 2*factor*math.random()); | |
| 55 end | |
| 56 | |
| 49 local task_runner = async.runner(run_task); | 57 local task_runner = async.runner(run_task); |
| 50 scheduled = module:add_timer(1, function() | 58 scheduled = module:add_timer(cron_initial_delay, function() |
| 51 module:log("info", "Running periodic tasks"); | 59 module:log("info", "Running periodic tasks"); |
| 52 local delay = 3600; | 60 local delay = spread(cron_check_delay, cron_spread_factor); |
| 53 for host in pairs(active_hosts) do | 61 for host in pairs(active_hosts) do |
| 54 module:log("debug", "Running periodic tasks for host %s", host); | 62 module:log("debug", "Running periodic tasks for host %s", host); |
| 55 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end | 63 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end |
| 56 end | 64 end |
| 57 module:log("debug", "Wait %ds", delay); | 65 module:log("debug", "Wait %ds", delay); |