   "This configures logging and exception handling, to make the configuration phase simpler to understand"
   (graph/eager-compile
     {:exception-handler (fnk [[:config [:unhandled-exceptions {log-level :error} {email nil}]] logging]
-                             ((lazy-load-var 'cook.util/install-email-on-exception-handler) log-level email))
+                          ((lazy-load-var 'cook.util/install-email-on-exception-handler) log-level email))
      :logging (fnk [[:config log]]
-                   (init-logger log))}))
+                (init-logger log))}))
 
 (defn- env [name]
   (System/getenv name))


(def raw-scheduler-routes
  {:scheduler (fnk [mesos-datomic framework-id mesos-pending-jobs-atom [:settings task-constraints mesos-gpu-enabled is-authorized-fn]]
                   ((lazy-load-var 'cook.mesos.api/main-handler)
                    mesos-datomic
                    framework-id
                    task-constraints
                    mesos-gpu-enabled
                    (fn [] @mesos-pending-jobs-atom)
                    is-authorized-fn))
   :view (fnk [scheduler]
              scheduler)})

(def full-routes
  {:raw-scheduler raw-scheduler-routes
   :view (fnk [raw-scheduler]
              (routes (:view raw-scheduler)
                      (route/not-found "<h1>Not a valid route</h1>")))})

(def mesos-scheduler
  {:mesos-scheduler (fnk [[:settings mesos-master mesos-master-hosts mesos-leader-path mesos-failover-timeout mesos-principal mesos-role mesos-framework-name offer-incubate-time-ms mea-culpa-failure-limit fenzo-max-jobs-considered fenzo-scaleback fenzo-floor-iterations-before-warn fenzo-floor-iterations-before-reset task-constraints riemann mesos-gpu-enabled rebalancer good-enough-fitness] mesos-datomic mesos-datomic-mult curator-framework mesos-pending-jobs-atom mesos-offer-cache]
                         (let [make-mesos-driver-fn (partial (lazy-load-var 'cook.mesos/make-mesos-driver)
                                                             {:mesos-master mesos-master
                                                              :mesos-failover-timeout mesos-failover-timeout
                                                              :mesos-principal mesos-principal
                                                              :mesos-role mesos-role
                                                              :mesos-framework-name mesos-framework-name
                                                              :gpus-enabled? mesos-gpu-enabled})
                               get-mesos-utilization-fn (partial (lazy-load-var 'cook.mesos/get-mesos-utilization) mesos-master-hosts)]
                           (try
                             (Class/forName "org.apache.mesos.Scheduler")
                             ((lazy-load-var 'cook.mesos/start-mesos-scheduler)
                              make-mesos-driver-fn
                              get-mesos-utilization-fn
                              curator-framework
                              mesos-datomic
                              mesos-datomic-mult
                              mesos-leader-path
                              offer-incubate-time-ms
                              mea-culpa-failure-limit
                              task-constraints
                              (:host riemann)
                              (:port riemann)
                              mesos-pending-jobs-atom
                              mesos-offer-cache
                              mesos-gpu-enabled
                              rebalancer
                              {:fenzo-max-jobs-considered fenzo-max-jobs-considered
                               :fenzo-scaleback fenzo-scaleback
                               :fenzo-floor-iterations-before-warn fenzo-floor-iterations-before-warn
                               :fenzo-floor-iterations-before-reset fenzo-floor-iterations-before-reset
                               :good-enough-fitness good-enough-fitness})
                             (catch ClassNotFoundException e
                               (log/warn e "Not loading mesos support...")
                               nil))))})

(defn health-check-middleware
  "This adds /debug to return 200 OK"


(def mesos-datomic
  (fnk [[:settings mesos-datomic-uri]]
    ((lazy-load-var 'datomic.api/create-database) mesos-datomic-uri)
    (let [conn ((lazy-load-var 'datomic.api/connect) mesos-datomic-uri)]
      (doseq [txn (deref (lazy-load-var 'cook.mesos.schema/work-item-schema))]
        (deref ((lazy-load-var 'datomic.api/transact) conn txn))
        ((lazy-load-var 'metatransaction.core/install-metatransaction-support) conn)
        ((lazy-load-var 'metatransaction.utils/install-utils-support) conn))
      conn)))

(def curator-framework
  (fnk [[:settings zookeeper] local-zookeeper]
    (let [retry-policy (BoundedExponentialBackoffRetry. 100 120000 10)
          ;; The 180s session and 30s connection timeouts were pulled from a google group
          ;; recommendation
          curator-framework (CuratorFrameworkFactory/newClient zookeeper 180000 30000 retry-policy)
          ]
      (.. curator-framework
          getConnectionStateListenable
          (addListener (reify ConnectionStateListener
                         (stateChanged [_ client newState]
                           (log/info "Curator state changed:"
                                     (str newState))))))
      (.start curator-framework)
      curator-framework)))

(defn tell-jetty-about-usename [h]
  "Our auth in cook.spnego doesn't hook in to Jetty - this handler
