Software /
code /
prosody
Annotate
plugins/mod_cron.lua @ 13366:9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 30 Nov 2023 12:41:26 +0000 |
parent | 13365:fda192db8f18 |
child | 13371:a22e3a980178 |
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 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
9 local last_run_times = host_module:open_store("cron", "map"); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
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 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
12 local function save_task(task, started_at) |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
13 last_run_times:set(nil, task.id, started_at); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
14 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
|
15 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
16 local function restore_task(task) |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
17 if task.last == nil then |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
18 task.last = last_run_times:get(nil, task.id); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
19 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
20 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
22 local function task_added(event) |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
23 local task = event.item; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
24 if task.name == nil then task.name = task.when; end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
25 if task.id == nil then |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
26 task.id = event.source.name .. "/" .. |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
27 task.name:gsub("%W", "_"):lower(); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
28 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
29 task.period = host_module:get_option_period( |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
30 task.id:gsub("/", "_") .. "_period", "1" .. task.when, |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
31 60, 86400 * 7 * 53); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
32 task.restore = restore_task; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
33 task.save = save_task; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
34 module:log("debug", "%s task %s added", task.when, task.id); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
35 return true |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
36 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
38 local function task_removed(event) |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
39 local task = event.item; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
40 host_module:log("debug", "Task %s removed", task.id); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
41 return true |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
42 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
44 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
|
45 |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
46 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
|
47 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
13284
ffd3dadf6247
mod_cron: Make task frequencies configurable in overly generic manner
Kim Alvefur <zash@zash.se>
parents:
13269
diff
changeset
|
49 local function should_run(task, last) |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
50 return not last or last + task.period * 0.995 <= os.time() |
13265
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
51 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 local function run_task(task) |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
54 task:restore(); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
55 if not should_run(task, task.last) then return end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
56 local started_at = os.time(); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
57 task:run(started_at); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
58 task.last = started_at; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
59 task:save(started_at); |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 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
|
63 scheduled = module:add_timer(1, function() |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
64 module:log("info", "Running periodic tasks"); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
65 local delay = 3600; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
66 for host in pairs(active_hosts) do |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
67 module:log("debug", "Running periodic tasks for host %s", host); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
68 for _, task in ipairs(module:context(host):get_host_items("task")) do |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
69 task_runner:run(task); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
70 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
71 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
72 module:log("debug", "Wait %ds", delay); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
73 return delay |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 end); |
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
75 |
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
76 module:add_item("shell-command", { |
13366
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
77 section = "cron", |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
78 section_desc = "View and manage recurring tasks", |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
79 name = "tasks", |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
80 desc = "View registered tasks", |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
81 args = {}, |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
82 handler = function(self, filter_host) |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
83 local format_table = require("prosody.util.human.io").table; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
84 local it = require("util.iterators"); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
85 local row = format_table({ |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
86 {title = "Host", width = "2p"}, {title = "Task", width = "3p"}, |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
87 {title = "Desc", width = "3p"}, {title = "When", width = "1p"}, |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
88 {title = "Last run", width = "20"} |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
89 }, self.session.width); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
90 local print = self.session.print; |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
91 print(row()); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
92 for host in it.sorted_pairs(filter_host and {[filter_host] = true} or |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
93 active_hosts) do |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
94 for _, task in ipairs(module:context(host):get_host_items("task")) do |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
95 print(row({ |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
96 host, task.id, task.name, task.when, |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
97 task.last and os.date("%Y-%m-%d %R:%S", task.last) or |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
98 "never" |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
99 })); |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
100 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
101 end |
9f1f1e7afdbd
mod_cron: Update Teal source and rebuild
Matthew Wild <mwild1@gmail.com>
parents:
13365
diff
changeset
|
102 end |
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
103 }); |