anoia.svc use timeout for inotify

in case we miss a message, check the directory every 5s
anyway
This commit is contained in:
Daniel Barlow 2024-07-26 23:40:40 +01:00
parent d300373b96
commit eca8e37e7a
1 changed files with 8 additions and 2 deletions

View File

@ -1,13 +1,14 @@
(local inotify (require :inotify))
(local { : file-exists? } (require :anoia))
(local { : file-type : dir &as fs } (require :anoia.fs))
(local ll (require :lualinux))
(fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
(f:read "*l")))
(fn watch-fsevents [directory-name]
(let [handle (inotify.init)]
(let [handle (inotify.init { :blocking false})]
(handle:addwatch directory-name
inotify.IN_CREATE
inotify.IN_MOVE
@ -39,11 +40,16 @@
(coroutine.yield self)
(self:wait))))
(fn read-with-timeout [watcher]
(let [fd (watcher:fileno)]
(ll.pollin fd 5000)
(watcher:read)))
(fn open [directory]
(let [watcher (watch-fsevents directory)
has-file? (fn [filename] (file-exists? (.. directory "/" filename)))]
{
:wait #(watcher:read)
:wait #(read-with-timeout watcher)
:ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock"))))
:output (fn [_ filename]