Software /
code /
prosody
Comparison
teal-src/plugins/mod_cron.tl @ 11986:3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
A number of modules now have periodic tasks that need to run, e.g. for
cleaning out old messages or files. This has highlighted a need for
coordinating and optimizing scheduling of such tasks.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 21 Nov 2021 15:50:36 +0100 |
child | 11995:bbd3ac65640d |
comparison
equal
deleted
inserted
replaced
11985:3740cf7a66a3 | 11986:3d5135e8a2a7 |
---|---|
1 module:set_global(); | |
2 | |
3 local async = require "util.async"; | |
4 | |
5 local record map_store<K,V> | |
6 -- TODO move to somewhere sensible | |
7 get : function (map_store<K,V>, string, K) : V | |
8 set : function (map_store<K,V>, string, K, V) | |
9 end | |
10 | |
11 local enum frequency | |
12 "hourly" | |
13 "daily" | |
14 end | |
15 | |
16 local record task_spec | |
17 id : string -- unique id | |
18 name : string -- name or short description | |
19 when : frequency | |
20 last : integer | |
21 run : function (task_spec, integer) | |
22 save : function (task_spec, integer) | |
23 end | |
24 | |
25 local record task_event | |
26 source : module | |
27 item : task_spec | |
28 end | |
29 | |
30 local periods : { frequency : integer } = { hourly = 3600, daily = 86400 } | |
31 | |
32 local active_hosts : { string : boolean } = { } | |
33 | |
34 function module.add_host(host_module : moduleapi) | |
35 | |
36 local last_run_times = host_module:open_store("cron", "map") as map_store<string,integer>; | |
37 active_hosts[host_module.host] = true; | |
38 | |
39 local function save_task(task : task_spec, started_at : integer) | |
40 last_run_times:set(nil, task.id, started_at); | |
41 end | |
42 | |
43 local function task_added(event : task_event) : boolean | |
44 local task = event.item; | |
45 if task.name == nil then | |
46 task.name = task.when; | |
47 end | |
48 if task.id == nil then | |
49 task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); | |
50 end | |
51 if task.last == nil then | |
52 task.last = last_run_times:get(nil, task.id); | |
53 end | |
54 task.save = save_task; | |
55 module:log("debug", "%s task %s added, last run %s", task.when, task.id, | |
56 task.last and require"util.datetime".datetime(task.last) or "never"); | |
57 return true; | |
58 end | |
59 | |
60 local function task_removed(event : task_event) : boolean | |
61 local task = event.item; | |
62 host_module:log("debug", "Task %s removed", task.id); | |
63 return true; | |
64 end | |
65 | |
66 host_module:handle_items("task", task_added, task_removed, true); | |
67 | |
68 function host_module.unload() | |
69 active_hosts[host_module.host]=nil; | |
70 end | |
71 end | |
72 | |
73 local function should_run(when : frequency, last : integer) : boolean | |
74 return not last or last + periods[when] <= os.time(); | |
75 end | |
76 | |
77 local function run_task(task : task_spec) | |
78 local started_at = os.time(); | |
79 task:run(started_at); | |
80 task:save(started_at); | |
81 end | |
82 | |
83 local task_runner = async.runner(run_task); | |
84 module:add_timer(1, function() : integer | |
85 module:log("info", "Running periodic tasks"); | |
86 local delay = 3600; | |
87 for host in pairs(active_hosts) do | |
88 module:log("debug", "Running periodic tasks for host %s", host); | |
89 for _, task in ipairs(module:context(host):get_host_items("task") as { task_spec } ) do | |
90 module:log("debug", "Considering %s task %s (%s)", task.when, task.id, task.run); | |
91 if should_run(task.when, task.last) then task_runner:run(task); end | |
92 end | |
93 end | |
94 module:log("debug", "Wait %ds", delay); | |
95 return delay; | |
96 end); | |
97 | |
98 -- TODO measure load, pick a good time to do stuff |