 
     (migrations/migration? args)
     (do
-      (mount/start-with-args #'db/databases)
+      (log-mount-states (mount/start #'db/databases))
       (migrate-database!)
       (migrations/migrate args (select-keys config/env [:database-url]))
       (System/exit 0))
 
     :else
-    (start-app args)))
+    (start-app)))

            [territory-bro.congregation :as congregation])
  (:gen-class))

(def ^:private cli-options
  [["-p" "--port PORT" "Port number"
    :parse-fn #(Integer/parseInt %)]])

(mount/defstate ^{:on-reload :noop} http-server
  :start
  (http/start
    (-> config/env
        (assoc :handler #'router/app)
        (update :io-threads #(or % (* 2 (.availableProcessors (Runtime/getRuntime)))))
        (update :port #(or (-> config/env :options :port) %))))
  :stop
  (http/stop http-server))


    (-> (db/tenant-schema (::congregation/schema-name congregation))
        (.migrate))))

(defn- log-mount-states [result]
  (doseq [component (:started result)]
    (log/info component "started"))
  (doseq [component (:stopped result)]
    (log/info component "stopped")))

(defn stop-app []
  (log-mount-states (mount/stop))
  (shutdown-agents))

(defn start-app []
  (try
    (log-mount-states (mount/start-without #'http-server))
    (migrate-database!)
    ;; start the public API only after the database is ready
    (log-mount-states (mount/start #'http-server))
    (doto (Runtime/getRuntime)
          (.addShutdownHook (Thread. ^Runnable stop-app)))
    (log/info "Started")
    (catch Throwable t
      (log/error t "Failed to start")
      (stop-app))))

(defn -main [& args]
  (log-mount-states (mount/start #'config/env))
  (cond
    (nil? (:database-url config/env))
    (do
