问题
I am using this base Heroku project for a Node cluster environment.
https://github.com/heroku-examples/node-workers-example
However since it uses foreman to create multiple processes on start, I can't figure out how to connect it to VS Code Debug using a launch.json file like I normally would.
This is the start command.
"scripts": {
"start": "nf start",
}
回答1:
It took me a little while to figure out a workable debugging methodology.. but to debug the server, this is what I am currently using:
NOTE: I am not using Foreman ... I only use that in production
Assumptions
This assumes you are using the example from Heroku on background threads and you have Redis installed locally (and of course all the supporting packages you need)
concurrently is the secret sauce !
Developer side (DEBUG)
Package.json
just the script section
"scripts": {
"start": "nf start",
"server": "node server.js",
"client": "npm start --prefix client",
"devRedis": "redis-server.exe",
"devRedisWorker": "node ./routes/api/workers/worker_redis.js",
"devDebugPostman": "concurrently \"npm run devRedis\" \"npm run devRedisWorker\"",
"dev": "concurrently \"npm run devRedis\" \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"dependencies": {
"bull": "^3.14.0",
"concurrently": "^5.2.0",
"express": "^4.17.1",
"foreman": "^3.0.1",
"threads": "^1.4.1",
"throng": "^4.0.0"
}
Making it all 'work'
I use two easy (in my mind) steps
Step 1
Use NPM to start redis and my worker thread
Open a terminal and
npm run devDebugPostman
this is the output I get:
D:\Code\Udemy\workerthreadtest>npm run devDebugPostman
> workerthreadtest@0.1.0 devDebugPostman D:\Code\Udemy\workerthreadtest
> concurrently "npm run devRedis" "npm run devRedisWorker"
[0]
[0] > workerthreadtest@0.1.0 devRedis D:\Code\Udemy\workerthreadtest
[0] > redis-server.exe
[0]
[1]
[1] > workerthreadtest@0.1.0 devRedisWorker D:\Code\Udemy\workerthreadtest
[1] > node ./routes/api/workers/workerRedis.js
[1]
[0] [2720] 08 May 16:09:44.775 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server.exe /path/to/redis.conf
[0] _._
[0] _.-``__ ''-._
[0] _.-`` `. `_. ''-._ Redis 3.0.503 (00000000/0) 64 bit
[0] .-`` .-```. ```\/ _.,_ ''-._
[0] ( ' , .-` | `, ) Running in standalone mode
[0] |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
[0] | `-._ `._ / _.-' | PID: 2720
[0] `-._ `-._ `-./ _.-' _.-'
[0] |`-._`-._ `-.__.-' _.-'_.-'|
[0] | `-._`-._ _.-'_.-' | http://redis.io
[0] `-._ `-._`-.__.-'_.-' _.-'
[0] |`-._`-._ `-.__.-' _.-'_.-'|
[0] | `-._`-._ _.-'_.-' |
[0] `-._ `-._`-.__.-'_.-' _.-'
[0] `-._ `-.__.-' _.-'
[0] `-._ _.-'
[0] `-.__.-'
[0]
[0] [2720] 08 May 16:09:44.779 # Server started, Redis version 3.0.503
[0] [2720] 08 May 16:09:44.779 * DB loaded from disk: 0.000 seconds
[0] [2720] 08 May 16:09:44.779 * The server is now ready to accept connections on port 6379
[1] throng start 2 worker(s) 10 job(s) per worker
[1] throng start 2 worker(s) 10 job(s) per worker
[1] throng start 2 worker(s) 10 job(s) per worker
Step 2
Optionally set breakpoints in file of your choice (server side)
Start the Visual code debugger
Now I can use Postman to send API calls into the server and hit the break points.
my server start up:
C:\Program Files\nodejs\node.exe --inspect-brk=17446 server.js
Debugger listening on ws://127.0.0.1:17446/65d39c8b-33e0-4c90-ae9b-3d12f6db4e27
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
server info:
port 4000
redis redis://127.0.0.1:6379
Production side (RELEASE)
These are the files that get used on Heroku
Procfile
web: node server.js
worker: node ./routes/api/workers/worker_redis.js
Package.json
these are the 'important' parts for the production stuff to work
"scripts": {
"start": "nf start",
},
"dependencies": {
"foreman": "^3.0.1"
}
来源:https://stackoverflow.com/questions/61591835/how-to-get-vs-code-debug-to-work-with-foreman-and-nf-start