annotate mod_reload_modules/mod_reload_modules.lua @ 4578:d95fcde6e39d

mod_measure_lua: add openmetrics-spirited way to collect lua memory use In the spirit of separate collectors found in other Prometheus packages, this is a separate module to collect metrics related to the lua stack itself.
author Jonas Schäfer <jonas@wielicki.name>
date Sat, 29 May 2021 12:12:17 +0200
parents 3b8f4f3b1718
children cc14bfec209b
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", {});
3882
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
11 -- ignore removed hosts
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
12 if not prosody.hosts[module.host] then
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
13 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
14 return;
3b8f4f3b1718 mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents: 3759
diff changeset
15 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
16 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
17 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
18 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
19
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
20 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
21 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
22 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
23 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
24
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 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
26 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
27 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
28 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
29
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 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
31
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 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
33 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
34 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
35 end
3759
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
36
57eb248f6dd3 mod_reload_modules: Accept a list of global modules to reload
Matthew Wild <mwild1@gmail.com>
parents: 1644
diff changeset
37 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
38 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
39 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
40 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
41 end
502
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
42 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
43
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
44
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
45 if module.hook_global then
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
46 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
47 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
48 function module.load()
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
49 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
50 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
51 function module.unload()
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
52 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
53 end
aad0b2df9e6b mod_reload_modules: Use module:hook_global() if available
Matthew Wild <mwild1@gmail.com>
parents: 313
diff changeset
54 end