extract service output watcher to fennel module

This commit is contained in:
Daniel Barlow 2023-07-08 22:15:56 +01:00
parent 708350711b
commit 0c41e9305c
3 changed files with 40 additions and 33 deletions

View file

@ -10,7 +10,9 @@ in stdenv.mkDerivation {
src = ./.;
nativeBuildInputs = [ fennel ];
buildPhase = ''
fennel --compile init.fnl > init.lua
for f in *.fnl ; do
fennel --compile $f > `basename $f .fnl`.lua
done
'';
installPhase = ''
mkdir -p "$out/share/lua/${lua.luaversion}/${pname}"

31
pkgs/anoia/svc.fnl Normal file
View file

@ -0,0 +1,31 @@
(local inotify (require :inotify))
(local { : file-exists? } (require :anoia))
(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)]
(handle:addwatch directory-name
inotify.IN_CREATE
inotify.IN_MOVE
inotify.IN_DELETE
inotify.IN_DELETE_SELF
inotify.IN_MOVED_FROM
inotify.IN_MOVED_TO
inotify.IN_CLOSE_WRITE)
handle))
(fn open [directory]
(let [watcher (watch-fsevents directory)
has-file? (fn [filename] (file-exists? (.. directory "/" filename)))]
{
:wait (fn [] (watcher:read))
:ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock"))))
:output (fn [_ filename] (read-line (.. directory "/" filename)))
:close #(watcher:close)
}))
{ : open }