Skip to content

Commit 5e91dd6

Browse files
Kyu-seokclaude
andauthored
dev-status and drop: say when Kafka or Redis is stopped (#415) (#419)
* dev-status: flag a stopped Postgres, Kafka or Redis container (#415) One line per container that isn't running, with what fails and the compose command that starts it; a Docker daemon that doesn't answer is one line for all three. container_state in dev-servers-common.sh is now the one place that tells the daemon from a container, and db_down_reason reads it. A stopped container keeps the OK line away; the exit code is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * dev-branch-servers: drop skips a stopped Kafka or Redis (#415) drop asks once whether each of the Kafka and the Redis container runs and skips that cleanup with one line, where it printed one docker error per call; the database is still dropped and the exit code is unchanged, so ship-branch still finishes. start's refusal names the container and the compose command that starts it, from the same container_state. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * dev-branch-servers: a skipped cleanup says what is left behind (#415) 'not dropped' read as handled, but after the database is gone no drop finds the topics or the Redis index again (#418). Review follow-up. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent b10bfb0 commit 5e91dd6

6 files changed

Lines changed: 259 additions & 28 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ source scripts/dev-env.sh && SPRING_PROFILES_ACTIVE=dev ./gradlew :backend:bootR
6969
```
7070
Pre-commit is the full-suite run (~10 min) for `backend/`: commit in the background.
7171
`scripts/dev-env.sh` loads only the OAuth and GitHub App values from `infra/.env` (from a worktree, the main checkout's). That file is the production server's config: sourcing it whole overrides the dev CORS origins, JWT secret, database and Judge0 URL.
72-
When the local app answers 403, acts like old code, doesn't show a change, or a button "does nothing", run `scripts/dev-status.sh` from the checkout or worktree whose code you expect. It flags a backend, worker or frontend running another checkout's code, a process older than its source (bootRun doesn't reload) or carrying production values, a database missing this checkout's migrations, a migration number another branch already ran under another name, a row of the gitignored `docs/PROBLEMS_MAPPING.md` that disagrees with the database, and a Judge0 that doesn't answer (every Run and Submit then fails, so check it before planning a UI check).
72+
When the local app answers 403, acts like old code, doesn't show a change, or a button "does nothing", run `scripts/dev-status.sh` from the checkout or worktree whose code you expect. It flags a backend, worker or frontend running another checkout's code, a process older than its source (bootRun doesn't reload) or carrying production values, a `codebite-postgres`, `codebite-kafka` or `codebite-redis` container that isn't running (or a Docker daemon that doesn't answer), a database missing this checkout's migrations, a migration number another branch already ran under another name, a row of the gitignored `docs/PROBLEMS_MAPPING.md` that disagrees with the database, and a Judge0 that doesn't answer (every Run and Submit then fails, so check it before planning a UI check).
7373
To see a branch's backend changes (migrations, endpoints, problems) or check its UI end to end, run `scripts/dev-branch-servers.sh` from its worktree instead of restarting or mocking the shared servers: that checkout's backend, worker and Vite on free ports, with a database, Kafka topics and Redis index of their own. From an agent, `start --detach`, then wait with `until scripts/dev-branch-servers.sh status >/dev/null; do sleep 2; done`. Its header lists the subcommands (`restart` after a new migration, `stop`, `drop`, `orphans`).
7474
To sign in locally without OAuth (dev profile only), `POST /api/auth/test-token?username=<name>&role=ADMIN` returns a JWT; `role` (USER or ADMIN) also switches an existing user; left out, it keeps the role (new users are USER). An ADMIN token checks admin pages against real data. Check the UI from a session of your own: the Vite `scripts/dev-branch-servers.sh` prints (`127.0.0.1` at an unusual port), signed in by a test token from the branch backend. `localhost:5173`/`5174` and the user's Chrome tabs hold their real signed-in sessions, drafts and settings. Headless Chrome over CDP, as `scripts/walkthrough-app-check.mjs` drives it, needs neither: its own profile, any width by `Emulation.setDeviceMetricsOverride`, and screenshots to files. In the user's Chrome, `resize_window` may leave the window as it was: for phone width open a page of that Vite and replace its body with a 390px-wide `<iframe>` of the route; media queries follow the iframe, and it shares the page's sign-in and localStorage.
7575

‎scripts/dev-branch-servers.sh‎

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# migrations on it, starting no server: for reading a problem
1515
# (CODEBITE_DB=<it> scripts/problem-text.sh) as the branch has it
1616
# scripts/dev-branch-servers.sh drop drop the branch database, Kafka topics and groups, Redis index
17+
# (a Kafka or Redis container that isn't running: says what is left behind, skips it)
1718
# scripts/dev-branch-servers.sh orphans list the branch databases whose worktree is gone
1819
# scripts/dev-branch-servers.sh drop [<name>] with a name, drop one of those, from any checkout
1920
# scripts/dev-branch-servers.sh name print the branch database name
@@ -414,6 +415,31 @@ require_postgres() {
414415
|| { echo "codebite-postgres is not running: docker compose -f infra/docker-compose.yml up -d" >&2; exit 1; }
415416
}
416417

418+
# Exits, saying what to start, unless the Kafka and the Redis container both run; a Docker daemon
419+
# that doesn't answer is said once.
420+
require_kafka_and_redis() {
421+
local container down refused=false
422+
for container in codebite-kafka codebite-redis; do
423+
down=$(container_down_reason "$container")
424+
[[ -z "$down" ]] && continue
425+
echo "dev-branch-servers.sh: $down" >&2
426+
refused=true
427+
[[ "$down" == "$(docker_down_message)" ]] && break
428+
done
429+
if $refused; then exit 1; fi
430+
}
431+
432+
# Whether container $1 runs, for a cleanup that needs it; when it doesn't, says in one line that
433+
# $2 is left as it is, and returns 1.
434+
container_runs_or_says() {
435+
case "$(container_state "$1")" in
436+
running) return 0 ;;
437+
stopped) echo "$1 isn't running: $2" >&2 ;;
438+
*) echo "the Docker daemon doesn't answer: $2" >&2 ;;
439+
esac
440+
return 1
441+
}
442+
417443
# Creates the branch database, marked this worktree's, when it doesn't exist ($1 says what
418444
# migrates it); exits when it exists but is another worktree's.
419445
ensure_database() {
@@ -455,10 +481,7 @@ cmd_start() {
455481
exit 1
456482
fi
457483
require_postgres
458-
if ! docker exec codebite-kafka true || ! docker exec codebite-redis true; then
459-
echo "codebite-kafka or codebite-redis is not running" >&2
460-
exit 1
461-
fi
484+
require_kafka_and_redis
462485
ensure_database "the backend migrates it on start"
463486

464487
local redis_db
@@ -629,20 +652,25 @@ drop_database() {
629652
}
630653

631654
# Deletes $db's Kafka topics and groups, flushes its Redis index and removes its state and port
632-
# claims: everything but the database.
655+
# claims: everything but the database. A container that doesn't run is asked once and its cleanup
656+
# skipped with one line, so the rest still happens and the drop still succeeds.
633657
drop_branch_data() {
634658
local topics groups g
635-
topics=$(kafka_bin kafka-topics.sh --list | grep "^$prefix" || true)
636-
if [[ -n "$topics" ]]; then
637-
kafka_bin kafka-topics.sh --delete --topic "$(echo "$topics" | paste -sd, -)" >/dev/null
638-
echo "Kafka topics: deleted $(echo "$topics" | paste -sd' ' -)"
659+
if container_runs_or_says codebite-kafka "topics and groups prefixed ${prefix%-} left behind, and no later drop finds them"; then
660+
topics=$(kafka_bin kafka-topics.sh --list | grep "^$prefix" || true)
661+
if [[ -n "$topics" ]]; then
662+
kafka_bin kafka-topics.sh --delete --topic "$(echo "$topics" | paste -sd, -)" >/dev/null
663+
echo "Kafka topics: deleted $(echo "$topics" | paste -sd' ' -)"
664+
fi
665+
groups=$(kafka_bin kafka-consumer-groups.sh --list 2>/dev/null | grep "^$prefix" || true)
666+
for g in $groups; do
667+
kafka_bin kafka-consumer-groups.sh --delete --group "$g" >/dev/null 2>&1 || true
668+
done
669+
if [[ -n "$groups" ]]; then echo "Kafka groups: deleted $(echo "$groups" | paste -sd' ' -)"; fi
670+
fi
671+
if container_runs_or_says codebite-redis "the Redis index of $db left claimed, and no later drop finds it"; then
672+
flush_owned_redis_index
639673
fi
640-
groups=$(kafka_bin kafka-consumer-groups.sh --list 2>/dev/null | grep "^$prefix" || true)
641-
for g in $groups; do
642-
kafka_bin kafka-consumer-groups.sh --delete --group "$g" >/dev/null 2>&1 || true
643-
done
644-
[[ -n "$groups" ]] && echo "Kafka groups: deleted $(echo "$groups" | paste -sd' ' -)"
645-
flush_owned_redis_index
646674
release_ports
647675
rm -rf "$state"
648676
}

‎scripts/dev-branch-servers.test.sh‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ kafka_bin() {
277277
}
278278
flush_owned_redis_index() { echo "redis flush $db" >> "$fake/calls"; }
279279
branch_pids() { :; }
280+
container_state() { echo running; }
280281
check "orphans lists a database whose worktree is gone" "$(orphan_databases)" "codebite_gone_1 $fake/gone-wt"
281282
out=$(cmd_orphans)
282283
[[ "$out" == *codebite_gone_1* && "$out" != *codebite_live_2* ]] || { echo "FAIL: orphans printed: $out"; fail=1; }
@@ -295,12 +296,61 @@ calls=$(cat "$fake/calls")
295296
[[ "$calls" == *"--delete --topic codebite_gone_1-submission-events"* && "$calls" != *"topic codebite_live_2"* ]] \
296297
|| { echo "FAIL: drop should delete only the orphan's topics: $calls"; fail=1; }
297298
[[ "$calls" == *"redis flush codebite_gone_1"* ]] || { echo "FAIL: drop should flush the orphan's Redis index: $calls"; fail=1; }
299+
# Kafka and Redis stopped: the database is still dropped, and each skipped cleanup is one line,
300+
# with no docker call that could only print "container ... is not running".
301+
container_state() { case "$1" in codebite-postgres) echo running ;; *) echo stopped ;; esac; }
302+
# The script's own kafka_bin and Redis functions again, over a docker that only fails; the other
303+
# fakes stay.
304+
kept=$(declare -f psql_admin branch_pids container_state)
305+
# shellcheck source=SCRIPTDIR/dev-branch-servers.sh
306+
source "$script"
307+
eval "$kept"
308+
docker() { echo "docker $*" >> "$fake/calls"; echo "Error response from daemon: container 3f2a9c is not running" >&2; return 1; }
309+
mkdir -p "$state_base/codebite_gone_1"
310+
: > "$fake/calls"
311+
out=$(cmd_drop_orphan codebite_gone_1 2>&1) && code=0 || code=$?
312+
check "drop with Kafka and Redis stopped still succeeds" "$code" 0
313+
check "drop with Kafka and Redis stopped says what it skipped" "$out" "database codebite_gone_1: dropped
314+
codebite-kafka isn't running: topics and groups prefixed codebite_gone_1 left behind, and no later drop finds them
315+
codebite-redis isn't running: the Redis index of codebite_gone_1 left claimed, and no later drop finds it"
316+
calls=$(cat "$fake/calls")
317+
[[ "$calls" == *'DROP DATABASE IF EXISTS "codebite_gone_1"'* ]] || { echo "FAIL: the database wasn't dropped with Kafka and Redis stopped: $calls"; fail=1; }
318+
[[ "$calls" != *docker* ]] || { echo "FAIL: drop called docker on a stopped container: $calls"; fail=1; }
319+
[[ ! -d "$state_base/codebite_gone_1" ]] || { echo "FAIL: drop with Kafka and Redis stopped left its state directory"; fail=1; }
320+
# Only Redis stopped: Kafka is still cleaned.
321+
container_state() { case "$1" in codebite-redis) echo stopped ;; *) echo running ;; esac; }
322+
: > "$fake/calls"
323+
out=$(cmd_drop_orphan codebite_gone_1 2>&1) && code=0 || code=$?
324+
[[ "$out" == *"codebite-redis isn't running"* && "$out" != *"codebite-kafka isn't running"* && "$(cat "$fake/calls")" == *"docker exec codebite-kafka"* ]] \
325+
|| { echo "FAIL: with only Redis stopped, Kafka should still be cleaned: $out / $(cat "$fake/calls")"; fail=1; }
326+
# A Docker daemon that doesn't answer is named as such.
327+
container_state() { echo daemon; }
328+
out=$(cmd_drop_orphan codebite_gone_1 2>&1) && code=0 || code=$?
329+
[[ "$out" == *"the Docker daemon doesn't answer: topics and groups prefixed codebite_gone_1 left behind, and no later drop finds them"* ]] \
330+
|| { echo "FAIL: drop with the daemon down should say so: $out"; fail=1; }
331+
unset -f docker
332+
333+
# start refuses, saying what to start, when Kafka or Redis doesn't run; a daemon that doesn't
334+
# answer is said once.
335+
container_state() { case "$1" in codebite-postgres) echo running ;; *) echo stopped ;; esac; }
336+
out=$( (require_kafka_and_redis) 2>&1) && code=0 || code=$?
337+
check "start refused with Kafka and Redis stopped" "$code" 1
338+
check "start names what to start" "$out" "dev-branch-servers.sh: the codebite-kafka container isn't running; start it: docker compose -f infra/docker-compose.yml up -d kafka
339+
dev-branch-servers.sh: the codebite-redis container isn't running; start it: docker compose -f infra/docker-compose.yml up -d redis"
340+
container_state() { echo daemon; }
341+
out=$( (require_kafka_and_redis) 2>&1) && code=0 || code=$?
342+
check "start refused with Docker down" "$code" 1
343+
check "start names Docker once" "$out" "dev-branch-servers.sh: the Docker daemon doesn't answer; start Docker Desktop (open -a Docker) and rerun"
344+
container_state() { echo running; }
345+
out=$( (require_kafka_and_redis) 2>&1) && code=0 || code=$?
346+
check "start goes on with Kafka and Redis running" "$code/$out" "0/"
347+
298348
branch_pids() { echo 301; }
299349
: > "$fake/calls"
300350
out=$(cmd_drop_orphan codebite_gone_1 2>&1) && code=0 || code=$?
301351
check "drop by name refused while its servers run" "$code" 1
302352
[[ "$(cat "$fake/calls")" != *DROP* ]] || { echo "FAIL: dropped an orphan whose servers run"; fail=1; }
303-
unset -f psql_admin kafka_bin flush_owned_redis_index branch_pids
353+
unset -f psql_admin branch_pids container_state
304354
# shellcheck source=SCRIPTDIR/dev-branch-servers.sh
305355
source "$script"
306356

‎scripts/dev-servers-common.sh‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,46 @@ env_value() {
2727
{ ps eww -o command= -p "$1" 2>/dev/null || true; } | tr ' ' '\n' | sed -n "s/^$2=//p" | head -n 1
2828
}
2929

30-
# Why no database in the codebite-postgres container can be reached, as what to start; empty when
31-
# the container runs. One docker inspect then, and a docker info only to tell a daemon that doesn't
32-
# answer from a container that is stopped or was never created. Reads nothing from stdin, which may
33-
# hold the caller's query.
34-
db_down_reason() {
30+
# The one place that tells a Docker daemon that doesn't answer from a container that doesn't run.
31+
# Prints what container $1 (codebite-postgres, codebite-kafka, codebite-redis) is: running,
32+
# stopped (stopped, or never created) or daemon (the Docker daemon doesn't answer, so no container
33+
# runs). One docker inspect, and a docker info only when that doesn't say running. Reads nothing
34+
# from stdin, which may hold the caller's query.
35+
container_state() {
3536
local running
36-
running=$(docker inspect -f '{{.State.Running}}' codebite-postgres 2>/dev/null </dev/null) || true
37+
running=$(docker inspect -f '{{.State.Running}}' "$1" 2>/dev/null </dev/null) || true
3738
if [[ "$running" == true ]]; then
38-
return 0
39+
echo running
3940
elif docker info >/dev/null 2>&1 </dev/null; then
40-
echo "the codebite-postgres container isn't running; start it: docker compose -f infra/docker-compose.yml up -d postgres"
41+
echo stopped
4142
else
42-
echo "the Docker daemon doesn't answer; start Docker Desktop (open -a Docker) and rerun"
43+
echo daemon
4344
fi
4445
}
4546

47+
# The command that starts container $1: its compose service is its name without the codebite- prefix.
48+
container_start_command() {
49+
echo "docker compose -f infra/docker-compose.yml up -d ${1#codebite-}"
50+
}
51+
52+
docker_down_message() {
53+
echo "the Docker daemon doesn't answer; start Docker Desktop (open -a Docker) and rerun"
54+
}
55+
56+
# Why container $1 can't be used, as what to start; empty when it runs.
57+
container_down_reason() {
58+
case "$(container_state "$1")" in
59+
running) ;;
60+
stopped) echo "the $1 container isn't running; start it: $(container_start_command "$1")" ;;
61+
*) docker_down_message ;;
62+
esac
63+
}
64+
65+
# Why no database in the codebite-postgres container can be reached; empty when the container runs.
66+
db_down_reason() {
67+
container_down_reason codebite-postgres
68+
}
69+
4670
# psql on the local database $1 (the codebite-postgres container), the rest of the arguments
4771
# psql's own; stdin goes through, so a query can arrive as a heredoc. Stops at the first error.
4872
# Runs nothing and returns 2, saying on stderr what to start, when Docker or the container is down.

‎scripts/dev-status.sh‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# change on this checkout's branch isn't in it; restart it from here
77
# STALE the source changed after the process started; bootRun doesn't reload, so restart it
88
# PROD it carries a value from the production infra/.env (see scripts/dev-env.sh)
9+
# STOPPED the codebite-postgres, codebite-kafka or codebite-redis container isn't running
10+
# DOWN the Docker daemon doesn't answer, so none of the three runs
911
# BEHIND the database lacks a migration this checkout has; the backend applies it on start
1012
# UNREACHABLE Judge0 doesn't answer, so every Run and Submit fails; known before a UI check is planned
1113
# MAPPING a row of the main checkout's gitignored docs/PROBLEMS_MAPPING.md disagrees with the database
@@ -119,14 +121,42 @@ for pid in $pids; do
119121
report_checkout "$(checkout_of "$pid" "$(app_pattern frontend)")" "$(env_value "$pid" CODEBITE_DEV_BRANCH)"
120122
done
121123

124+
# The containers the servers need, each with what fails while it doesn't run. A daemon that
125+
# doesn't answer is said once, for all three.
126+
stopped=0
127+
postgres_down=0
128+
for entry in \
129+
"codebite-postgres|no database, so the backend and worker don't start and the database scripts refuse" \
130+
"codebite-kafka|every Submit stays PENDING" \
131+
"codebite-redis|the Run and Submit cooldowns and the token blacklist fail"; do
132+
container=${entry%%|*}
133+
case "$(container_state "$container")" in
134+
running) ;;
135+
stopped)
136+
echo "$container: STOPPED ${entry#*|}; start it: $(container_start_command "$container")"
137+
stopped=1
138+
[[ "$container" == codebite-postgres ]] && postgres_down=1 ;;
139+
*)
140+
echo "docker: DOWN no Postgres, Kafka or Redis: $(docker_down_message)"
141+
stopped=1
142+
postgres_down=1
143+
break ;;
144+
esac
145+
done
146+
if [[ $stopped -eq 0 ]]; then
147+
echo "containers: codebite-postgres, codebite-kafka and codebite-redis run"
148+
fi
149+
122150
db=$(flyway_version codebite)
123151
ours=$(newest_migration "$here")
124152
if in_main_checkout; then
125153
start_hint="start the backend from here to apply them"
126154
else
127155
start_hint="the shared backend runs $main; scripts/dev-branch-servers.sh migrates a database of this branch's own"
128156
fi
129-
if [[ -z "$db" ]]; then
157+
if [[ $postgres_down -eq 1 ]]; then
158+
echo "database: not reachable (see above)"
159+
elif [[ -z "$db" ]]; then
130160
echo "database: $(db_unreachable_message)"
131161
elif (( db < ours )); then
132162
echo "database: at V$db"
@@ -200,7 +230,8 @@ if [[ -f "$mapping" && -n "$db" ]] && (( db >= ours )); then
200230
| python3 -I "$here/scripts/check-problems-mapping.py" "$mapping" || problems=1
201231
fi
202232

203-
if [[ $found -eq 1 && $problems -eq 0 ]]; then
233+
# A stopped container keeps the OK line away and leaves the exit code as the rest decides it.
234+
if [[ $found -eq 1 && $problems -eq 0 && $stopped -eq 0 ]]; then
204235
echo "OK: running code, config and database match this checkout"
205236
fi
206237
exit "$problems"

0 commit comments

Comments
 (0)