Changeset

475:db5702bb9e41

mod_pubsub_feed: Dynamicaly reloadable config.
author Kim Alvefur <zash@zash.se>
date Thu, 10 Nov 2011 11:45:32 +0100
parents 474:942738953ff3
children 476:2c85635318a5
files mod_pubsub_feed/mod_pubsub_feed.lua
diffstat 1 files changed, 35 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_feed/mod_pubsub_feed.lua	Thu Nov 10 11:24:31 2011 +0100
+++ b/mod_pubsub_feed/mod_pubsub_feed.lua	Thu Nov 10 11:45:32 2011 +0100
@@ -38,19 +38,33 @@
 local urldecode  = http.urldecode;
 local urlencode  = http.urlencode;
 
-local config = module:get_option("feeds") or {
-	planet_jabber = "http://planet.jabber.org/atom.xml";
-	prosody_blog = "http://blog.prosody.im/feed/atom.xml";
-};
-local refresh_interval = module:get_option_number("feed_pull_interval", 15) * 60;
-local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true); -- HTTP by default or not?
-local httphost = module:get_option_string("pubsubhubub_httphost", module.host); -- If module.host IN A doesn't point to this server, use this to override.
-local feed_list = { }
-for node, url in pairs(config) do
-	feed_list[node] = { url = url; node = node; last_update = 0 };
+local feed_list = {};
+local refresh_interval;
+
+-- Dynamicaly reloadable config.
+local function update_config()
+	local config = module:get_option("feeds") or {
+		planet_jabber = "http://planet.jabber.org/atom.xml";
+		prosody_blog = "http://blog.prosody.im/feed/atom.xml";
+	};
+	refresh_interval = module:get_option_number("feed_pull_interval", 15) * 60;
+	local new_feed_list = {};
+	for node, url in pairs(config) do
+		new_feed_list[node] = true;
+		if not feed_list[node] then
+			feed_list[node] = { url = url; node = node; last_update = 0 };
+		else
+			feed_list[node].url = url;
+		end
+	end
+	for node in pairs(feed_list) do
+		if not new_feed_list[node] then
+			feed_list[node] = nil;
+		end
+	end
 end
--- TODO module:hook("config-reloaded", above loop);
--- Also, keeping it somewhere persistent in order to avoid duplicated publishes?
+update_config();
+module:hook("config-reloaded", update_config);
 
 -- Used to kill the timer
 local module_unloaded = false;
@@ -58,6 +72,13 @@
 	module_unloaded = true;
 end
 
+-- Config stuff that can't be reloaded, since it would need to re-bind HTTP stuff.
+
+-- If module.host IN A doesn't point to this server, use this to override.
+local httphost = module:get_option_string("pubsubhubub_httphost", module.host);
+-- HTTP by default or not?
+local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true);
+
 -- Thanks to Maranda for this
 local port, base, ssl = 5280, "callback", false;
 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl };
@@ -169,12 +190,13 @@
 end
 
 function refresh_feeds()
+	local now = time();
 	if module_unloaded then return end
 	--module:log("debug", "Refreshing feeds");
 	for node, item in pairs(feed_list) do
 		--FIXME Don't fetch feeds which have a subscription
 		-- Otoho, what if the subscription expires or breaks?
-		if item.last_update + refresh_interval < time() then 
+		if item.last_update + refresh_interval < now then 
 			module:log("debug", "checking %s", item.node);
 			fetch(item, update_entry);
 		end