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

Config: Add debugpy support and update Docker configuration #1356

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

drikusroor
Copy link
Contributor

@drikusroor drikusroor commented Nov 8, 2024

Integrate debugpy for remote debugging and adjust Docker settings to facilitate its use, including necessary configurations in the Dockerfile and Docker Compose. It will allow you to do remote debugging from VS Code, using the Python and/or Python Debugger extensions.

It adds a Docker image variant for the backend; the debug image. You can (temporarily) configure your docker-compose.yaml file to use this image instead of the base image (here). This image starts up a debugging server on port 5678 next to the Django server (that runs on port 8000).

Then, with the use of the (now un-ignored by .gitignore) launch.json file, you can attach VS Code to the debugging server that is now running on 5678.

In one of the backend's python files, place a breakpoint() somewhere where you want the code execution to break. Start VS Code's debugging by pressing F5 on your keyboard and open the part of the MUSCLE application that executes the code where you placed a breakpoint. In my case, I added a breakpoint to the next_round method in session/views.py (1). The frontend clearly cannot load the next round yet (the code execution is interrupted):

image

My VS Code then allows me to resume code execution (2), step in/out of deeper executions, or allows me to see local and global variables(3):

image

@drikusroor drikusroor self-assigned this Nov 8, 2024
docker-compose.yaml Outdated Show resolved Hide resolved
Copy link
Collaborator

@BeritJanssen BeritJanssen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally fine with just using vanilla pdb, but anything that makes dev life easier is certainly worth adding. I do hope there's a way to add this without having to adjust the docker-compose file every time we want to run the debug server. BTW, I would expect the current state of docker-compose.yaml (i.e., having the backend image set to base and calling the debugpy function) to not run?

@drikusroor
Copy link
Contributor Author

I'm personally fine with just using vanilla pdb, but anything that makes dev life easier is certainly worth adding. I do hope there's a way to add this without having to adjust the docker-compose file every time we want to run the debug server. BTW, I would expect the current state of docker-compose.yaml (i.e., having the backend image set to base and calling the debugpy function) to not run?

That's right, the debugpy server won't run by default in this scenario. Although we could configure it like that, I think it would cause an unnecessary performance hit / resource hog to the dev environment.

command: >
bash -c "python manage.py migrate &&
python manage.py bootstrap &&
${DEBUG_COMMAND:-python manage.py runserver 0.0.0.0:8000}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this part runs the debug command as configured in your .env file if configured and runs the normal startup command if there is no debug command configured.

@drikusroor drikusroor force-pushed the feat/debug branch 2 times, most recently from b9145d4 to fa1c44e Compare November 12, 2024 08:33
@drikusroor drikusroor changed the title Config: Add debugpy support and update Docker configuration Config: Add debugpy support and update Docker configuration Nov 12, 2024
Comment on lines +40 to +44
if not is_debugpy_running(5678):
print("Starting debugpy on port 5678 🐛")
debugpy.listen(("0.0.0.0", 5678))
else:
print("Debugpy is already running on port 5678 👍")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check has been added since the django server (but not the container) will restart once you change a file. The debug server will spawn a separate process as far as I understood and won't get terminated whenever the django server restarts.

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

Successfully merging this pull request may close these issues.

2 participants