我有下面的代码为我的基座组件。当Stuart Sierra的库启动我的system-map时,在Pedestal defrecord中实现的方法start将被调用,并返回我的组件的更新版本,其中:pedestal-server相关联。生命周期管理器不应该传播更新后的组件,这样它就可以被stop方法使用吗?每当我试图通过调用REPL中的(component/stop (system))来停止服务器时,什么都不会发生,因为:pedestal-server键设置为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 {}))发布于 2018-08-17 03:36:12
您应该注意到,在组件上调用(com.stuartsierra.component/start)时,函数会返回组件的启动副本,但不会修改组件本身。同样,调用(com.stuartsierra.component/stop)将返回一个已停止的组件副本。
我认为:pedestal-server键的值是空的,因为您没有存储(start)调用的返回值,而是在原始(未启动)组件上调用了它。
您需要将应用程序的状态存储在某种存储中,如原子或变量。然后,您可以使用start和stop更新存储的状态。
例如:
;; 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))https://stackoverflow.com/questions/51882817
复制相似问题