Skip to content

Instantly share code, notes, and snippets.

@aroemers aroemers/app.clj
Last active Dec 12, 2019

What would you like to do?
(ns app
(:require [lifecycle :refer [combine parallel]]))
(defn components [app-config]
(combine (parallel (users-db/component)
(rest-api/component app-config)))
(ns core
(:require [lifecycle :refer [start stop combine]]))
(defn -main [& args]
(let [component (combine (logger/component)
(jdbc/component jdbc-config)
(app/components app-config))
system (start component {})]
;; bonus: the empty map above could be filled by another
;; lifecycle library/approach.
(stop component system)))
(ns jdbc
(:require [hikari-cp.core :as hikari]
[lifecycle :refer [Lifecycle]]))
(defn component [config]
(reify Lifecycle
(start [_ {:keys [logger]}]
(logger "starting JDBC pool...")
{:jdbc (hikari/make-datasource config)})
(stop [_ {:keys [logger jdbc]}]
(logger "stopping JDBC pool...")
(hikari/close-datasource jdbc))))
(ns lifecycle)
(defprotocol Lifecycle
:extend-via-metadata true
(start [this system])
(stop [this system]))
(defn combine
[& lifecycles]
(reify Lifecycle
(start [_ system]
(reduce #(merge %1 (start %2 %1)) system lifecycles))
(stop [_ system]
(doseq [lifecycle (reverse lifecycles)]
(stop lifecycle system)))))
(defn parallel
[& lifecycles]
(reify Lifecycle
(start [_ system]
(->> (mapv #(future (start % system)) lifecycles)
(map deref)
(apply merge)))
(stop [_ system]
(->> (mapv #(future (stop % system)) lifecycles)
(mapv deref)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.