0

I have two tasks in task group 1) a db task to bring up a db and 2) the app that needs the db to be up.

Both start in parallel and the db tasks takes a lil bit time but by then the app recognizes that db is not up and kills the db task. Any solutions? Please advise.

Julie
  • 179
  • 2
  • 13

1 Answers1

0

It's somewhat common to have an entrypoint script that checks if the db is healthy. Here's a script i've used before:

#!/bin/sh

set -e
cmd="$*"

postgres_ready() {
    if test -z "${NO_DB}"
    then
        PGPASSWORD="${RDS_PASSWORD}" psql -h "${RDS_HOSTNAME}" -U "${RDS_USERNAME}" -d "${RDS_DB_NAME}" -c '\l'
        return $?
    else
        echo "NO_DB Postgres will pretend to be up"
        return 0
    fi
}


until postgres_ready
do
    >&2 echo "Postgres is unavailable - sleeping"
    sleep 1
done

>&2 echo "Postgres is up - continuing..."

exec "${cmd}"

You could save it as entrypoint.sh and run it with your application start script as the argument. eg: entrypoint.sh python main.py

maxm
  • 3,412
  • 1
  • 19
  • 27