Mercurial > prosody-modules
comparison mod_auto_activate_hosts/mod_auto_activate_hosts.lua @ 1006:9c88960b0f81
mod_auto_activate_hosts: Automatically activate and deactivate hosts when they are added/removed from the config
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 09 May 2013 10:19:24 +0100 |
| parents | |
| children | 8285c3502100 |
comparison
equal
deleted
inserted
replaced
| 1005:591590de34ef | 1006:9c88960b0f81 |
|---|---|
| 1 module:set_global(); | |
| 2 | |
| 3 local array = require "util.array"; | |
| 4 local set = require "util.set"; | |
| 5 local it = require "util.iterators"; | |
| 6 local config = require "core.configmanager"; | |
| 7 | |
| 8 local function host_not_global(host) | |
| 9 return host ~= "*"; | |
| 10 end | |
| 11 | |
| 12 local function host_is_enabled(host) | |
| 13 return config.get(host, "enabled") ~= false; | |
| 14 end | |
| 15 | |
| 16 function handle_reload() | |
| 17 local new_config = config.getconfig(); | |
| 18 local active_hosts = set.new(array.collect(it.keys(prosody.hosts)):filter(host_not_global)); | |
| 19 local enabled_hosts = set.new(array.collect(it.keys(new_config)):filter(host_is_enabled)); | |
| 20 local need_to_activate = enabled_hosts - active_hosts; | |
| 21 local need_to_deactivate = active_hosts - enabled_hosts; | |
| 22 | |
| 23 module:log("debug", "Config reloaded... %d hosts need activating, and %d hosts need deactivating", it.count(need_to_activate), it.count(need_to_deactivate)); | |
| 24 module:log("debug", "There are %d enabled and %d active hosts", it.count(enabled_hosts), it.count(active_hosts)); | |
| 25 for host in need_to_deactivate do | |
| 26 hostmanager.deactivate(host); | |
| 27 end | |
| 28 | |
| 29 -- If the lazy loader is loaded, hosts will get activated when they are needed | |
| 30 if not(getmetatable(prosody.hosts) and getmetatable(prosody.hosts).lazy_loader) then | |
| 31 for host in need_to_activate do | |
| 32 hostmanager.activate(host); | |
| 33 end | |
| 34 end | |
| 35 end | |
| 36 | |
| 37 module:hook_global("config-reloaded", handle_reload); |
