#! /bin/bash

# check running

echo "初始化数据库迁移文件"
python ./src/backend/production_manage.py makemigrations
if [ $? -ne 0 ]; then
    exit 1
fi
echo "初始化数据库迁移文件成功"
echo "执行数据库迁移"
python ./src/backend/production_manage.py migrate
if [ $? -ne 0 ]; then
    exit 1
fi
echo "执行数据库迁移成功"
echo "启动服务"

gunicorn src.backend.etc.wsgi.wsgi:application --config=./src/backend/etc/gunicorn/config.gunicorn.py &
sleep 3
port=`ps -ef | grep -v grep | grep gunicorn | wc -w`

if [ $port -eq 0 ]; then
    echo "启动服务失败"
    exit 1
else
    echo "启动服务成功"
    python ./src/backend/check_running.py &
    if [ $? -ne 0 ]; then
        echo "swagger 启动异常"
        exit 1
    else
        echo "swagger 启动成功"
    fi
fi
