commit a305d2ebfc7f0a5ec08c4cd9edfc017f03ffe5b6
parent 6489b509848e5792f94e2548dff729a7c1d28fdf
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 6 Aug 2025 20:30:26 +0100
move the file watcher into site.fnl
Diffstat:
2 files changed, 24 insertions(+), 18 deletions(-)
diff --git a/main.fnl b/main.fnl
@@ -1,9 +1,7 @@
(import-macros { : define-tests : expect : expect= } :assert)
(local lfs (require :lfs))
(local lcmark (require :lcmark))
-
(local cqueues (require :cqueues))
-(local notify (require :cqueues.notify))
(local server (require :server))
(local textile (require :textile))
@@ -309,25 +307,11 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
(dispatch route stream)
(server.headers stream { ":status" :404 } true))))
-(fn watch-files [site]
- (let [n (notify.opendir site.path notify.ALL)]
- (each [_ p (ipairs (site:search {}))]
- (n:add p.path))
- n))
-
(fn run []
(let [event-loop (cqueues.new)
- myserver (server.new { :onstream http-handler } )
- watcher (watch-files post-index)
- ]
+ myserver (server.new { :onstream http-handler } )]
(event-loop:wrap #(myserver:loop))
- (event-loop:wrap
- (fn []
- (while true
- (case (watcher:get)
- (flags name)
- (do (print "notified " flags "on" name)
- (post-index:refresh))))))
+ (event-loop:wrap #(post-index:watch))
(event-loop:loop)))
diff --git a/site.fnl b/site.fnl
@@ -2,10 +2,13 @@
(local lfs (require :lfs))
(local { : realpath } (require :posix.stdlib))
(local { : view } (require :fennel))
+(local cqueues (require :cqueues))
+(local notify (require :cqueues.notify))
(local textile (require :textile))
(local lcmark (require :lcmark))
(local { : to-html } (require :html))
+(local { : trace } (require :lib))
;; there are two date formats used in most of the extant posts:
;; "Fri Jun 29 14:17:56 2018" (emacs current-time-string)
@@ -221,6 +224,24 @@
some-posts)
posts)))
+(fn watch-changes [site]
+ (let [n (notify.opendir site.path notify.ALL)]
+ (while true
+ (each [_ p (ipairs (site:search {}))]
+ ;; inotify(7) says adding the same file twice is ok, it just
+ ;; updates the existing watch
+ (n:add p.path))
+ (case (n:get)
+ (flags name)
+ (do
+ (print "file change event" flags name)
+ ;; wait half a second before reloading in case there are
+ ;; more events
+ (while (= (cqueues.poll n 0.5) n)
+ (print "also" (n:get)))
+ (print "reloading posts" site.path)
+ (site:refresh))))))
+
(fn for-pathname [path]
(var posts (index-posts path))
{
@@ -232,6 +253,7 @@
:topics #(topics posts)
:weighted-topics #(weighted-topics posts)
:refresh #(set posts (index-posts path))
+ :watch watch-changes
})
(define-tests :static-path