 
 
 (mount/defstate scheduled-refresh
-  :start (doto (Executors/newScheduledThreadPool 1)
+  :start (doto (Executors/newScheduledThreadPool 1 (-> (ThreadFactoryBuilder.)
+                                                       (.setNameFormat "territory-bro.projections/scheduled-refresh")
+                                                       (.setDaemon true)
+                                                       (.build)))
            (.scheduleWithFixedDelay refresh-async! 0 60 TimeUnit/SECONDS))
   :stop (.shutdown ^ScheduledExecutorService scheduled-refresh))
 

;; The license text is at http://www.apache.org/licenses/LICENSE-2.0

(ns territory-bro.projections
  (:require [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)))

(mount/defstate cache
  :start (atom {:last-event nil

        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))
