Mercurial > prosody-modules
comparison mod_inotify_reload/mod_inotify_reload.lua @ 5395:82207f936f1f
mod_inotify_reload: Update to use FD watching method
This removes the need to present a fake socket interface, simplifying
everything.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 30 Apr 2023 20:34:36 +0200 |
parents | ab988e98a9f9 |
children |
comparison
equal
deleted
inserted
replaced
5394:434ee49b04de | 5395:82207f936f1f |
---|---|
10 local inh = inotify.init(); | 10 local inh = inotify.init(); |
11 | 11 |
12 local watches = {}; | 12 local watches = {}; |
13 local watch_ids = {}; | 13 local watch_ids = {}; |
14 | 14 |
15 -- Fake socket object around inotify | 15 require"net.server".watchfd(inh:fileno(), function() |
16 local inh_conn = { | 16 local events = inh:read(); |
17 getfd = function () return inh:fileno(); end; | 17 for _, event in ipairs(events) do |
18 dirty = function (self) return false; end; | 18 local mod = watches[watch_ids[event.wd]]; |
19 settimeout = function () end; | 19 if mod then |
20 send = function (_, d) return #d, 0; end; | 20 local host, name = mod.host, mod.name; |
21 close = function () end; | 21 module:log("debug", "Reloading changed module mod_%s on %s", name, host); |
22 receive = function () | 22 modulemanager.reload(host, name); |
23 local events = inh:read(); | 23 else |
24 for _, event in ipairs(events) do | 24 module:log("warn", "no watch for %d", event.wd); |
25 local mod = watches[watch_ids[event.wd]]; | |
26 if mod then | |
27 local host, name = mod.host, mod.name; | |
28 module:log("debug", "Reloading changed module mod_%s on %s", name, host); | |
29 modulemanager.reload(host, name); | |
30 else | |
31 module:log("warn", "no watch for %d", event.wd); | |
32 end | |
33 end | 25 end |
34 return ""; | |
35 end | 26 end |
36 }; | 27 end); |
37 require "net.server".wrapclient(inh_conn, "inotify", inh:fileno(), { | |
38 onincoming = function () end, ondisconnect = function () end | |
39 }, "*a"); | |
40 | 28 |
41 function watch_module(name, host, path) | 29 function watch_module(name, host, path) |
42 local id, err = inh:addwatch(path, inotify.IN_CLOSE_WRITE); | 30 local id, err = inh:addwatch(path, inotify.IN_CLOSE_WRITE); |
43 if not id then return nil, err; end | 31 if not id then return nil, err; end |
44 local k = host.."\0"..name; | 32 local k = host.."\0"..name; |