 ;; The license text is at http://www.apache.org/licenses/LICENSE-2.0
 
 (ns territory-bro.projections
-  (:require [mount.core :as mount]
+  (:require [clojure.tools.logging :as log]
+            [mount.core :as mount]
             [territory-bro.congregation :as congregation]
             [territory-bro.db :as db]
             [territory-bro.event-store :as event-store]
             [territory-bro.gis-user :as gis-user]
             [territory-bro.poller :as poller])
-  (:import (java.util.concurrent Executors ScheduledExecutorService TimeUnit)))
+  (:import (com.google.common.util.concurrent ThreadFactoryBuilder)
+           (java.util.concurrent Executors ScheduledExecutorService TimeUnit)))
 
 (mount/defstate cache
   :start (atom {:last-event nil



(mount/defstate scheduled-refresh
  :start (doto (Executors/newScheduledThreadPool 1)
           (.scheduleWithFixedDelay refresh-async! 0 60 TimeUnit/SECONDS))
  :stop (.shutdown ^ScheduledExecutorService scheduled-refresh))


        updated (apply-new-events conn cached)]
    (when-not (identical? cached updated)
      ;; with concurrent requests, only one of them will update the cache
      (when (compare-and-set! cache cached updated)
        (log/info "Updated from revision" (:event/global-revision (:last-event cached))
                  "to" (:event/global-revision (:last-event updated)))))))

(defn cached-state []
  (:state @cache))
