Skip to content

Commit

Permalink
Add a debugger configuration for VS Code (#323)
Browse files Browse the repository at this point in the history
* Add a debugger configuration for VS Code

* Update the ENVIRONMENT env variable handling

* Enable Python warnings for debug runs too

* Rename GUNICORN_WORKERS to GUNICORN_WORKERS_COUNT
  • Loading branch information
danniel authored Sep 27, 2024
1 parent 2955ac6 commit 3ce5918
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 38 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#
# strings: Everything else


# Valid choices: "development", "debug", "debugwait"; anything else will cause it to run in production mode
ENVIRONMENT="development"

DEBUG=True
ALLOWED_HOSTS=localhost
APEX_DOMAIN=redirectioneaza.ro
Expand Down
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ blobs/
# Local History for Visual Studio Code
.history/

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
Expand Down Expand Up @@ -290,8 +293,6 @@ cython_debug/
/backend/static/
/backend/media/

/.vscode/

# Logs
logs
*.log
Expand Down Expand Up @@ -410,9 +411,6 @@ dist
# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
Expand Down
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"configurations": [
{
"name": "Attach to runserver in Docker",
"type": "debugpy",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/var/www/redirect/"
}
],
"connect": {
"port": 5678,
"host": "127.0.0.1"
}
},
{
"name": "Attach to qcluster in Docker",
"type": "debugpy",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/var/www/redirect/"
}
],
"connect": {
"port": 5677,
"host": "127.0.0.1"
}
}
]
}
4 changes: 3 additions & 1 deletion backend/requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ ruff~=0.6.4

faker~=28.4.1


# testing
pytest~=8.3.3

pytest-django~=4.9.0
pytest-xdist~=3.6.1
pytest-cov~=5.0.0

# debugging
debugpy~=1.8.6

-r requirements.txt
16 changes: 9 additions & 7 deletions backend/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements-dev.txt --strip-extras requirements-dev.in
Expand All @@ -12,11 +12,11 @@ black==24.8.0
# via -r requirements-dev.in
blessed==1.20.0
# via -r requirements.txt
boto3==1.35.20
boto3==1.35.27
# via
# -r requirements.txt
# django-ses
botocore==1.35.20
botocore==1.35.27
# via
# -r requirements.txt
# boto3
Expand Down Expand Up @@ -54,6 +54,8 @@ cssselect2==0.7.0
# via
# -r requirements.txt
# svglib
debugpy==1.8.6
# via -r requirements-dev.in
django==5.1.1
# via
# -r requirements.txt
Expand Down Expand Up @@ -93,7 +95,7 @@ faker==28.4.1
# via -r requirements-dev.in
gevent==24.2.1
# via -r requirements.txt
greenlet==3.1.0
greenlet==3.1.1
# via
# -r requirements.txt
# gevent
Expand Down Expand Up @@ -122,7 +124,7 @@ markupsafe==2.1.5
# jinja2
mypy-extensions==1.0.0
# via black
ngohub==0.0.6
ngohub==0.0.7
# via -r requirements.txt
packaging==24.1
# via
Expand All @@ -139,7 +141,7 @@ pillow==10.4.0
# reportlab
pip-tools==7.4.1
# via -r requirements-dev.in
platformdirs==4.3.3
platformdirs==4.3.6
# via black
pluggy==1.5.0
# via pytest
Expand Down Expand Up @@ -195,7 +197,7 @@ reportlab==4.2.2
# svglib
requests==2.32.3
# via -r requirements.txt
ruff==0.6.5
ruff==0.6.7
# via -r requirements-dev.in
s3transfer==0.10.2
# via
Expand Down
10 changes: 5 additions & 5 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements.txt --strip-extras requirements.in
Expand All @@ -8,11 +8,11 @@ asgiref==3.8.1
# via django
blessed==1.20.0
# via -r requirements.in
boto3==1.35.20
boto3==1.35.27
# via
# django-ses
# django-storages
botocore==1.35.20
botocore==1.35.27
# via
# boto3
# s3transfer
Expand Down Expand Up @@ -66,7 +66,7 @@ django-unfold==0.39.0
# via -r requirements.in
gevent==24.2.1
# via -r requirements.in
greenlet==3.1.0
greenlet==3.1.1
# via gevent
gunicorn==23.0.0
# via -r requirements.in
Expand All @@ -82,7 +82,7 @@ lxml==5.3.0
# via svglib
markupsafe==2.1.5
# via jinja2
ngohub==0.0.6
ngohub==0.0.7
# via -r requirements.in
packaging==24.1
# via gunicorn
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ services:
dockerfile: ./docker/dockerfiles/Dockerfile.dev
volumes:
- ./backend:/var/www/redirect/backend
environment:
- "ENVIRONMENT=development"
env_file:
- .env
ports:
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ services:
- ./backend/.db_sqlite:/var/www/redirect/backend/.db_sqlite
environment:
- "DATABASE_ENGINE=sqlite3"
ports:
- "5678:5678"
- "5677:5677"
4 changes: 2 additions & 2 deletions docker/dockerfiles/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ FROM node:${NODE_VERSION}-bookworm-slim

ENV PYTHONUNBUFFERED=1

ENV DEBUG=True
ENV ENVIRONMENT=development
ENV DEBUG=${DEBUG:-True}
ENV ENVIRONMENT=${ENVIRONMENT:-development}

ENV RUN_MIGRATIONS=True
ENV RUN_COMPILE_MESSAGES=True
Expand Down
40 changes: 26 additions & 14 deletions docker/s6-rc.d/backend/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ set -e

cd /var/www/redirect/backend/ || exit 1

if [ "${ENVIRONMENT}" = "development" ]; then
echo "Running in development mode"

python3 -Wd manage.py runserver 0.0.0.0:8000
if [ "${ENVIRONMENT}" = "debug" ]; then
echo "**********************************************"
echo "*** Web server in development DEBUG mode ***"
echo "**********************************************"
python3 -Wd -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000
elif [ "${ENVIRONMENT}" = "debugwait" ]; then
echo "***********************************************"
echo "*** Web server in development DEBUG mode ***"
echo "*** Waiting for debugger connection... ***"
echo "***********************************************"
python3 -Wd -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000
elif [ "${ENVIRONMENT}" = "development" ]; then
echo "****************************************"
echo "*** Web server in development mode ***"
echo "****************************************"
python3 -Wd manage.py runserver 0.0.0.0:8000
else
echo "Running in production mode"
echo "Running in production mode"

# https://docs.gunicorn.org/en/latest/design.html#how-many-workers
WORKERS=${GUNICORN_WORKERS:-$(((2 * $(nproc)) + 1))}
# https://docs.gunicorn.org/en/latest/design.html#how-many-workers
WORKERS=${GUNICORN_WORKERS_COUNT:-$(((2 * $(nproc)) + 1))}

echo "Starting Gunicorn with ${WORKERS} workers"
echo "Starting Gunicorn with ${WORKERS} workers"

python3 -m gunicorn redirectioneaza.wsgi \
--bind "unix:///run/gunicorn.sock" \
--log-level "${LOG_LEVEL}" \
--worker-class gevent \
--workers "${WORKERS}" \
--timeout 60
python3 -m gunicorn redirectioneaza.wsgi \
--bind "unix:///run/gunicorn.sock" \
--log-level "${LOG_LEVEL}" \
--worker-class gevent \
--workers "${WORKERS}" \
--timeout 60
fi
18 changes: 17 additions & 1 deletion docker/s6-rc.d/qcluster/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ set -e

cd /var/www/redirect/backend

python3 manage.py qcluster
if [ "${ENVIRONMENT}" = "debug" ]; then
echo "*********************************************"
echo "*** Starting the qcluster in DEBUG mode ***"
echo "*********************************************"
python3 -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5677 manage.py qcluster
elif [ "${ENVIRONMENT}" = "debugwait" ]; then
echo "*********************************************"
echo "*** Starting the qcluster in DEBUG mode ***"
echo "*** Waiting for debugger connection... ***"
echo "*********************************************"
python3 -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5677 manage.py qcluster
else
echo "Starting the qcluster in production mode"

python3 manage.py qcluster
fi

2 changes: 1 addition & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ module "ecs_redirectioneaza" {
value = 0.5
},
{
name = "GUNICORN_WORKERS"
name = "GUNICORN_WORKERS_COUNT"
value = 3
},
{
Expand Down

0 comments on commit 3ce5918

Please sign in to comment.