【发布时间】:2012-01-05 22:55:25
【问题描述】:
我正在开发一个 Zappa 应用程序,我目前正在尝试制作一个小监视脚本来停止服务器,清除 require.cache 然后在文件更改时重新要求并重新启动服务器,例如:
# watch all dependent files
for file of require.cache
fs.watch file, ->
# attach a handler to 'close'
# -- here's the issue: this takes far too long to trigger
server.on 'close', ->
server = require './server'
server.start()
# log the new server's id
console.log server.id
# stop the current server instance
server.stop()
# clear require's cache
delete require.cache[f] for f of require.cache
我的请求处理程序中还有一个 console.log server.id 行,因此我可以检查 ID 是否匹配。
所以,发生的事情是:当我更改一个依赖项时,服务器停止,一个新的启动并记录新的 ID,这都是肉汁。但是,在之后的随机时间内,对服务器的请求仍然记录旧 ID,表明旧的侦听器仍然以某种方式连接。最终,监听器似乎“切换”并记录了新 ID。
更新: 这似乎与close 事件有关(不出所料) - 如果我将一个简单的console.log 'close' 回调附加到close 事件,则ID 在@987654327 之后开始更改@ 出现。但是,触发close 事件可能需要很长时间(10 秒以上),为什么需要这么长时间?
【问题讨论】: