From 949ad7b128f1bc42f7d55a421979edccdef52fe4 Mon Sep 17 00:00:00 2001 From: "airo.pi_" <47398145+AiroPi@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:36:35 +0100 Subject: [PATCH] Squashed commit of the following: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit 3559f2b8b82bc22df6c8ed7efa4da0f3a201f174 Author: Pıεяяε <47398145+AiroPi@users.noreply.github.com> Date: Sun Mar 24 00:30:12 2024 +0100 Add toml type info for the config file (#145) Co-authored-by: Nicolas Paul commit f5b2eb5dc5e922eaa7d2c26714f5824c6c095dc8 Author: Pıεяяε <47398145+AiroPi@users.noreply.github.com> Date: Sun Mar 24 00:17:53 2024 +0100 Improve the debbugging (#146) - 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) --- .github/CONTRIBUTING.md | 134 +++++++++++++++++++++++++++------------- Dockerfile | 2 +- compose.yml | 1 + 3 files changed, 93 insertions(+), 44 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index c262cb2..40ce8db 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -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: -```json -"preLaunchTask": "bot up", -"postDebugTask": "bot restart", -``` -And in `.vscode/tasks.json`, add the tasks: +And a `.vscode/tasks.json` file with: ```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 @@ -117,16 +165,16 @@ First, a `.env` file with the following values: Then, create a `config.toml` ([TOML](https://toml.io/en/)) with the following values: -| Key | Description | -|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------| -| `support_guild_id` | The bot needs to be member and administrator of this guild | -| `bot_id` | Used for top.gg (if enabled) | -| `bot_name` | Used for the webhook logs | -| `owners_ids` | Grant permissions to these users (e.g. eval command, extensions reloading...) reload... | -| `translator_services` | A list of translations services to enable. Names will be imported from [`cogs.translate.adapters`](/src/cogs/translate/adapters/) | -| `extensions` | A list of extensions to enable. Names will be imported from [`cogs`](/src/cogs/) | - -## Extra informations +| Key | Type | Description | +|-----------------------|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------| +| `support_guild_id` | [Integer](https://toml.io/en/v1.0.0#integer) | The bot needs to be member and administrator of this guild | +| `bot_id` | [Integer](https://toml.io/en/v1.0.0#integer) | Used for top.gg (if enabled) | +| `bot_name` | [String](https://toml.io/en/v1.0.0#string) | Used for the webhook logs | +| `owners_ids` | [Array](https://toml.io/en/v1.0.0#array) of [Integer](https://toml.io/en/v1.0.0#integer) | Grant permissions to these users (e.g. eval command, extensions reloading...) | +| `translator_services` | [Array](https://toml.io/en/v1.0.0#array) of [String](https://toml.io/en/v1.0.0#string) | A list of translations services to enable. Names will be imported from [`cogs.translate.adapters`](/src/cogs/translate/adapters/) | +| `extensions` | [Array](https://toml.io/en/v1.0.0#array) of [String](https://toml.io/en/v1.0.0#string) | A list of extensions to enable. Names will be imported from [`cogs`](/src/cogs/) | + +## Extra information In the project structure, `main.py` serves as the entry point executed by Docker. It provides a compact CLI application with various options that can be used with pre-created shell files in the `bin/` directory. `mybot.py` is the base of MyBot, containing the `MyBot` class, instantiated once at launch and available in many places in the code. diff --git a/Dockerfile b/Dockerfile index de9e721..da0ac3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/compose.yml b/compose.yml index 376b3b3..548cc13 100644 --- a/compose.yml +++ b/compose.yml @@ -2,6 +2,7 @@ version: '3' services: mybot: + init: true image: airopi/mybot:stable build: dockerfile: ./Dockerfile