Skip to content

Commit

Permalink
Use daemon threads for auto-reload
Browse files Browse the repository at this point in the history
Use dedicated daemon threads for auto-reload logic so that it doesn't
prevent orderly JVM shutdown.  Also names the thread 'enlive
auto-reloader: $ns'.

Prior to this patch, `clojure.core/future` was used to run the code that
waits for file changes for a ns.  `future` puts tasks on the
agent-send-off thread pool, which consisits of non-daemon threads.
There is no shutdown/stop logic for the auto-reload function, so
auto-reload threads will run forever and prevent orderly shutdown of the
JVM.  Daemon threads don't block JVM shutdown, so this seems like a good
place to use them.
  • Loading branch information
overthink committed Jan 22, 2014
1 parent 2dbf770 commit 714867c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/net/cgrand/reload.clj
Expand Up @@ -19,26 +19,30 @@
::watch-fn
(let [ws (-> (java.nio.file.FileSystems/getDefault)
.newWatchService)
file-paths (atom #{})]
(future
(loop []
(let [wk (.take ws)
^java.nio.file.Path dir (.watchable wk)
paths (map (fn [^java.nio.file.WatchEvent e]
(.resolve dir (.context e)))
(.pollEvents wk))]
(if (some @file-paths paths)
(do
(.close ws)
;; file updates concurrent with reloading may be skipped
(println "Reloading" (ns-name ns))
(alter-meta! ns assoc ::deps #{}
::watch-fn nil)
(require (ns-name ns) :reload)
(auto-reload ns))
(do
(.reset wk)
(recur))))))
file-paths (atom #{})
reloader-fn (fn []
(loop []
(let [wk (.take ws)
^java.nio.file.Path dir (.watchable wk)
paths (map (fn [^java.nio.file.WatchEvent e]
(.resolve dir (.context e)))
(.pollEvents wk))]
(if (some @file-paths paths)
(do
(.close ws)
;; file updates concurrent with reloading may be skipped
(println "Reloading" (ns-name ns))
(alter-meta! ns assoc ::deps #{}
::watch-fn nil)
(require (ns-name ns) :reload)
(auto-reload ns))
(do
(.reset wk)
(recur))))))]
(doto (Thread. reloader-fn)
(.setName (format "enlive auto-reloader: %s" ns))
(.setDaemon true)
(.start))
(fn [path]
(swap! file-paths conj path)
(.register (.getParent path) ws
Expand Down

0 comments on commit 714867c

Please sign in to comment.