Software / code / prosody
Comparison
core/modulemanager.lua @ 5192:3fc3a3072cc2
modulemanager: Set module.reloading when a module is reloading, and when loading make the saved state available in module.saved_state (if any)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 22 Nov 2012 20:59:20 +0000 |
| parent | 5123:7c5c86fa552e |
| child | 5377:898454038524 |
comparison
equal
deleted
inserted
replaced
| 5190:76c73bd3d483 | 5192:3fc3a3072cc2 |
|---|---|
| 109 mod.module.loaded = false; | 109 mod.module.loaded = false; |
| 110 modulemap[host][name] = nil; | 110 modulemap[host][name] = nil; |
| 111 return true; | 111 return true; |
| 112 end | 112 end |
| 113 | 113 |
| 114 local function do_load_module(host, module_name) | 114 local function do_load_module(host, module_name, state) |
| 115 if not (host and module_name) then | 115 if not (host and module_name) then |
| 116 return nil, "insufficient-parameters"; | 116 return nil, "insufficient-parameters"; |
| 117 elseif not hosts[host] and host ~= "*"then | 117 elseif not hosts[host] and host ~= "*"then |
| 118 return nil, "unknown-host"; | 118 return nil, "unknown-host"; |
| 119 end | 119 end |
| 150 | 150 |
| 151 | 151 |
| 152 | 152 |
| 153 local _log = logger.init(host..":"..module_name); | 153 local _log = logger.init(host..":"..module_name); |
| 154 local api_instance = setmetatable({ name = module_name, host = host, | 154 local api_instance = setmetatable({ name = module_name, host = host, |
| 155 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() } | 155 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable(), |
| 156 reloading = not not state, saved_state = state~=true and state or nil } | |
| 156 , { __index = api }); | 157 , { __index = api }); |
| 157 | 158 |
| 158 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); | 159 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
| 159 api_instance.environment = pluginenv; | 160 api_instance.environment = pluginenv; |
| 160 | 161 |
| 174 ok, err = call_module_method(pluginenv, "load"); | 175 ok, err = call_module_method(pluginenv, "load"); |
| 175 if not ok then | 176 if not ok then |
| 176 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); | 177 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); |
| 177 end | 178 end |
| 178 end | 179 end |
| 180 api_instance.reloading, api_instance.saved_state = nil, nil; | |
| 179 | 181 |
| 180 if api_instance.host == "*" then | 182 if api_instance.host == "*" then |
| 181 if not api_instance.global then -- COMPAT w/pre-0.9 | 183 if not api_instance.global then -- COMPAT w/pre-0.9 |
| 182 if host ~= "*" then | 184 if host ~= "*" then |
| 183 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name); | 185 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name); |
| 223 log("warn", "Continuing with reload (using the force)"); | 225 log("warn", "Continuing with reload (using the force)"); |
| 224 end | 226 end |
| 225 end | 227 end |
| 226 end | 228 end |
| 227 | 229 |
| 230 mod.module.reloading = true; | |
| 228 do_unload_module(host, name); | 231 do_unload_module(host, name); |
| 229 local ok, err = do_load_module(host, name); | 232 local ok, err = do_load_module(host, name, saved or true); |
| 230 if ok then | 233 if ok then |
| 231 mod = get_module(host, name); | 234 mod = get_module(host, name); |
| 232 if module_has_method(mod, "restore") then | 235 if module_has_method(mod, "restore") then |
| 233 local ok, err = call_module_method(mod, "restore", saved or {}) | 236 local ok, err = call_module_method(mod, "restore", saved or {}) |
| 234 if (not ok) and err then | 237 if (not ok) and err then |