Skip to content

Commit

Permalink
chore: add param support start various apps (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed May 1, 2024
1 parent 489818c commit 7edc98f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 19 deletions.
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
97 changes: 80 additions & 17 deletions start.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,87 @@
#!/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
Q_CLUSTER=false
PORT=""

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

0 comments on commit 7edc98f

Please sign in to comment.