Skip to content

Commit

Permalink
Improve the debbugging (#146)
Browse files Browse the repository at this point in the history
- use init to run the bot on PID 1. This make the bot stop in less than a second, while it stop within 10s otherwise.
- update the debug config files for vscode, because I figured out why it wasn't working this way before (added a wait instruction)
  • Loading branch information
AiroPi authored Mar 23, 2024
1 parent 79a09df commit f5b2eb5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
114 changes: 81 additions & 33 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,100 @@ Additionally, this will setup `config.DEBUG` to `True` from the code perspective

### VSCode debug config

As an example, here is a json configuration that can be added inside your local `.vscode/launch.py` to use the integrated debugger:
To enable debugging inside VSCode, create a `.vscode/launch.json` file with:
```json
{
"name": "debug",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
"configurations": [
{
"localRoot": "${workspaceFolder}/src",
"remoteRoot": "/app"
"name": "debug",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/src",
"remoteRoot": "/app"
}
],
"preLaunchTask": "wait",
"postDebugTask": "restart"
}
]
}
```

After you run the code in debug mode, click on the "play" icon inside VSCode to attach the debug console. You can then use breakpoints, etc.
To make the restart button actually restart the bot and not just re-attach the debugger, you can add pre&post tasks:
And a `.vscode/tasks.json` file with:
```json
"preLaunchTask": "bot up",
"postDebugTask": "bot restart",
```
And in `.vscode/tasks.json`, add the tasks:
```json
{
"label": "bot up",
"type": "shell",
"presentation": {
"reveal": "silent"
},
"command": "docker compose -f compose.yml -f compose.debug.yml up -d",
},
{
"label": "bot restart",
"type": "shell",
"presentation": {
"reveal": "silent"
},
"command": "docker-compose restart mybot"
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"presentation": {
"focus": true,
},
"command": "docker-compose -f compose.yml -f compose.debug.yml build",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "watch",
"type": "shell",
"isBackground": true,
"presentation": {
"focus": true,
"panel": "new",
},
"command": "docker-compose -f compose.yml -f compose.debug.yml watch",
"problemMatcher": []
},
{
"label": "down",
"type": "shell",
"presentation": {
"focus": true,
},
"command": "docker compose down",
"problemMatcher": []
},
{
"label": "restart",
"type": "shell",
"presentation": {
"reveal": "silent",
"panel": "dedicated",
"close": true
},
"command": "docker-compose -f compose.yml -f compose.debug.yml restart mybot",
"problemMatcher": []
},
{
"label": "wait",
"type": "shell",
"presentation": {
"reveal": "silent",
"panel": "dedicated",
"close": true
},
"command": "sleep 0.1",
"problemMatcher": []
}
]
}
```

If the bot is executed with the up task, you should then use the `watch` command with `--no-up`.
The `restart` and `wait` tasks are used by the debug task. Waiting 0.1s is a little trick to avoid the debugger to close because debugpy isn't ready yet.
The other tasks aren't required, but you can run them from `Terminal > Run Task...` (and `Run Build Task...` for the build task).

You must run the `watch` command before debugging.

More information here: https://code.visualstudio.com/docs/python/debugging

## Database revisions
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ FROM base as debug
ENV DEBUG=1
ENV LOG_LEVEL=DEBUG
RUN pip install debugpy
CMD ["/bin/sh", "-c", "alembic upgrade head && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./main.py run -c ./config.toml"]
CMD ["/bin/sh", "-c", "alembic upgrade head && python -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./main.py run -c ./config.toml"]
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3'

services:
mybot:
init: true
image: airopi/mybot:stable
build:
dockerfile: ./Dockerfile
Expand Down

0 comments on commit f5b2eb5

Please sign in to comment.