awx/tools/ansible/roles/dockerfile/files/wait-for-migrations
Christian M. Adams 5ffffebe34
Attempt to check/wait for migrations 30x (~12 min)
- the task container needs to wait longer for migrations to complete for fresh installs before starting services
  - otherwise, services start prematurely and clutter the logs with errors because migrations are mid-flight
2021-06-28 10:51:03 -04:00

52 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
set -o errexit
set -o nounset
readonly CMDNAME=$(basename "$0")
readonly MIN_SLEEP=0.5
readonly MAX_SLEEP=30
readonly ATTEMPTS=30
readonly TIMEOUT=60
log_message() { echo "[${CMDNAME}]" "$@" >&2; }
log_error() { echo "[${CMDNAME}] ERROR:" "$@" >&2; }
# Args: last_sleep
next_sleep() {
awk "BEGIN {ns = ${1} * 2; ns = ns > ${MAX_SLEEP} ? ${MAX_SLEEP} : ns; print(ns)}"
}
wait_for() {
local rc=1
local attempt=1
local next_sleep="${MIN_SLEEP}"
while true; do
log_message "Attempt ${attempt} of ${ATTEMPTS}"
timeout "${TIMEOUT}" \
/bin/bash -c "! awx-manage showmigrations | grep '\[ \]'" &>/dev/null \
&& return || rc=$?
(( ++attempt > ATTEMPTS )) && break
log_message "Waiting ${next_sleep} seconds before next attempt"
sleep "${next_sleep}"
next_sleep=$(next_sleep ${next_sleep})
done
return $rc
}
main() {
log_message "Waiting for database migrations..."
if ! wait_for "$@"; then
log_message "ERROR: Database migrations not applied"
exit 1
fi
}
main "$@"