comparison mod_reload_modules/mod_reload_modules.lua @ 1178:412f62d05a23

mod_reload_modules: Add support for loading/unloading of modules, as well as simply reloading
author Matthew Wild <mwild1@gmail.com>
date Mon, 02 Sep 2013 21:20:21 +0100
parents aad0b2df9e6b
children fc08d841a309
comparison
equal deleted inserted replaced
1177:a464261deba8 1178:412f62d05a23
1 local array, it, set = require "util.array", require "util.iterators", require "util.set";
1 local mm = require "core.modulemanager"; 2 local mm = require "core.modulemanager";
2 3
3 local function reload_module(name)
4 local ok, err = mm.reload(module.host, name);
5 if ok then
6 module:log("debug", "Reloaded %s", name);
7 else
8 module:log("error", "Failed to reload %s: %s", name, err);
9 end
10 end
11
12 function reload_all() 4 function reload_all()
13 local modules = module:get_option_array("reload_modules"); 5 local modules = module:get_option_set("reload_modules", {});
14 if not modules then 6 if not modules then
15 module:log("warn", "No modules listed in the config to reload - set reload_modules to a list"); 7 module:log("warn", "No modules listed in the config to reload - set reload_modules to a list");
16 return; 8 return;
17 end 9 end
18 for _, module in ipairs(modules) do 10 local configured_modules = module:get_option_inherited_set("modules_enabled", {});
19 reload_module(module); 11 local loaded_modules = set.new(array.collect(it.keys(prosody.hosts[module.host].modules)));
12 local need_to_load = set.intersection(configured_modules - loaded_modules, modules);
13 local need_to_unload = set.intersection(loaded_modules - configured_modules, modules);
14
15 for module_name in need_to_load do
16 module:log("debug", "Loading %s", module_name);
17 mm.load(module.host, module_name);
18 end
19
20 for module_name in need_to_unload do
21 module:log("debug", "Unloading %s", module_name);
22 mm.unload(module.host, module_name);
23 end
24
25 modules:exclude(need_to_load+need_to_unload)
26
27 for module_name in set.intersection(modules,configured_modules) do
28 module:log("debug", "Reloading %s", module_name);
29 mm.reload(module_name);
20 end 30 end
21 end 31 end
22 32
23 33
24 if module.hook_global then 34 if module.hook_global then