Comparison

mod_cleanup_http/mod_cleanup_http.lua @ 610:50dc20e96a78

mod_cleanup_http: "spring cleans" module, aka unload global http modules without hassle (open ports cleanup doesn't work with server_event)
author Marco Cirillo <maranda@lightwitch.org>
date Sun, 12 Feb 2012 18:04:51 +0000
child 611:d87a9e1e6d30
comparison
equal deleted inserted replaced
609:edb4afb6a227 610:50dc20e96a78
1 -- Auto-cleanup BOSH stuff module is added globally.
2
3 module:set_global()
4
5 local http_modules = module:get_option("cleanup_http_modules", {})
6 if type(http_modules) ~= "table" then module:log("error", "cleanup_http_modules needs to be a module.") ; return false end
7
8 local function cleanup(data)
9 if data.module == "cleanup_http" then -- it's us getting unloaded destroy handler.
10 prosody.events.remove_handler("module-unloaded", cleanup)
11 elseif http_modules[data.module] then
12 local ports = http_modules[data.module]
13
14 module:log("debug", "Cleaning up http handlers and ports as module %s is being unloaded.", data.module)
15 for _, options in ipairs(ports) do
16 if options.port then
17 httpserver.new.http_servers[options.port].handlers[options.path or "register_account"] = nil
18 end
19 end
20
21 -- if there are no handlers left clean and close the socket, doesn't work with server_event
22 local event = require "core.configmanager".get("*", "core", "use_libevent")
23
24 if not event then
25 for _, options in ipairs(ports) do
26 if options.port and not next(httpserver.new.http_servers[options.port].handlers) then
27 httpserver.new.http_servers[options.port] = nil
28 if options.interface then
29 for _, value in ipairs(options.interface) do
30 if server.getserver(value, options.port) then server.removeserver(value, options.port) end
31 end
32 else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end
33 end
34 end
35 end
36 end
37 end
38
39 prosody.events.add_handler("module-unloaded", cleanup)