Software /
code /
prosody
Annotate
plugins/mod_cron.lua @ 13371:a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Nov 2023 18:42:56 +0100 |
parent | 13366:9f1f1e7afdbd |
child | 13421:92301fa7a673 |
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 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local active_hosts = {} |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 function module.add_host(host_module) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
9 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
|
10 active_hosts[host_module.host] = true; |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
12 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
|
13 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
14 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
|
15 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
16 local function task_added(event) |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
17 local task = event.item; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 task.restore = restore_task; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
22 task.save = save_task; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
23 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
|
24 return true |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
25 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
27 local function task_removed(event) |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
28 local task = event.item; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
29 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
|
30 return true |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
31 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
33 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
|
34 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
35 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
|
36 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
38 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
|
39 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 local function run_task(task) |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
41 task:restore(); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
42 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
|
43 local started_at = os.time(); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
44 task:run(started_at); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
45 task.last = started_at; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
46 task:save(started_at); |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 local task_runner = async.runner(run_task); |
12009
f6fff0658108
mod_cron: Expose the One Timer via module environment
Kim Alvefur <zash@zash.se>
parents:
12002
diff
changeset
|
50 scheduled = module:add_timer(1, function() |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
51 module:log("info", "Running periodic tasks"); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
52 local delay = 3600; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 end |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
57 module:log("debug", "Wait %ds", delay); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
58 return delay |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end); |
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
60 |
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
61 module:add_item("shell-command", { |
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
62 section = "cron"; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
63 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
|
64 name = "tasks"; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
65 desc = "View registered tasks"; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
66 args = {}; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
67 handler = function(self, filter_host) |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
68 local format_table = require("prosody.util.human.io").table; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
69 local it = require("util.iterators"); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
70 local row = format_table({ |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
71 { title = "Host"; width = "2p" }; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
72 { title = "Task"; width = "3p" }; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
73 { title = "Desc"; width = "3p" }; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
74 { title = "When"; width = "1p" }; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
75 { title = "Last run"; width = "20" }; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
76 }, self.session.width); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
77 local print = self.session.print; |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
78 print(row()); |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 end |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
83 end |
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
84 end; |
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
85 }); |