检查由环处理程序更新的状态原子 [英] Inspect state atom which gets updated by a ring handler

查看:218
本文介绍了检查由环处理程序更新的状态原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情况:

最低启动任务启动http服务器:

A minimal boot task starts up a http server:

(boot (serve :handler 'myapp.server/handler
             :port 3000))

(这可能以几种方式启动,这里可以从nrepl会话运行它,例如由 boot repl

处理程序由处理程序在命名空间 myapp.server 中。相应的文件如下所示:

The handler is represented by the function handler inside the namespace myapp.server. The corresponding file looks like this:

(ns myapp.server (:require ...))

(defonce server-state (atom {:nr 0}))

(defn handler [req] 
  (prn (swap! server-state update :nr inc))
  {:body "Answer.\n"})

原子被更新,并且新版本被打印到repl中的stdout。

How can the atom been inspected at any time?

boot.user=> @myapp.server/server-state

会产生错误。 (... no such var ...)

当在emacs苹果酒中尝试同样的东西时连接,以前的尝试总是显示原子的初始值: {:n 0}

When trying the same thing from within an emacs cider nrepl connection, this previous attempt always shows up the initial value of the atom: {:n 0}

UPDATE

以下是使用emacs / cider时的操作步骤:

Here are the exact steps I do when using emacs/cider:


  1. cd projectdir

  2. emacs

  3. cider-jack-in

  4. (boot(dev))

  5. Ctrl + C + C

  6. 然后使用 curl 进行测试:获取响应+内部emacs更新atom记录: {:n 1} .. {:n 2} ..

  7. repl:(require'myapp.server)需要一段时间: nil

  8. finally: @ myapp.server / state - >但是: {:n 0}

  1. cd projectdir
  2. start emacs
  3. cider-jack-in
  4. (boot (dev))
  5. Ctrl+C+C (in order to get a prompt again.)
  6. Then Testing with curl: getting responses + inside emacs updated atom is logged: {:n 1} .. {:n 2} ..
  7. Then, in the repl: (require 'myapp.server), takes a while: nil.
  8. finally: @myapp.server/state --> however: {:n 0}


推荐答案

您的(... no such var ...) code>错误可能发生,因为你不需要 myapp.server 命名空间。尝试查看CIDER REPL中原子的更新失败可能是因为您的环应用程序运行在另一个JVM进程而不是您的REPL,所以REPL只看到初始值,因为环处理程序的更新发生在另一个JVM或它被包含在一个单独的类加载器中它可能会被引导POD 隔离。

Your (...no such var...) error probably happens because you haven't require the myapp.server namespace. Attempts to see updates happening to your atom in CIDER REPL fail probably because your ring app runs in another JVM process than your REPL so the REPL sees only initial value as updates from ring handler happen in another JVM or it is enclosed in a separated classloader as it might be isolated by boot POD.

您有两个选项:

启动您的REPL,然后从中启动您的铃声应用程式,即可存取所有载入的命名空间。

start your REPL and then start your ring app from it and you have access to all your loaded namespaces.

使用第一种方法,你可能需要使用boot-clj的 nrepl 选项。当你配置它启动nREPL服务器,那么你可以使用 boot repl -c (可选地提供与boot-http nrepl选项相同的坐标)或直接从CIDER 使用 cider- connect

With the first approach you probably need to use boot-clj's nrepl option. When you configure it to start nREPL server then you can connect to it using boot repl -c (optionally providing the same coordinates as to boot-http nrepl options) or directly from CIDER using cider-connect.

这篇关于检查由环处理程序更新的状态原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆