#!/bin/bash

if [ "$ENV" == "production" ]; then
    export WORKERS=2
    export THREADS=2
    export RELOAD=""
else
    cd /pybook_mapped_volume
    export WORKERS=1
    export THREADS=1
    export RELOAD="--reload"
fi

# start cron as root to do historical snapshots
/usr/sbin/cron -f &

# start finplan with right amount of threads/workers, etc.
sudo -u finplan gunicorn --bind=0.0.0.0:80 --timeout 300 --workers=$WORKERS --threads=$THREADS main:app --env ENV="$ENV" --error-logfile gunicorn.error.log --access-logfile gunicorn.log --capture-output $RELOAD --enable-stdio-inheritance

# just in case it fails this keeps the container up so you can check gunicorn logs
sleep 99999
