Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
feat: Unify wait-ready with check.sh; implement mongo wait (#1199)
Browse files Browse the repository at this point in the history
- Move MySQL readiness checks from wait-ready.sh to existing check.sh,
  and call check.sh for any service we want to wait for
- Add Mongo readiness check to check.sh, and use it from provisioning
- Give cms a separate check from lms
- Change how check commands are executed; now that some of the
  commands have a space in them, we have to ensure that quoted or
  escaped spaces are interpreted properly. Using a bash -c invocation
  fixes this.
- Limit docker log output to 30 lines when check fails
  • Loading branch information
timmc-edx authored Oct 5, 2023
1 parent 2eb7161 commit 591c664
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
37 changes: 31 additions & 6 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,39 @@ run_check() {
local cmd="$3"
echo "> $cmd"
set +e # Disable exit-on-error
if $cmd; then # Run the command itself and check if it succeeded.
if bash -c "$cmd"; then # Run the command itself and check if it succeeded.
succeeded="$succeeded $check_name"
else
docker compose logs "$service"
docker compose logs --tail 30 "$service" # Just show recent logs, not all history
failed="$failed $check_name"
fi
set -e # Re-enable exit-on-error
echo # Newline
}

mysql_run_check() {
container_name="$1"
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
run_check "${container_name}_query" "$container_name" \
"docker compose exec -T $(printf %q "$container_name") mysql -uroot -se $(printf %q "$mysql_probe")"
}

if should_check mysql57; then
echo "Checking MySQL 5.7 query endpoint:"
mysql_run_check mysql57
fi

if should_check mysql80; then
echo "Checking MySQL 8.0 query endpoint:"
mysql_run_check mysql80
fi

if should_check mongo; then
echo "Checking MongoDB status:"
run_check mongo_status mongo \
"docker compose exec -T mongo mongo --eval \"db.serverStatus()\""
fi

if should_check registrar; then
echo "Checking Registrar heartbeat:"
run_check registrar_heartbeat registrar \
Expand All @@ -64,15 +87,17 @@ if should_check lms; then
run_check lms_heartbeat lms \
"curl --fail -L http://localhost:18000/heartbeat"

echo "Checking CMS heartbeat:"
run_check cms_heartbeat lms \
"curl --fail -L http://localhost:18010/heartbeat"

echo "Validating LMS volume:"
run_check lms_volume lms \
"make validate-lms-volume"
fi

if should_check cms; then
echo "Checking CMS heartbeat:"
run_check cms_heartbeat cms \
"curl --fail -L http://localhost:18010/heartbeat"
fi

if should_check ecommerce; then
echo "Checking ecommerce health:"
run_check ecommerce_heartbeat ecommerce \
Expand Down
6 changes: 1 addition & 5 deletions provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ docker compose exec -T mysql80 bash -e -c "mysql -uroot mysql" < provision-mysql
if needs_mongo "$to_provision_ordered"; then
echo -e "${GREEN}Waiting for MongoDB...${NC}"
# mongo container and mongo process/shell inside the container
until docker compose exec -T mongo mongo --eval "db.serverStatus()" &> /dev/null
do
printf "."
sleep 1
done
./wait-ready.sh mongo
echo -e "${GREEN}MongoDB ready.${NC}"
echo -e "${GREEN}Creating MongoDB users...${NC}"
docker compose exec -T mongo bash -e -c "mongo" < mongo-provision.js
Expand Down
22 changes: 3 additions & 19 deletions wait-ready.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@ if [[ $# == 0 ]]; then
exit 0
fi

function wait_db {
container_name="$1"
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
until docker compose exec -T "$container_name" mysql -uroot -se "$mysql_probe" &> /dev/null; do
for service_name in "$@"; do
until ./check.sh "$service_name" >/dev/null 2>&1; do
printf "." >&2
sleep 1
done
echo >&2 "$container_name is ready"
}


for service_name in "$@"; do
case "$service_name" in
mysql*)
wait_db "$service_name"
;;
# TODO: Add other services...
*)
echo >&2 "Unknown service: $service_name"
exit 1
;;
esac
echo >&2 "$service_name is ready"
done

0 comments on commit 591c664

Please sign in to comment.