-
-
Notifications
You must be signed in to change notification settings - Fork 103
vscode debug cluster 的一些研究过程流水账 #14
Copy link
Copy link
Closed
Description
背景
有些同学反馈 vscode 无法 debug egg 应用, 于是分析了下
流水账
启动时卡住
问题
在 vscode 的 debug 界面 launch 时:
node --debug-brk=21904 --nolazy index.js
Debugger listening on [::]:21904
2016-09-29 08:15:12,277 INFO 52531 [master] =================== egg start =====================
2016-09-29 08:15:12,278 INFO 52531 [master] egg version 0.1.3
2016-09-29 08:15:12,279 INFO 52531 [master] start with options: {"customEgg":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/.0.1.3@egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7002,"workers":1,"plugins":null,"https":false,"key":"","cert":""}
2016-09-29 08:15:12,282 INFO 52531 [master] Agent Worker:52532 start with ["{\"customEgg\":\"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/.0.1.3@egg\",\"baseDir\":\"/Users/tz/Workspaces/eggjs/test/showcase\",\"port\":7002,\"workers\":1,\"plugins\":null,\"https\":false,\"key\":\"\",\"cert\":\"\"}"]
Debugger listening on [::]:5856而正常的 egg 控制台启动日志, 除了上面那段外, 还会有以下:
2016-09-29 08:19:59,184 INFO 52849 [egg:watcher:agent] watcher start success
2016-09-29 08:19:59,219 INFO 52848 [master] Agent Worker started (404ms)
2016-09-29 08:19:59,223 INFO 52848 [master] App Worker#1:52850 start, state: none, current workers: ["1"]
Debugger listening on [::]:19883
2016-09-29 08:19:59,897 WARN 52848 [master] App Worker#1:52850 started at 7002, remain 0 (678ms)
2016-09-29 08:19:59,897 INFO 52848 [master] egg started on http://127.0.0.1:7002 (1082ms)分析 && 处理
- 看日志是 master 就被挂住了
--debug-brk把 master 进程挂住, 所以 agent 和 worker 都没起来- 这个是 vscode 的一个 bug: Node.js Cluster Example Fails under VS Code microsoft/vscode#3201 (comment)
- 在 index.js 的
startCluster之前加入以下代码可以临时解决
process.execArgv[0] = process.execArgv[0].replace('-brk', '');断点不了
问题
- 可是上面那种方式, attach 的是 master 进程, 并不能断点到 worker.
分析 && 处理
- 所以只能考虑先在外部启动
npm run dev - 然后在 vscode 里面不用 launch 而是 attach 指定端口
- 然而,
ps -ef|grep customEgg后发现egg-bin并没有传递--debug给 worker - 所以只能先用
node --debug index.js, 晚点再修复下egg-bin
worker 自动重启怎么办?
问题
egg-development监控到文件改变时, 会自动重启 worker- 而根据 node cluster 的机制, 新的 worker 的 debugPort 会 +1
- vscode 无法自动重连, 必须断开后, 修改
launch.json的端口, 然后重新 attach - 太麻烦了
分析 && 处理
思路:
- 可以写个 vscode 的插件, 用来启动 egg
- 此时, 它就是 egg 进程模型里面的 parent, 可以监听到 worker 的重启事件
- 进而调用 vscode 的 api, 去自动 attach 新端口
目前进展:
- vscode 的开发者文档真心不全, 自己也不是很熟悉 electron 的一些运作模式, 导致翻源码效率很低.
- 找到
vscode.commands.executeCommand('vscode.startDebug', {})这条指令, 可以成功的自动 attach
遇到问题:
- 插件设计
- vscode 同一时刻只能启动一个 debug, 如果期望 egg 也是在 vscode 里面启动
- 那上面想到的那个插件, 需要写成一个 vscode debugger 插件, 这个有点难度...
- API
vscode.startDebug这类 API 的文档, 根本不知道去哪里找... google 不到, 源码翻得很累
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels