changeset 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 a464261deba8
children 27b4e01ddbc4
files mod_reload_modules/mod_reload_modules.lua
diffstat 1 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mod_reload_modules/mod_reload_modules.lua	Thu Aug 29 12:20:20 2013 +0100
+++ b/mod_reload_modules/mod_reload_modules.lua	Mon Sep 02 21:20:21 2013 +0100
@@ -1,22 +1,32 @@
+local array, it, set = require "util.array", require "util.iterators", require "util.set";
 local mm = require "core.modulemanager";
 
-local function reload_module(name)
-	local ok, err = mm.reload(module.host, name);
-	if ok then
-		module:log("debug", "Reloaded %s", name);
-	else
-		module:log("error", "Failed to reload %s: %s", name, err);
-	end
-end
-
 function reload_all()
-	local modules = module:get_option_array("reload_modules");
+	local modules = module:get_option_set("reload_modules", {});
 	if not modules then
 		module:log("warn", "No modules listed in the config to reload - set reload_modules to a list");
 		return;
 	end
-	for _, module in ipairs(modules) do
-		reload_module(module);
+	local configured_modules = module:get_option_inherited_set("modules_enabled", {});
+	local loaded_modules = set.new(array.collect(it.keys(prosody.hosts[module.host].modules)));
+	local need_to_load = set.intersection(configured_modules - loaded_modules, modules);
+	local need_to_unload = set.intersection(loaded_modules - configured_modules, modules);
+
+	for module_name in need_to_load do
+		module:log("debug", "Loading %s", module_name);
+		mm.load(module.host, module_name);
+	end
+
+	for module_name in need_to_unload do
+		module:log("debug", "Unloading %s", module_name);
+		mm.unload(module.host, module_name);
+	end
+
+	modules:exclude(need_to_load+need_to_unload)
+
+	for module_name in set.intersection(modules,configured_modules) do
+		module:log("debug", "Reloading %s", module_name);
+		mm.reload(module_name);
 	end
 end