# HG changeset patch # User Kim Alvefur # Date 1697993882 -7200 # Node ID ffd3dadf6247b314ef8170262d9702b99f768620 # Parent 1c63b8c3cd9d68c406b03d6f4e7d0752c2df9ee9 mod_cron: Make task frequencies configurable in overly generic manner Requested feature for many modules, notably MAM and file sharing. diff -r 1c63b8c3cd9d -r ffd3dadf6247 CHANGES --- a/CHANGES Sun Oct 22 18:57:28 2023 +0200 +++ b/CHANGES Sun Oct 22 18:58:02 2023 +0200 @@ -53,6 +53,7 @@ - Moved all modules into the Lua namespace `prosody.` - Forwarded header from RFC 7239 supported, disabled by default - mod_http_file_share now uses roles framework, affecting access from e.g. components +- Intervals of mod_cron managed periodic jobs made configurable ## Removed diff -r 1c63b8c3cd9d -r ffd3dadf6247 plugins/mod_cron.lua --- a/plugins/mod_cron.lua Sun Oct 22 18:57:28 2023 +0200 +++ b/plugins/mod_cron.lua Sun Oct 22 18:58:02 2023 +0200 @@ -2,8 +2,6 @@ local async = require("prosody.util.async"); -local periods = { hourly = 3600; daily = 86400; weekly = 7 * 86400 } - local active_hosts = {} function module.add_host(host_module) @@ -29,6 +27,7 @@ if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end + task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53); task.restore = restore_task; task.save = save_task; module:log("debug", "%s task %s added", task.when, task.id); @@ -48,13 +47,13 @@ end end -local function should_run(when, last) - return not last or last + periods[when] * 0.995 <= os.time() +local function should_run(task, last) + return not last or last + task.period * 0.995 <= os.time() end local function run_task(task) task:restore(); - if not should_run(task.when, task.last) then + if not should_run(task, task.last) then return end local started_at = os.time(); diff -r 1c63b8c3cd9d -r ffd3dadf6247 teal-src/prosody/plugins/mod_cron.tl --- a/teal-src/prosody/plugins/mod_cron.tl Sun Oct 22 18:57:28 2023 +0200 +++ b/teal-src/prosody/plugins/mod_cron.tl Sun Oct 22 18:58:02 2023 +0200 @@ -18,6 +18,7 @@ id : string -- unique id name : string -- name or short description when : frequency + period : number last : integer run : function (task_spec, integer) save : function (task_spec, integer) @@ -29,8 +30,6 @@ item : task_spec end -local periods : { frequency : integer } = { hourly = 3600, daily = 86400, weekly = 7*86400 } - local active_hosts : { string : boolean } = { } function module.add_host(host_module : moduleapi) @@ -56,6 +55,7 @@ if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end + task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400*7*53); task.restore = restore_task; task.save = save_task; module:log("debug", "%s task %s added", task.when, task.id); @@ -75,13 +75,13 @@ end end -local function should_run(when : frequency, last : integer) : boolean - return not last or last + periods[when]*0.995 <= os.time(); +local function should_run(task : task_spec, last : integer) : boolean + return not last or last + task.period * 0.995 <= os.time(); end local function run_task(task : task_spec) task:restore(); - if not should_run(task.when, task.last) then + if not should_run(task, task.last) then return; end local started_at = os.time();