From f3db3a7e049b49167ab7d2e0931eb1b34f959597 Mon Sep 17 00:00:00 2001 From: Bob Steers Date: Wed, 24 Jan 2024 16:23:50 +0100 Subject: [PATCH 1/5] Enable faulthandler if config.debugger is set --- brewblox_history/app_factory.py | 8 ++++++++ brewblox_history/models.py | 1 + poetry.lock | 29 ++++++++++++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/brewblox_history/app_factory.py b/brewblox_history/app_factory.py index 274dfc0..7a1fa02 100644 --- a/brewblox_history/app_factory.py +++ b/brewblox_history/app_factory.py @@ -60,6 +60,14 @@ def create_app() -> FastAPI: config = utils.get_config() setup_logging(config.debug) + if config.debugger: # pragma: no cover + import faulthandler + faulthandler.enable() + + import debugpy + debugpy.listen(('0.0.0.0', 5678)) + LOGGER.info('Debugger is enabled and listening on 5678') + # Call setup functions for modules mqtt.setup() redis.setup() diff --git a/brewblox_history/models.py b/brewblox_history/models.py index 68802eb..e84bb08 100644 --- a/brewblox_history/models.py +++ b/brewblox_history/models.py @@ -40,6 +40,7 @@ class ServiceConfig(BaseSettings): name: str = 'history' debug: bool = False + debugger: bool = False mqtt_protocol: Literal['mqtt', 'mqtts'] = 'mqtt' mqtt_host: str = 'eventbus' diff --git a/poetry.lock b/poetry.lock index 314da76..adeb80b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -251,6 +251,33 @@ files = [ [package.extras] toml = ["tomli"] +[[package]] +name = "debugpy" +version = "1.8.0" +description = "An implementation of the Debug Adapter Protocol for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "debugpy-1.8.0-cp310-cp310-macosx_11_0_x86_64.whl", hash = "sha256:7fb95ca78f7ac43393cd0e0f2b6deda438ec7c5e47fa5d38553340897d2fbdfb"}, + {file = "debugpy-1.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef9ab7df0b9a42ed9c878afd3eaaff471fce3fa73df96022e1f5c9f8f8c87ada"}, + {file = "debugpy-1.8.0-cp310-cp310-win32.whl", hash = "sha256:a8b7a2fd27cd9f3553ac112f356ad4ca93338feadd8910277aff71ab24d8775f"}, + {file = "debugpy-1.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:5d9de202f5d42e62f932507ee8b21e30d49aae7e46d5b1dd5c908db1d7068637"}, + {file = "debugpy-1.8.0-cp311-cp311-macosx_11_0_universal2.whl", hash = "sha256:ef54404365fae8d45cf450d0544ee40cefbcb9cb85ea7afe89a963c27028261e"}, + {file = "debugpy-1.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:60009b132c91951354f54363f8ebdf7457aeb150e84abba5ae251b8e9f29a8a6"}, + {file = "debugpy-1.8.0-cp311-cp311-win32.whl", hash = "sha256:8cd0197141eb9e8a4566794550cfdcdb8b3db0818bdf8c49a8e8f8053e56e38b"}, + {file = "debugpy-1.8.0-cp311-cp311-win_amd64.whl", hash = "sha256:a64093656c4c64dc6a438e11d59369875d200bd5abb8f9b26c1f5f723622e153"}, + {file = "debugpy-1.8.0-cp38-cp38-macosx_11_0_x86_64.whl", hash = "sha256:b05a6b503ed520ad58c8dc682749113d2fd9f41ffd45daec16e558ca884008cd"}, + {file = "debugpy-1.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3c6fb41c98ec51dd010d7ed650accfd07a87fe5e93eca9d5f584d0578f28f35f"}, + {file = "debugpy-1.8.0-cp38-cp38-win32.whl", hash = "sha256:46ab6780159eeabb43c1495d9c84cf85d62975e48b6ec21ee10c95767c0590aa"}, + {file = "debugpy-1.8.0-cp38-cp38-win_amd64.whl", hash = "sha256:bdc5ef99d14b9c0fcb35351b4fbfc06ac0ee576aeab6b2511702e5a648a2e595"}, + {file = "debugpy-1.8.0-cp39-cp39-macosx_11_0_x86_64.whl", hash = "sha256:61eab4a4c8b6125d41a34bad4e5fe3d2cc145caecd63c3fe953be4cc53e65bf8"}, + {file = "debugpy-1.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:125b9a637e013f9faac0a3d6a82bd17c8b5d2c875fb6b7e2772c5aba6d082332"}, + {file = "debugpy-1.8.0-cp39-cp39-win32.whl", hash = "sha256:57161629133113c97b387382045649a2b985a348f0c9366e22217c87b68b73c6"}, + {file = "debugpy-1.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:e3412f9faa9ade82aa64a50b602544efcba848c91384e9f93497a458767e6926"}, + {file = "debugpy-1.8.0-py2.py3-none-any.whl", hash = "sha256:9c9b0ac1ce2a42888199df1a1906e45e6f3c9555497643a85e0bf2406e3ffbc4"}, + {file = "debugpy-1.8.0.zip", hash = "sha256:12af2c55b419521e33d5fb21bd022df0b5eb267c3e178f1d374a63a2a6bdccd0"}, +] + [[package]] name = "fastapi" version = "0.104.1" @@ -1297,4 +1324,4 @@ h11 = ">=0.9.0,<1" [metadata] lock-version = "2.0" python-versions = ">=3.11,<4" -content-hash = "333cebf394f5ea31d556932fcc7c1a3a07930eea2e4f46482240df5fb1cc9971" +content-hash = "dbbddfb9f789515eacdd85c2db2cfe1cae497a348477ed8c4e22253a2b2dafab" diff --git a/pyproject.toml b/pyproject.toml index 5c6101f..69ab250 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ pydantic-settings = "^2.1.0" fastapi-mqtt = "^2.0.0" httpx = "^0.25.2" websockets = "^12.0" +debugpy = "^1.8.0" [tool.poetry.group.dev.dependencies] pytest-cov = "*" From 26aaa6a0d24d569968899c64875910f5207e4bac Mon Sep 17 00:00:00 2001 From: Bob Steers Date: Wed, 24 Jan 2024 17:00:27 +0100 Subject: [PATCH 2/5] add debug launch file --- .vscode/launch.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..099eaba --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Remote Attach", + "type": "python", + "request": "attach", + "port": 5678, + "host": "buildbot32.local", + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "/app" + } + ] + } + ], + "compounds": [] +} \ No newline at end of file From 105f4a71afee43e83d5fc14a99f8b3fee5aa0c23 Mon Sep 17 00:00:00 2001 From: Bob Steers Date: Wed, 24 Jan 2024 17:01:15 +0100 Subject: [PATCH 3/5] set breakpoint on debugger --- brewblox_history/app_factory.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/brewblox_history/app_factory.py b/brewblox_history/app_factory.py index 7a1fa02..8e39a6c 100644 --- a/brewblox_history/app_factory.py +++ b/brewblox_history/app_factory.py @@ -67,6 +67,8 @@ def create_app() -> FastAPI: import debugpy debugpy.listen(('0.0.0.0', 5678)) LOGGER.info('Debugger is enabled and listening on 5678') + debugpy.wait_for_client() + debugpy.breakpoint() # Call setup functions for modules mqtt.setup() From a7704b40a217bb649f57c27ff8a290c094a3c7f4 Mon Sep 17 00:00:00 2001 From: Bob Steers Date: Wed, 24 Jan 2024 17:47:29 +0100 Subject: [PATCH 4/5] immediately start debugger --- brewblox_history/app_factory.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/brewblox_history/app_factory.py b/brewblox_history/app_factory.py index 8e39a6c..272a4bd 100644 --- a/brewblox_history/app_factory.py +++ b/brewblox_history/app_factory.py @@ -1,3 +1,4 @@ +import debugpy import logging from contextlib import AsyncExitStack, asynccontextmanager from pprint import pformat @@ -12,6 +13,11 @@ LOGGER = logging.getLogger(__name__) +debugpy.listen(('0.0.0.0', 5678)) +debugpy.wait_for_client() +debugpy.breakpoint() + + def setup_logging(debug: bool): level = logging.DEBUG if debug else logging.INFO unimportant_level = logging.INFO if debug else logging.WARN @@ -64,11 +70,7 @@ def create_app() -> FastAPI: import faulthandler faulthandler.enable() - import debugpy - debugpy.listen(('0.0.0.0', 5678)) LOGGER.info('Debugger is enabled and listening on 5678') - debugpy.wait_for_client() - debugpy.breakpoint() # Call setup functions for modules mqtt.setup() From b4bc845648e84593130d97ed24a9c94fb9a7fa6d Mon Sep 17 00:00:00 2001 From: Bob Steers Date: Wed, 24 Jan 2024 18:53:24 +0100 Subject: [PATCH 5/5] Revert "immediately start debugger" --- brewblox_history/app_factory.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/brewblox_history/app_factory.py b/brewblox_history/app_factory.py index 272a4bd..7a1fa02 100644 --- a/brewblox_history/app_factory.py +++ b/brewblox_history/app_factory.py @@ -1,4 +1,3 @@ -import debugpy import logging from contextlib import AsyncExitStack, asynccontextmanager from pprint import pformat @@ -13,11 +12,6 @@ LOGGER = logging.getLogger(__name__) -debugpy.listen(('0.0.0.0', 5678)) -debugpy.wait_for_client() -debugpy.breakpoint() - - def setup_logging(debug: bool): level = logging.DEBUG if debug else logging.INFO unimportant_level = logging.INFO if debug else logging.WARN @@ -70,6 +64,8 @@ def create_app() -> FastAPI: import faulthandler faulthandler.enable() + import debugpy + debugpy.listen(('0.0.0.0', 5678)) LOGGER.info('Debugger is enabled and listening on 5678') # Call setup functions for modules