0

I have the code bellow for my Pedestal component. When Stuart Sierra's library starts my system-map, the method start implemented in the Pedestal defrecord gets called and it returns an updated version of my component with :pedestal-server associated. Shouldn't the lifecycle manager propagate the updated component so it could be used by the stop method? Whenever I try to stop the server by calling (component/stop (system)) in the REPL, nothing happens because the :pedestal-server key is set to nil.

(defrecord Pedestal [service-map pedestal-server]
  component/Lifecycle

 (start [this]
  (if pedestal-server
    this
    (assoc this :pedestal-server
                (-> service-map
                    http/create-server
                    http/start))))
 (stop [this]
   (when pedestal-server
     (http/stop pedestal-server))
   (assoc this :pedestal-server nil)))

(defn new-pedestal []
  (map->Pedestal {}))

1 Answers1

1

You should note that calling (com.stuartsierra.component/start) on a component the function returns a started copy of the component but it does not modify the component itself. Likewise calling (com.stuartsierra.component/stop) will return a stopped copy of the component.

I think that the value of :pedestal-server key is nil because you did not store the returned value of the (start) call but you called it on the original (unstarted) component.

You need to store the state of the application in some storage, like an atom or a var. Then you can update the state of the storage with start and stop.

For example:

;; first we create a new component and store it in system.
(def system (new-pedestal))

;; this function starts the state and saves it:
(defn start-pedestal! [] (alter-var-root #'system component/start))

;; this function stops the running state:
(defn stop-pedestal! [] (alter-var-root #'system component/stop))
erdos
  • 3,135
  • 2
  • 16
  • 27
  • Well, storing the state in an atom solved my problem. Thanks for the tip! Although Stuart Sierra's web app example does not store it in a mutable data structure. In his code, the instance returned by the web framework is assoc'ed to the returned component in the start method and is used by the stop method. Here is the example: https://github.com/stuartsierra/component – Norman Chimpsky Aug 18 '18 at 13:39