annotate mod_reload_modules/mod_reload_modules.lua @ 5668:ecfd7aece33b

mod_measure_modules: Report module statuses via OpenMetrics Someone in the chat asked about a health check endpoint, which reminded me of mod_http_status, which provides access to module statuses with full details. After that, this idea came about, which seems natural. As noted in the README, it could be used to monitor that critical modules are in fact loaded correctly. As more modules use the status API, the more useful this module and mod_http_status becomes.
author Kim Alvefur <zash@zash.se>
date Fri, 06 Oct 2023 18:34:39 +0200
parents cc14bfec209b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1178
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
1 local array, it, set = require "util.array", require "util.iterators", require "util.set";
313
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local mm = require "core.modulemanager";
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
502
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
4 function reload_all()
1178
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
5 local modules = module:get_option_set("reload_modules", {});
313
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 if not modules then
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 module:log("warn", "No modules listed in the config to reload - set reload_modules to a list");
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 return;
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end
1178
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
10 local configured_modules = module:get_option_inherited_set("modules_enabled", {});
4947
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
11 local component_module = module:get_option_string("component_module");
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
12 if component_module then
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
13 -- Ensure awareness of the component module so that it is not unloaded
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
14 configured_modules:add(component_module);
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
15 end
cc14bfec209b mod_reload_modules: Be aware of component module (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 3882
diff changeset
16
3882
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
17 -- ignore removed hosts
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
18 if not prosody.hosts[module.host] then
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
19 module:log("warn", "Ignoring host %s: host was removed...", module.host);
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
20 return;
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
21 end
1178
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
22 local loaded_modules = set.new(array.collect(it.keys(prosody.hosts[module.host].modules)));
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
23 local need_to_load = set.intersection(configured_modules - loaded_modules, modules);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
24 local need_to_unload = set.intersection(loaded_modules - configured_modules, modules);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
25
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
26 for module_name in need_to_load do
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
27 module:log("debug", "Loading %s", module_name);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
28 mm.load(module.host, module_name);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
29 end
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
30
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
31 for module_name in need_to_unload do
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
32 module:log("debug", "Unloading %s", module_name);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
33 mm.unload(module.host, module_name);
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
34 end
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
35
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
36 modules:exclude(need_to_load+need_to_unload)
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
37
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
38 for module_name in set.intersection(modules,configured_modules) do
412f62d05a23 mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
39 module:log("debug", "Reloading %s", module_name);
1644
fc08d841a309 mod_reload_modules: Fix a bug introduced in commit 412f62d05a23, which caused mod_reload_modules to... not reload modules
Matthew Wild <mwild1@gmail.com>
parents: 1178
diff changeset
40 mm.reload(module.host, module_name);
313
524f22ef2c2b mod_reload_modules: Module to, erm, reload modules, on SIGHUP/config reload
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
3759
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
42
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
43 local global_modules = module:get_option_set("reload_global_modules", {});
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
44 for module_name in global_modules do
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
45 module:log("debug", "Global reload of mod_%s", module_name);
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
46 mm.reload("*", module_name);
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
47 end
502
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
48 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
49
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
50
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
51 if module.hook_global then
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
52 module:hook_global("config-reloaded", reload_all);
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
53 else -- COMPAT w/pre-0.9
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
54 function module.load()
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
55 prosody.events.add_handler("config-reloaded", reload_all);
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
56 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
57 function module.unload()
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
58 prosody.events.remove_handler("config-reloaded", reload_all);
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
59 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
60 end