add option to shutdown fns to allow blocking until threads finish

This commit is contained in:
Gered 2016-05-26 13:24:23 -04:00
parent c75cf6abbc
commit 41edaf8665

View file

@ -311,18 +311,24 @@
(defn stop-update-watcher! (defn stop-update-watcher!
"Stops threads for the views refresh watcher and worker threads." "Stops threads for the views refresh watcher and worker threads."
[] [& [wait-for-threads?]]
(trace "stopping refresh watcher and workers") (trace "stopping refresh watcher and workers")
(swap! view-system assoc (let [worker-threads (:workers @view-system)
:stop-refresh-watcher? true watcher-thread (:refresh-watcher @view-system)
:stop-workers? true) threads (->> worker-threads
(if-let [^Thread refresh-watcher (:refresh-watcher @view-system)] (cons watcher-thread)
(.interrupt refresh-watcher)) (remove nil?))]
(doseq [^Thread worker-thread (:workers @view-system)] (swap! view-system assoc
(.interrupt worker-thread)) :stop-refresh-watcher? true
(swap! view-system assoc :stop-workers? true)
:refresh-watcher nil (doseq [^Thread t threads]
:workers nil)) (.interrupt t))
(if wait-for-threads?
(doseq [^Thread t threads]
(.join t)))
(swap! view-system assoc
:refresh-watcher nil
:workers nil)))
(defn logger-thread (defn logger-thread
"Returns a logger thread function. A logger periodically writes view system "Returns a logger thread function. A logger periodically writes view system
@ -358,12 +364,13 @@
(defn stop-logger! (defn stop-logger!
"Stops the logger thread." "Stops the logger thread."
[] [& [wait-for-thread?]]
(trace "stopping logger") (trace "stopping logger")
(swap! statistics assoc :stop? true) (let [^Thread logger-thread (:logger @statistics)]
(if-let [^Thread logger (:logger @statistics)] (swap! statistics assoc :stop? true)
(.interrupt logger)) (if logger-thread (.interrupt logger-thread))
(swap! statistics assoc :logger nil)) (if wait-for-thread? (.join logger-thread))
(swap! statistics assoc :logger nil)))
(defn hint (defn hint
"Create a hint." "Create a hint."
@ -472,9 +479,9 @@
(defn shutdown! (defn shutdown!
"Shuts the view system down, terminating all worker threads and clearing "Shuts the view system down, terminating all worker threads and clearing
all view subscriptions and data." all view subscriptions and data."
[] [& [wait-for-threads?]]
(trace "shutting down views sytem") (trace "shutting down views sytem")
(stop-update-watcher!) (stop-update-watcher! wait-for-threads?)
(if (:logging? @view-system) (if (:logging? @view-system)
(stop-logger!)) (stop-logger! wait-for-threads?))
(reset! view-system {})) (reset! view-system {}))