comparison mod_pubsub_feed/mod_pubsub_feed.lua @ 405:fe4fdba21a23

mod_pubsub_feed: Kill the timer on module unload
author Kim Alvefur <zash@zash.se>
date Sat, 27 Aug 2011 04:49:11 +0200
parents fc62b26dfdf6
children 52f2188ec47d
comparison
equal deleted inserted replaced
404:eb8b005d2a3d 405:fe4fdba21a23
50 feed_list[node] = { url = url; node = node; last_update = 0 }; 50 feed_list[node] = { url = url; node = node; last_update = 0 };
51 end 51 end
52 -- TODO module:hook("config-reloaded", above loop); 52 -- TODO module:hook("config-reloaded", above loop);
53 -- Also, keeping it somewhere persistent in order to avoid duplicated publishes? 53 -- Also, keeping it somewhere persistent in order to avoid duplicated publishes?
54 54
55 -- Used to kill the timer
56 local module_unloaded = false;
57 function module.unload()
58 module_unloaded = true;
59 end
60
55 -- Thanks to Maranda for this 61 -- Thanks to Maranda for this
56 local port, base, ssl = 5280, "callback", false; 62 local port, base, ssl = 5280, "callback", false;
57 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl }; 63 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl };
58 -- FIXME If ports isn't a table, this will cause an error 64 -- FIXME If ports isn't a table, this will cause an error
59 local _, first_port = next(ports); -- We base the callback URL on the first port config 65 local _, first_port = next(ports); -- We base the callback URL on the first port config
74 ["200"] = "OK"; 80 ["200"] = "OK";
75 ["202"] = "Accepted"; 81 ["202"] = "Accepted";
76 ["400"] = "Bad Request"; 82 ["400"] = "Bad Request";
77 ["403"] = "Forbidden"; 83 ["403"] = "Forbidden";
78 ["404"] = "Not Found"; 84 ["404"] = "Not Found";
85 ["500"] = "Internal Server Error";
79 ["501"] = "Not Implemented"; 86 ["501"] = "Not Implemented";
80 }; 87 };
81 88
82 local function http_response(code, headers, body) 89 local function http_response(code, headers, body)
83 return { 90 return {
160 end 167 end
161 end); 168 end);
162 end 169 end
163 170
164 function refresh_feeds() 171 function refresh_feeds()
172 if module_unloaded then return end
165 --module:log("debug", "Refreshing feeds"); 173 --module:log("debug", "Refreshing feeds");
166 for node, item in pairs(feed_list) do 174 for node, item in pairs(feed_list) do
167 --FIXME Don't fetch feeds which have a subscription 175 --FIXME Don't fetch feeds which have a subscription
168 -- Otoho, what if the subscription expires or breaks? 176 -- Otoho, what if the subscription expires or breaks?
169 if item.last_update + refresh_interval < time() then 177 if item.last_update + refresh_interval < time() then
200 module:log("debug", "subscription to %s submitted, staus %s", feed.node, code); 208 module:log("debug", "subscription to %s submitted, staus %s", feed.node, code);
201 end); 209 end);
202 end 210 end
203 211
204 function handle_http_request(method, body, request) 212 function handle_http_request(method, body, request)
213 if module_unloaded then
214 module:log("warn", "Received a HTTP request after module unload");
215 return http_response(500)
216 -- FIXME if this happens.
217 end
205 --module:log("debug", "%s request to %s%s with body %s", method, request.url.path, request.url.query and "?" .. request.url.query or "", #body > 0 and body or "empty"); 218 --module:log("debug", "%s request to %s%s with body %s", method, request.url.path, request.url.query and "?" .. request.url.query or "", #body > 0 and body or "empty");
206 local query = request.url.query or {}; 219 local query = request.url.query or {};
207 if query and type(query) == "string" then 220 if query and type(query) == "string" then
208 query = formdecode(query); 221 query = formdecode(query);
209 --module:log("debug", "GET data: %s", dump(query)); 222 --module:log("debug", "GET data: %s", dump(query));