Changeset

562:b3f8435e661c

mod_stanza_counter_http: added cleanup function (from mod_register_json)
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 17 Jan 2012 01:27:38 +0000
parents 561:f2ec7149b005
children 563:cd5581b58fdc
files mod_stanza_counter/mod_stanza_counter_http.lua
diffstat 1 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_stanza_counter/mod_stanza_counter_http.lua	Tue Jan 17 01:18:36 2012 +0000
+++ b/mod_stanza_counter/mod_stanza_counter_http.lua	Tue Jan 17 01:27:38 2012 +0000
@@ -42,9 +42,38 @@
 end
 
 -- initialization.
--- init http interface
+-- init http and cleanup interface
+
+function cleanup() -- recycled from mod_register_json, it's handy
+        module:log("debug", "Cleaning up handlers and stuff as module is being unloaded.")
+        for _, options in ipairs(ports) do
+                if options.port then
+                        httpserver.new.http_servers[options.port].handlers[options.path or "stanza-counter"] = nil
+                end
+        end
+
+        -- if there are no handlers left clean and close the socket, doesn't work with server_event
+        local event = require "core.configmanager".get("*", "core", "use_libevent")
+
+        if not event then
+                for _, options in ipairs(ports) do
+                        if options.port and not next(httpserver.new.http_servers[options.port].handlers) then
+                                httpserver.new.http_servers[options.port] = nil
+                                if options.interface then
+                                        for _, value in ipairs(options.interface) do
+                                                if server.getserver(value, options.port) then server.removeserver(value, options.port) end
+                                        end
+                                else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end
+                        end
+                end
+        end
+
+        prosody.events.remove_handler("module-unloaded", cleanup)
+end
+
 local function setup()
 	httpserver.new_from_config(ports, req, { base = "stanza-counter" })
+	prosody.events.add_handler("module-unloaded", cleanup)
 end
 
 -- set it