Software /
code /
prosody-modules
Annotate
mod_inotify_reload/mod_inotify_reload.lua @ 681:03ef667c96c3
mod_host_guard: removed s2smanager import.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Sat, 26 May 2012 22:48:03 +0000 |
parent | 652:3e6f43ab7e22 |
child | 744:ab988e98a9f9 |
rev | line source |
---|---|
652
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- mod_inotify_reload |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Reloads modules when their files change |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Depends on linotify: https://github.com/hoelzro/linotify |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 module:set_global(); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local inotify = require "inotify"; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local modulemanager = require "core.modulemanager"; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local inh = inotify.init(); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local watches = {}; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local watch_ids = {}; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 -- Fake socket object around inotify |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local inh_conn = { |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 getfd = function () return inh:fileno(); end; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 dirty = function (self) return false; end; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 settimeout = function () end; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 send = function (_, d) return #d, 0; end; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 close = function () end; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 receive = function () |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local events = inh:read(); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 for _, event in ipairs(events) do |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local mod = watches[watch_ids[event.wd]]; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 if mod then |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local host, name = mod.host, mod.name; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 module:log("debug", "Reloading changed module mod_%s on %s", name, host); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 modulemanager.reload(host, name); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 else |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 module:log("warn", "no watch for %d", event.wd); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return ""; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 }; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 require "net.server".wrapclient(inh_conn, "inotify", inh:fileno(), { |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 onincoming = function () end, ondisconnect = function () end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 }, "*a"); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 function watch_module(name, host, path) |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local id, err = inh:addwatch(path, inotify.IN_CLOSE_WRITE); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if not id then return nil, err; end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local k = host.."\0"..name; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 watches[k] = { id = id, path = path, name = name, host = host }; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 watch_ids[id] = k; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 return true; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 function unwatch_module(name, host) |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local k = host.."\0"..name; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if not watches[k] then |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 return nil, "not-watching"; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 local id = watches[k].id; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local ok, err = inh:rmwatch(id); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 watches[k] = nil; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 watch_ids[id] = nil; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 return ok, err; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 function module_loaded(event) |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local host, name = event.host, event.module; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local path = modulemanager.get_module(host, name).module.path; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if not path then |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 module:log("warn", "Couldn't watch mod_%s, no path", name); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 return; |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 if watch_module(name, host, path) then |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 module:log("debug", "Watching mod_%s", name); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 function module_unloaded(event) |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 unwatch_module(event.module, event.host); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 function module.add_host(module) |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 module:hook("module-loaded", module_loaded); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 module:hook("module-unloaded", module_unloaded); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 module:hook("module-loaded", module_loaded); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 module:hook("module-unloaded", module_unloaded); |
3e6f43ab7e22
mod_inotify_reload: Reload modules when their code changes
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |