move actual view refresh processing to new fn to make testing easier

This commit is contained in:
Gered 2016-05-27 11:46:46 -04:00
parent c31a896bd0
commit 1c47d4ca67

View file

@ -243,6 +243,20 @@
[last-update min-refresh-interval] [last-update min-refresh-interval]
(Thread/sleep (max 0 (- min-refresh-interval (- (System/currentTimeMillis) last-update))))) (Thread/sleep (max 0 (- min-refresh-interval (- (System/currentTimeMillis) last-update)))))
(defn do-view-refresh!
[{:keys [namespace view-id parameters] :as view-sig}]
(if (collect-stats?) (swap! statistics update-in [:refreshes] inc))
(try
(let [view (get-in @view-system [:views view-id])
vdata (data view namespace parameters)
hdata (hash vdata)]
(when-not (= hdata (get-in @view-system [:hashes view-sig]))
(doseq [subscriber-key (get-in @view-system [:subscribers view-sig])]
(send-view-data! subscriber-key view-sig vdata))
(swap! view-system assoc-in [:hashes view-sig] hdata)))
(catch Exception e
(error e "error refreshing:" namespace view-id parameters))))
(defn refresh-worker-thread (defn refresh-worker-thread
"Returns a refresh worker thread function. A 'refresh worker' continually waits for "Returns a refresh worker thread function. A 'refresh worker' continually waits for
refresh requests and when there is one, handles it by running the view, getting the view refresh requests and when there is one, handles it by running the view, getting the view
@ -251,19 +265,9 @@
(let [^ArrayBlockingQueue refresh-queue (:refresh-queue @view-system)] (let [^ArrayBlockingQueue refresh-queue (:refresh-queue @view-system)]
(fn [] (fn []
(try (try
(when-let [{:keys [namespace view-id parameters] :as view-sig} (.poll refresh-queue 60 TimeUnit/SECONDS)] (when-let [view-sig (.poll refresh-queue 60 TimeUnit/SECONDS)]
(trace "worker running refresh for" view-sig) (trace "worker running refresh for" view-sig)
(if (collect-stats?) (swap! statistics update-in [:refreshes] inc)) (do-view-refresh! view-sig))
(try
(let [view (get-in @view-system [:views view-id])
vdata (data view namespace parameters)
hdata (hash vdata)]
(when-not (= hdata (get-in @view-system [:hashes view-sig]))
(doseq [subscriber-key (get-in @view-system [:subscribers view-sig])]
(send-view-data! subscriber-key view-sig vdata))
(swap! view-system assoc-in [:hashes view-sig] hdata)))
(catch Exception e
(error e "error refreshing:" namespace view-id parameters))))
(catch InterruptedException e)) (catch InterruptedException e))
(if-not (:stop-workers? @view-system) (if-not (:stop-workers? @view-system)
(recur) (recur)