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

chore: add param support start various apps #125

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
build:
context: .
dockerfile: Dockerfile
command: ["/usr/src/start.sh"]
command: ["/usr/src/start.sh", "-d", "$PORT"]
container_name: django-server
networks:
- default
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'
services:
django-server:
image: ghcr.io/anitrend/anitrend:latest
command: ["/usr/src/start.sh"]
command: ["/usr/src/start.sh", "-w"]
container_name: django-server
networks:
- default
Expand Down
95 changes: 78 additions & 17 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,85 @@
#!/usr/bin/env bash

echo "Checking and starting migrations"
python manage.py makemigrations
python manage.py migrate

if [ "$1" = "--debug" ]; then
echo 'Collecting static files...'
python manage.py collectstatic --no-input
fi

echo "Starting qcluster"
python manage.py qcluster &

echo "Starting server"
if [ "$1" = "--debug" ]; then
python manage.py runserver 0.0.0.0:8800
else
set -e

DEBUG_MODE=false

usage() {
echo "Unrecognised option: -$OPTARG" >&2
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h Display this help message"
echo " -d Run server in debug mode"
echo " -w Start work scheduler"
}

migrations() {
echo "Checking and starting migrations"
python manage.py makemigrations
python manage.py migrate
}

cluster() {
echo "Starting qcluster"
python manage.py qcluster &
}

collect_static() {
echo 'Collecting static files...'
python manage.py collectstatic --no-input
}

start_dev_server() {
python manage.py runserver "0.0.0.0:$PORT"
}

start_prod_server() {
gunicorn "$APP_NAME.wsgi:application" \
--bind "0.0.0.0:$GUNICORN_PORT" \
--workers "$GUNICORN_WORKERS" \
--timeout "$GUNICORN_TIMEOUT" \
--log-level "$GUNICORN_LOG_LEVEL"
fi
}

start_service() {
migrations

if $Q_CLUSTER; then
cluster
fi

echo "Starting server"
if $DEBUG_MODE; then
collect_static
start_dev_server
else
start_prod_server
fi
}

while getopts ":wd:h" opt; do
case ${opt} in
w)
Q_CLUSTER=true
;;
d)
DEBUG_MODE=true
PORT="$OPTARG"
;;
h)
usage
exit 0
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
\?)
usage
exit 1
;;
esac
done
shift $((OPTIND -1))

start_service
Loading