Software /
code /
prosody
Annotate
plugins/mod_cron.lua @ 13270:14bbfb2cc8dd default tip
lint: Teach luacheck about module:once
Silence warning for using this introduced in 9c62ffbdf2ae
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 15 Oct 2023 16:43:14 +0200 |
parent | 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"); |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
12002
cbed7d8d8f35
mod_cron: Add a 'weekly' job frequency
Kim Alvefur <zash@zash.se>
parents:
12001
diff
changeset
|
5 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
|
6 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local active_hosts = {} |
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 function module.add_host(host_module) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 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
|
12 active_hosts[host_module.host] = true; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
13265
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
14 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
|
15 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
|
16 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
17 |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 end |
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 |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local function task_added(event) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
29 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
|
30 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
|
31 end |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
32 task.restore = restore_task; |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 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
|
34 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
|
35 return true |
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 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 local function task_removed(event) |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local task = event.item; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 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
|
41 return true |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 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
|
45 |
13265
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
46 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
|
47 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
|
48 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 |
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 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
|
52 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
|
53 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 return |
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
59 end |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 local started_at = os.time(); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 task:run(started_at); |
12489
8b42575738f0
mod_cron: Fix recording last task run time #1751
Kim Alvefur <zash@zash.se>
parents:
12186
diff
changeset
|
62 task.last = started_at; |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 task:save(started_at); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 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
|
67 scheduled = module:add_timer(1, function() |
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 module:log("info", "Running periodic tasks"); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local delay = 3600; |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 for host in pairs(active_hosts) do |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 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
|
72 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
|
73 task_runner:run(task); |
11986
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 end |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 module:log("debug", "Wait %ds", delay); |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 return delay |
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end); |