databaseclojureconfigcomponent-diagram

Clojure: How to start application even if database is not available?


I want to create a Clojure application that can use either a database or a configuration file to retrieve information.

I tried to disable the error message hoping that db would have the 'value' nil

(defstate ^:dynamic *db*
  :start (when-let [jdbc-url (env :database-url)]
           (conman/connect! {:jdbc-url jdbc-url})
           ; (do
             ; (log/warn "database connection URL was not found, please set :database-url in your config, e.g: dev-config.edn")
             ; *db*)
             )
  :stop (conman/disconnect! *db*))

I want to create only one application from which I can retrieve the name, version and ... of applications in a test environment. Not all these application have a database available in which we can add a table this information. Therefore I want to store this information in a configuration file. In this way I can deploy this little app as an API next to every application and use another application to call these API's and create a component-diagram in SVG.

Question: What should I do to be able to start this application if the database is not available?


Solution

  • In the above question I tried to solve my problem in the wrong file. The way that worked for me was by disabling the first condition in -main in core.clj.

    (defn -main [& args]
      (mount/start #'version-api2.config/env)
      (cond
        ; (nil? (:database-url env))
        ; (do
          ; (log/error "Database configuration not found, :database-url environment variable must be set before running")
          ; (System/exit 1))
        (some #{"init"} args)
        (do
          (migrations/init (select-keys env [:database-url :init-script]))
          (System/exit 0))
        (migrations/migration? args)
        (do
          (migrations/migrate args (select-keys env [:database-url]))
          (System/exit 0))
        :else
        (start-app args)))
    

    To make my application fool proof I should change this validation to something like: IF db not available THEN config file must be available.

    However this solved my problem for now. :-)