Annotate

plugins/mod_cron.lua @ 13364:6f9b15757384

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