Software /
code /
prosody
Annotate
plugins/mod_cron.lua @ 13265:6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
This ensures that all interactions with storage happen inside an async
thread, allowing async waiting to be performed in storage drivers.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 14 Oct 2023 22:32:33 +0200 |
parent | 13264:9b720c38fee8 |
child | 13269:d50bee584969 |
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"); |
74b9e05af71e
plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12489
diff
changeset
|
4 local datetime = require("prosody.util.datetime"); |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
12002
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
12001
diff
changeset
|
6 local periods = { hourly = 3600; daily = 86400; weekly = 7 * 86400 } |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local active_hosts = {} |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 function module.add_host(host_module) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 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
|
13 active_hosts[host_module.host] = true; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
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 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
|
16 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
|
17 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
18 |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
23 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local function task_added(event) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
30 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
|
31 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
|
32 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
33 task.restore = restore_task; |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 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
|
35 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
|
36 return true |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local function task_removed(event) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 local task = event.item; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 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
|
42 return true |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 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
|
46 |
13265
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
47 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
|
48 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
|
49 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
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 local function should_run(when, last) |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
53 return not last or last + periods[when] * 0.995 <= os.time() |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
54 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 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
|
57 task:restore(); |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
58 if not should_run(task.when, task.last) then |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
59 return |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
60 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local started_at = os.time(); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 task:run(started_at); |
12489
8b42575738f0
mod_cron: Fix recording last task run time #1751
Kim Alvefur <zash@zash.se>
parents:
12186
diff
changeset
|
63 task.last = started_at; |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 task:save(started_at); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 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
|
68 scheduled = module:add_timer(1, function() |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 module:log("info", "Running periodic tasks"); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 local delay = 3600; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 for host in pairs(active_hosts) do |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 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
|
73 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
|
74 task_runner:run(task); |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 module:log("debug", "Wait %ds", delay); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 return delay |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end); |