File size: 1,185 Bytes
7a88b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash

set -e

# Run any necessary startup commands here, like migrations
# Create the MailPilot user if it does not exist


# Start Uvicorn with app module, host, and port from environment variables
exec uvicorn app.main:fastapi_app --host 0.0.0.0 --port 5001 --reload

DB_CHECK_INTERVAL=${DB_CHECK_INTERVAL:-5}
DB_CHECK_RETRIES=${DB_CHECK_RETRIES:-120}

pg_isready() {
  i=0
  echo -n "waiting for database connection "
  while [ ${i} -le ${DB_CHECK_RETRIES} ]; do
    python pg_isready.py && return || echo -n "."
    sleep ${DB_CHECK_INTERVAL}
    let i++
  done
}

ACTION=""
if [ $# -ge 1 ]; then
  ACTION=${1} ; shift
fi

case "${ACTION}" in

  ''|-*)
    pg_isready
    exec uvicorn ${UVICORN_APP} ${ACTION} ${@}
    ;;

    uvicorn)
    pg_isready
    exec uvicorn ${UVICORN_APP} ${@}
    ;;

    migration)
      pg_isready
      exec alembic -c app/migrations/alembic.ini upgrade head
      ;;

    pytest)
    pg_isready
    exec pytest ${@}
    ;;

    noexit)
      # used locally for docker-based development
      # so things don't shut down after the process ends/exits.
      while sleep 1000; do :; done
      ;;

    *)
      exec ${ACTION} ${@}
      ;;

esac