clojure - 启动方法后基座组件不更新自身
问题描述
我的基座组件的代码如下。当 Stuart Sierra 的库启动我的系统映射时,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 {}))
解决方案
您应该注意,(com.stuartsierra.component/start)
在组件上调用该函数会返回组件的启动副本,但它不会修改组件本身。同样,调用(com.stuartsierra.component/stop)
将返回组件的已停止副本。
我认为:pedestal-server
key 的值为 nil ,因为您没有存储(start)
调用的返回值,而是在原始(未启动的)组件上调用了它。
您需要将应用程序的状态存储在某个存储中,例如 atom 或 var。然后您可以使用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))
推荐阅读
- java - 如何使用比较器
作为通用 SortedDoublyLinkedList 中的参数 - java - 如果鼠标正在移动,则重新启动计时器
- polymer - 什么是 Polymer 3 中纸质菜单和纸质子菜单的正确替换?
- python - 动态制作文件名时不理解此字符串连接错误
- ios - 在 Swift 中将单词转换为数字
- c# - 打印一个 DB 表的所有列,而不在打印语句中写入每个列名
- c - 为什么我的二维数组打印出不正确的值?
- css - 在容器上延伸行但将其他内容保留在容器内
- python - 使用 pd.Dataframe.replace 和 apply 函数作为替换值
- python - 在python中使用子进程流式传输窗口(框架)