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

feat: Reload support for backend, changes to entrypoint script #1100

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ docker/*.env
!docker/sample*.env
docker/public_tools.json
docker/proxy_overrides.yaml
docker/compose.override.yaml
Copy link
Contributor

@hari-kuriakose hari-kuriakose Jan 31, 2025

Choose a reason for hiding this comment

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

@chandrasekharan-zipstack This might not be needed.

We can check in the actual overrides yaml themselves. It will be activated only when you pass them via the -f flag, hence mentioned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hari-kuriakose I considered adding this here since each user might be motivated to run as per their own configuration which might not apply to all. For example, I might wish to mount the workflow dir from a different path in my machine / use different ports. I'm treating this similar to proxy_overrides we do with traefik. Let me know what you think

I was also hoping to populate the sample file with different examples that people can simply uncomment and use as per their need

docker/workflow_data/

# Tool development
Expand Down
8 changes: 8 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ pdm install --dev -G lint
pdm install --prod --no-editable
```

#### Running scripts

PDM allows you to run scripts applicable within the service dir.

```bash
# List the possible scripts that can be executed
pdm run -l
```

For example to run the backend (dev mode is recommended to take advantage of gunicorn's `reload` feature)

```bash
pdm run backend --dev
```

#### Running commands

- If you plan to run the django server locally, make sure the dependent services are up (either locally or through docker compose)
Expand Down
8 changes: 8 additions & 0 deletions backend/backend/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import logging
import os
import time

from django.conf import settings
from django.core.wsgi import get_wsgi_application
Expand All @@ -15,11 +17,17 @@

load_dotenv()

logger = logging.getLogger(__name__)

os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
os.environ.get("DJANGO_SETTINGS_MODULE", "backend.settings.dev"),
)

wsgi_start_time = time.perf_counter()
django_app = get_wsgi_application()
wsgi_init_elapsed = time.perf_counter() - wsgi_start_time
logger.info(f"WSGI application initialized in {wsgi_init_elapsed:.3f} seconds")


application = start_server(django_app, f"{settings.PATH_PREFIX}/socket")
57 changes: 44 additions & 13 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
#!/usr/bin/env bash

cmd=$1
if [ "$cmd" = "migrate" ]; then
show_help() {
echo "Usage: ./entrypoint.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --migrate Perform database migrations before starting the server."
echo " --dev Run Gunicorn in development mode with --reload and reduced graceful timeout (5s)."
echo " --help, -h Show this help message and exit."
}

# Parse arguments
migrate=false
dev=false

while [[ "$#" -gt 0 ]]; do
case $1 in
--migrate) migrate=true ;;
--dev) dev=true ;;
--help|-h) show_help; exit 0 ;;
*) echo "Unknown argument: $1"; exit 1 ;;
esac
shift
done

# Perform database migration if --migrate is provided
if [ "$migrate" = true ]; then
echo "Migration initiated"
.venv/bin/python manage.py migrate
fi

# NOTE: Leaving below for reference incase required in the future
# python manage.py runserver 0.0.0.0:8000 --insecure
# NOTE updated socket threads
.venv/bin/gunicorn \
--bind 0.0.0.0:8000 \
--workers 2 \
--threads 2 \
--log-level debug \
--timeout 600 \
--access-logfile - \
backend.wsgi:application
# Configure Gunicorn based on --dev flag
gunicorn_args=(
--bind 0.0.0.0:8000
--workers 2
--threads 2
--log-level debug
--timeout 600
--access-logfile -
)

if [ "$dev" = true ]; then
echo "Running in development mode"
gunicorn_args+=(--reload --graceful-timeout 5)
else
echo "Running in production mode"
fi

# Start Gunicorn
.venv/bin/gunicorn "${gunicorn_args[@]}" backend.wsgi:application
Loading