Annotate

plugins/mod_cron.lua @ 13742:47e537e340c4 default tip

Merge 13.0->trunk
author Matthew Wild <mwild1@gmail.com>
date Mon, 17 Feb 2025 23:06:26 +0000
parent 13701:1aa7efabeacb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 module:set_global();
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12489
diff changeset
3 local async = require("prosody.util.async");
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
13421
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
5 local cron_initial_delay = module:get_option_number("cron_initial_delay", 1);
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
6 local cron_check_delay = module:get_option_number("cron_check_delay", 3600);
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
7 local cron_spread_factor = module:get_option_number("cron_spread_factor", 0);
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
8
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local active_hosts = {}
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
13588
899453f11f50 mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 13443
diff changeset
11 if prosody.process_type == "prosodyctl" then
899453f11f50 mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 13443
diff changeset
12 return; -- Yes, it happens...
899453f11f50 mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 13443
diff changeset
13 end
899453f11f50 mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents: 13443
diff changeset
14
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 function module.add_host(host_module)
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
17 local last_run_times = host_module:open_store("cron", "map");
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
18 active_hosts[host_module.host] = true;
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
20 local function save_task(task, started_at) last_run_times:set(nil, task.id, started_at); end
13265
6ac5ad578565 mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents: 13264
diff changeset
21
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
22 local function restore_task(task) if task.last == nil then task.last = last_run_times:get(nil, task.id); end end
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
24 local function task_added(event)
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
25 local task = event.item;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
26 if task.name == nil then task.name = task.when; end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
27 if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
28 task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
29 task.restore = restore_task;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
30 task.save = save_task;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
31 module:log("debug", "%s task %s added", task.when, task.id);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
32 return true
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
33 end
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
35 local function task_removed(event)
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
36 local task = event.item;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
37 host_module:log("debug", "Task %s removed", task.id);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
38 return true
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
39 end
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
41 host_module:handle_items("task", task_added, task_removed, true);
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
43 function host_module.unload() active_hosts[host_module.host] = nil; end
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
46 local function should_run(task, last) return not last or last + task.period * 0.995 <= os.time() end
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local function run_task(task)
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
49 task:restore();
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
50 if not should_run(task, task.last) then return end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
51 local started_at = os.time();
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
52 task:run(started_at);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
53 task.last = started_at;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
54 task:save(started_at);
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
13421
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
57 local function spread(t, factor)
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
58 return t * (1 - factor + 2*factor*math.random());
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
59 end
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
60
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local task_runner = async.runner(run_task);
13421
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
62 scheduled = module:add_timer(cron_initial_delay, function()
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
63 module:log("info", "Running periodic tasks");
13421
92301fa7a673 mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents: 13371
diff changeset
64 local delay = spread(cron_check_delay, cron_spread_factor);
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
65 for host in pairs(active_hosts) do
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
66 module:log("debug", "Running periodic tasks for host %s", host);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
67 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
68 end
13443
98a6ec4ce140 mod_cron: Fix log format to account for float that was integer before
Kim Alvefur <zash@zash.se>
parents: 13421
diff changeset
69 module:log("debug", "Wait %gs", delay);
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
70 return delay
11986
3d5135e8a2a7 mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end);
13364
6f9b15757384 mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents: 13284
diff changeset
72
6f9b15757384 mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents: 13284
diff changeset
73 module:add_item("shell-command", {
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
74 section = "cron";
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
75 section_desc = "View and manage recurring tasks";
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
76 name = "tasks";
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
77 desc = "View registered tasks";
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
78 args = {};
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
79 handler = function(self, filter_host)
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
80 local format_table = require("prosody.util.human.io").table;
13701
1aa7efabeacb mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents: 13588
diff changeset
81 local it = require("prosody.util.iterators");
13371
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
82 local row = format_table({
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
83 { title = "Host"; width = "2p" };
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
84 { title = "Task"; width = "3p" };
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
85 { title = "Desc"; width = "3p" };
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
86 { title = "When"; width = "1p" };
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
87 { title = "Last run"; width = "20" };
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
88 }, self.session.width);
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
89 local print = self.session.print;
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
90 print(row());
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
91 for host in it.sorted_pairs(filter_host and { [filter_host] = true } or active_hosts) do
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
92 for _, task in ipairs(module:context(host):get_host_items("task")) do
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
93 print(row({ host; task.id; task.name; task.when; task.last and os.date("%Y-%m-%d %R:%S", task.last) or "never" }));
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
94 end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
95 end
a22e3a980178 mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents: 13366
diff changeset
96 end;
13364
6f9b15757384 mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents: 13284
diff changeset
97 });