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

Commit 591c664

Browse files
authored
feat: Unify wait-ready with check.sh; implement mongo wait (#1199)
- 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
1 parent 2eb7161 commit 591c664

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

check.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,39 @@ run_check() {
4343
local cmd="$3"
4444
echo "> $cmd"
4545
set +e # Disable exit-on-error
46-
if $cmd; then # Run the command itself and check if it succeeded.
46+
if bash -c "$cmd"; then # Run the command itself and check if it succeeded.
4747
succeeded="$succeeded $check_name"
4848
else
49-
docker compose logs "$service"
49+
docker compose logs --tail 30 "$service" # Just show recent logs, not all history
5050
failed="$failed $check_name"
5151
fi
5252
set -e # Re-enable exit-on-error
5353
echo # Newline
5454
}
5555

56+
mysql_run_check() {
57+
container_name="$1"
58+
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
59+
run_check "${container_name}_query" "$container_name" \
60+
"docker compose exec -T $(printf %q "$container_name") mysql -uroot -se $(printf %q "$mysql_probe")"
61+
}
62+
63+
if should_check mysql57; then
64+
echo "Checking MySQL 5.7 query endpoint:"
65+
mysql_run_check mysql57
66+
fi
67+
68+
if should_check mysql80; then
69+
echo "Checking MySQL 8.0 query endpoint:"
70+
mysql_run_check mysql80
71+
fi
72+
73+
if should_check mongo; then
74+
echo "Checking MongoDB status:"
75+
run_check mongo_status mongo \
76+
"docker compose exec -T mongo mongo --eval \"db.serverStatus()\""
77+
fi
78+
5679
if should_check registrar; then
5780
echo "Checking Registrar heartbeat:"
5881
run_check registrar_heartbeat registrar \
@@ -64,15 +87,17 @@ if should_check lms; then
6487
run_check lms_heartbeat lms \
6588
"curl --fail -L http://localhost:18000/heartbeat"
6689

67-
echo "Checking CMS heartbeat:"
68-
run_check cms_heartbeat lms \
69-
"curl --fail -L http://localhost:18010/heartbeat"
70-
7190
echo "Validating LMS volume:"
7291
run_check lms_volume lms \
7392
"make validate-lms-volume"
7493
fi
7594

95+
if should_check cms; then
96+
echo "Checking CMS heartbeat:"
97+
run_check cms_heartbeat cms \
98+
"curl --fail -L http://localhost:18010/heartbeat"
99+
fi
100+
76101
if should_check ecommerce; then
77102
echo "Checking ecommerce health:"
78103
run_check ecommerce_heartbeat ecommerce \

provision.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,7 @@ docker compose exec -T mysql80 bash -e -c "mysql -uroot mysql" < provision-mysql
160160
if needs_mongo "$to_provision_ordered"; then
161161
echo -e "${GREEN}Waiting for MongoDB...${NC}"
162162
# mongo container and mongo process/shell inside the container
163-
until docker compose exec -T mongo mongo --eval "db.serverStatus()" &> /dev/null
164-
do
165-
printf "."
166-
sleep 1
167-
done
163+
./wait-ready.sh mongo
168164
echo -e "${GREEN}MongoDB ready.${NC}"
169165
echo -e "${GREEN}Creating MongoDB users...${NC}"
170166
docker compose exec -T mongo bash -e -c "mongo" < mongo-provision.js

wait-ready.sh

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,10 @@ if [[ $# == 0 ]]; then
1515
exit 0
1616
fi
1717

18-
function wait_db {
19-
container_name="$1"
20-
mysql_probe="SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'root')"
21-
until docker compose exec -T "$container_name" mysql -uroot -se "$mysql_probe" &> /dev/null; do
18+
for service_name in "$@"; do
19+
until ./check.sh "$service_name" >/dev/null 2>&1; do
2220
printf "." >&2
2321
sleep 1
2422
done
25-
echo >&2 "$container_name is ready"
26-
}
27-
28-
29-
for service_name in "$@"; do
30-
case "$service_name" in
31-
mysql*)
32-
wait_db "$service_name"
33-
;;
34-
# TODO: Add other services...
35-
*)
36-
echo >&2 "Unknown service: $service_name"
37-
exit 1
38-
;;
39-
esac
23+
echo >&2 "$service_name is ready"
4024
done

0 commit comments

Comments
 (0)