Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode debug cluster 的一些研究过程流水账 #14

Closed
atian25 opened this issue Sep 29, 2016 · 2 comments
Closed

vscode debug cluster 的一些研究过程流水账 #14

atian25 opened this issue Sep 29, 2016 · 2 comments

Comments

@atian25
Copy link
Owner

atian25 commented Sep 29, 2016

背景

有些同学反馈 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)

分析 && 处理

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 同一时刻只能启动一个 debug, 如果期望 egg 也是在 vscode 里面启动
    • 那上面想到的那个插件, 需要写成一个 vscode debugger 插件, 这个有点难度...
  • API
    • vscode.startDebug 这类 API 的文档, 根本不知道去哪里找... google 不到, 源码翻得很累
@atian25
Copy link
Owner Author

atian25 commented Sep 29, 2016

想起了, 左耳朵耗子的这个微博: http://weibo.com/1401880315/Ea1F1zpoS

程序员解决问题时的情节:换个灯泡,发现架子松了,拿镙丝刀的时候,发现抽屉的滚轴要上油了,而油又没了,要开车去买,结果车又坏了……于是,老板骂到:“我只是让你换个灯泡,你TMD为什么在修车?!”

Yak Shaving is bad for short term productivity, but it's incredible for long term growth.

视频地址: http://t.cn/RcEuAkQ

@barretlee
Copy link

哈哈哈,程序员就是有这种毛病吧,要扫清眼前遇到的所有的障碍。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants