-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev
More file actions
executable file
·1091 lines (947 loc) · 28.6 KB
/
dev
File metadata and controls
executable file
·1091 lines (947 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$ROOT_DIR/backend"
FRONTEND_DIR="$ROOT_DIR/frontend"
LOG_DIR="$ROOT_DIR/logs"
DOCKER_COMPOSE_FILE="$ROOT_DIR/docker-compose.yml"
ENV_EXAMPLE="$BACKEND_DIR/.env.example"
ENV_FILE="$BACKEND_DIR/.env"
MONITOR_PID_FILE="$LOG_DIR/monitor.pid"
MONITOR_DAEMON_LOG_FILE="$LOG_DIR/monitor-daemon.log"
MONITOR_DAEMON_ENV_FILE="$LOG_DIR/monitor.env"
info() { printf '[INFO] %s\n' "$1"; }
ok() { printf '[OK] %s\n' "$1"; }
warn() { printf '[WARN] %s\n' "$1"; }
err() { printf '[ERROR] %s\n' "$1" >&2; }
usage() {
cat <<'EOF'
Usage: ./dev <command>
Commands:
init Validate prerequisites and bootstrap backend/.env
up Start local development environment
down Stop local infrastructure
logs Tail logs (default: app logs). Targets: app|infra|backend|frontend|celery|monitor
status Show service and endpoint status
monitor Run optional active monitor (foreground by default; --daemon supported)
migrate Run backend database migrations
seed Run optional development seed data setup
test Run backend test suite (and frontend tests if configured)
reset Reset local dev environment (asks for confirmation)
doctor Diagnose local environment issues
Examples:
./dev init
./dev up
./dev up --seed
./dev migrate
./dev seed
./dev test
./dev reset --volumes
./dev logs infra
./dev monitor
./dev monitor --daemon
./dev monitor --status
./dev monitor --stop
EOF
}
have_cmd() {
command -v "$1" >/dev/null 2>&1
}
version_ge() {
local version="$1"
local required_major="$2"
local required_minor="$3"
local major minor
major="$(printf '%s' "$version" | cut -d. -f1)"
minor="$(printf '%s' "$version" | cut -d. -f2)"
[[ "$major" =~ ^[0-9]+$ ]] || return 1
[[ "$minor" =~ ^[0-9]+$ ]] || minor=0
if (( major > required_major )); then
return 0
fi
if (( major == required_major && minor >= required_minor )); then
return 0
fi
return 1
}
resolve_python_cmd() {
if have_cmd python3; then
printf 'python3\n'
return 0
fi
if have_cmd python && python --version 2>&1 | grep -q "Python 3"; then
printf 'python\n'
return 0
fi
return 1
}
DOCKER_COMPOSE_CMD=()
resolve_docker_compose() {
if docker compose version >/dev/null 2>&1; then
DOCKER_COMPOSE_CMD=(docker compose)
return 0
fi
if have_cmd docker-compose; then
DOCKER_COMPOSE_CMD=(docker-compose)
return 0
fi
return 1
}
compose() {
if ! resolve_docker_compose; then
err "Docker Compose not found."
return 1
fi
"${DOCKER_COMPOSE_CMD[@]}" -f "$DOCKER_COMPOSE_FILE" "$@"
}
check_prereqs() {
local failures=0
local py_cmd py_ver node_ver
if py_cmd="$(resolve_python_cmd)"; then
py_ver="$("$py_cmd" --version 2>&1 | awk '{print $2}')"
if version_ge "$py_ver" 3 10; then
ok "Python: $py_ver"
else
err "Python 3.10+ required (found $py_ver)."
printf 'Next action: install Python 3.10+ and rerun ./dev init\n'
failures=1
fi
else
err "Python 3.10+ is not installed."
printf 'Next action: install Python 3.10+ and rerun ./dev init\n'
failures=1
fi
if have_cmd node; then
node_ver="$(node --version | sed 's/^v//')"
if version_ge "$node_ver" 18 0; then
ok "Node.js: $node_ver"
else
err "Node.js 18+ required (found $node_ver)."
printf 'Next action: install Node.js 18+ and rerun ./dev init\n'
failures=1
fi
else
err "Node.js 18+ is not installed."
printf 'Next action: install Node.js 18+ and rerun ./dev init\n'
failures=1
fi
if have_cmd npm; then
ok "npm: $(npm --version)"
else
err "npm is not installed."
printf 'Next action: reinstall Node.js (npm ships with Node) and rerun ./dev init\n'
failures=1
fi
if have_cmd docker; then
ok "Docker: $(docker --version | sed 's/, build.*//')"
else
err "Docker is not installed."
printf 'Next action: install Docker Desktop/Engine and rerun ./dev init\n'
failures=1
fi
if resolve_docker_compose; then
ok "Docker Compose available"
else
err "Docker Compose not found."
printf 'Next action: install Docker Compose v2 and rerun ./dev init\n'
failures=1
fi
return "$failures"
}
sync_env_file() {
if [[ ! -f "$ENV_EXAMPLE" ]]; then
err "Missing env template: $ENV_EXAMPLE"
return 1
fi
if [[ ! -f "$ENV_FILE" ]]; then
cp "$ENV_EXAMPLE" "$ENV_FILE"
ok "Created $ENV_FILE from .env.example"
return 0
fi
local added=0
local wrote_header=0
while IFS= read -r line; do
[[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || continue
local key="${line%%=*}"
if ! grep -qE "^${key}=" "$ENV_FILE"; then
if (( wrote_header == 0 )); then
printf '\n# Added by ./dev init (%s)\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >>"$ENV_FILE"
wrote_header=1
fi
printf '%s\n' "$line" >>"$ENV_FILE"
added=$((added + 1))
fi
done <"$ENV_EXAMPLE"
if (( added == 0 )); then
ok "$ENV_FILE already contains all template keys"
else
ok "Synced $ENV_FILE with template ($added keys added, existing values preserved)"
fi
}
check_openai_key_notice() {
if [[ ! -f "$ENV_FILE" ]]; then
return 0
fi
local key
key="$(grep -E '^OPENAI_API_KEY=' "$ENV_FILE" | tail -n1 | cut -d= -f2- || true)"
if [[ -z "$key" || "$key" == "your-openai-api-key-here" ]]; then
warn "OPENAI_API_KEY is not configured in backend/.env"
printf 'Next action: edit backend/.env and set OPENAI_API_KEY before embedding workflows\n'
fi
}
resolve_backend_python() {
local venv_python_unix="$BACKEND_DIR/venv/bin/python"
local venv_python_win="$BACKEND_DIR/venv/Scripts/python.exe"
if [[ -x "$venv_python_unix" ]]; then
printf '%s\n' "$venv_python_unix"
return 0
fi
if [[ -x "$venv_python_win" ]]; then
printf '%s\n' "$venv_python_win"
return 0
fi
resolve_python_cmd
}
is_pid_running() {
local pid="${1:-}"
[[ -n "$pid" ]] || return 1
[[ "$pid" =~ ^[0-9]+$ ]] || return 1
kill -0 "$pid" >/dev/null 2>&1
}
stop_monitor_daemon() {
local quiet="${1:-0}"
if [[ ! -f "$MONITOR_PID_FILE" ]]; then
if (( quiet == 0 )); then
warn "Monitor pid file not found; daemon is not running."
fi
return 0
fi
local pid
pid="$(cat "$MONITOR_PID_FILE" 2>/dev/null || true)"
if ! is_pid_running "$pid"; then
if (( quiet == 0 )); then
warn "Monitor process is not running; removing stale pid file."
fi
rm -f "$MONITOR_PID_FILE"
return 0
fi
if (( quiet == 0 )); then
info "Stopping monitor daemon (pid=$pid)"
fi
kill "$pid" >/dev/null 2>&1 || true
sleep 1
if is_pid_running "$pid"; then
if (( quiet == 0 )); then
warn "Monitor daemon did not stop cleanly; sending SIGKILL."
fi
kill -9 "$pid" >/dev/null 2>&1 || true
fi
rm -f "$MONITOR_PID_FILE"
if (( quiet == 0 )); then
ok "Monitor daemon stopped"
fi
}
bootstrap_if_needed() {
local venv_python_unix="$BACKEND_DIR/venv/bin/python"
local venv_python_win="$BACKEND_DIR/venv/Scripts/python.exe"
local needs_bootstrap=0
if [[ ! -f "$venv_python_unix" && ! -f "$venv_python_win" ]]; then
needs_bootstrap=1
fi
if [[ ! -d "$FRONTEND_DIR/node_modules" ]]; then
needs_bootstrap=1
fi
if (( needs_bootstrap == 0 )); then
return 0
fi
info "First-run bootstrap required. Running ./setup.sh ..."
if ! (cd "$ROOT_DIR" && bash ./setup.sh); then
err "Bootstrap failed."
printf 'Next action: inspect setup output above, then run ./dev doctor\n'
printf 'Where to look: docker logs via "./dev logs infra"\n'
return 1
fi
}
wait_for_services_healthy() {
local timeout="${DEV_INFRA_TIMEOUT_SEC:-240}"
local interval=3
local services=("$@")
local deadline=$((SECONDS + timeout))
if (( ${#services[@]} == 0 )); then
services=("postgres" "etcd" "minio-milvus" "milvus" "minio" "redis")
fi
info "Waiting for infrastructure health (timeout: ${timeout}s)"
while (( SECONDS < deadline )); do
local pending=0
local states=()
local svc
for svc in "${services[@]}"; do
local cid state health
cid="$(compose ps -q "$svc" 2>/dev/null || true)"
if [[ -z "$cid" ]]; then
states+=("$svc=missing")
pending=1
continue
fi
state="$(docker inspect --format '{{.State.Status}}' "$cid" 2>/dev/null || echo "unknown")"
health="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$cid" 2>/dev/null || echo "unknown")"
if [[ "$state" != "running" ]]; then
states+=("$svc=$state")
pending=1
continue
fi
if [[ "$health" == "healthy" || "$health" == "none" ]]; then
continue
fi
if [[ "$health" == "unhealthy" ]]; then
err "Infrastructure service is unhealthy: $svc"
printf 'Next action: run ./dev logs infra and inspect %s container health failures\n' "$svc"
return 1
fi
states+=("$svc=$health")
pending=1
done
if (( pending == 0 )); then
ok "Infrastructure services are healthy"
return 0
fi
info "Still waiting: ${states[*]}"
sleep "$interval"
done
err "Infrastructure did not become healthy within ${timeout}s."
printf 'Next action: run ./dev logs infra and docker compose ps\n'
printf 'Where to look: container health and startup errors for postgres/milvus/minio/redis\n'
return 1
}
run_migrations() {
local backend_py
if ! backend_py="$(resolve_backend_python)"; then
err "Unable to find a Python interpreter for backend migrations."
printf 'Next action: run ./dev init\n'
return 1
fi
info "Running database migrations"
if ! (cd "$BACKEND_DIR" && PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m scripts.setup.setup_postgres); then
err "Migration step failed."
printf 'Next action: inspect migration errors above, then rerun ./dev up\n'
printf 'Where to look: backend migration output and ./dev logs infra\n'
return 1
fi
ok "Migrations complete"
}
run_seed_data() {
local backend_py
local candidate
if ! backend_py="$(resolve_backend_python)"; then
err "Unable to find a Python interpreter for seed execution."
printf 'Next action: run ./dev init\n'
return 1
fi
if [[ -n "${DEV_SEED_CMD:-}" ]]; then
info "Running seed command from DEV_SEED_CMD"
if ! (cd "$ROOT_DIR" && bash -lc "$DEV_SEED_CMD"); then
err "DEV_SEED_CMD failed."
printf 'Next action: fix DEV_SEED_CMD and rerun ./dev seed\n'
return 1
fi
ok "Seed complete"
return 0
fi
for candidate in \
"$BACKEND_DIR/scripts/seed_dev.py" \
"$BACKEND_DIR/scripts/seed.py" \
"$BACKEND_DIR/scripts/setup/seed_dev.py" \
"$BACKEND_DIR/scripts/setup/seed.py"; do
if [[ -f "$candidate" ]]; then
info "Running seed script: ${candidate#$ROOT_DIR/}"
if ! (cd "$BACKEND_DIR" && PATH="$(dirname "$backend_py"):$PATH" "$backend_py" "${candidate#$BACKEND_DIR/}"); then
err "Seed script failed."
printf 'Next action: inspect the seed error above and rerun ./dev seed\n'
return 1
fi
ok "Seed complete"
return 0
fi
done
err "No seed script is configured for this repository."
printf 'Next action: add backend/scripts/seed_dev.py or set DEV_SEED_CMD, then rerun ./dev seed\n'
return 1
}
run_backend_tests() {
local backend_py
if ! backend_py="$(resolve_backend_python)"; then
err "Unable to find a Python interpreter for backend tests."
printf 'Next action: run ./dev init\n'
return 1
fi
info "Running backend tests (pytest)"
if ! (cd "$BACKEND_DIR" && PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m pytest); then
err "Backend tests failed."
printf 'Next action: inspect pytest output above and fix failures\n'
return 1
fi
ok "Backend tests passed"
}
run_frontend_tests_if_configured() {
if ! have_cmd node || ! have_cmd npm; then
warn "Skipping frontend tests (node/npm unavailable)"
return 0
fi
if ! (cd "$FRONTEND_DIR" && node -e 'const pkg=require("./package.json"); process.exit(pkg.scripts && pkg.scripts.test ? 0 : 1)'); then
info "No frontend test script configured; skipping"
return 0
fi
info "Running frontend tests"
if ! (cd "$FRONTEND_DIR" && npm test -- --watch=false); then
err "Frontend tests failed."
printf 'Next action: inspect frontend test output above and fix failures\n'
return 1
fi
ok "Frontend tests passed"
}
check_http() {
local label="$1"
local url="$2"
if ! have_cmd curl; then
warn "$label check skipped (curl not installed)"
return 0
fi
if curl -fsS "$url" >/dev/null; then
ok "$label reachable: $url"
return 0
else
warn "$label unreachable: $url"
return 1
fi
}
cmd_init() {
info "Running prerequisite checks"
if ! check_prereqs; then
err "Prerequisite check failed."
return 1
fi
info "Bootstrapping backend environment file"
sync_env_file
check_openai_key_notice
ok "Init complete"
}
cmd_migrate() {
cmd_init
info "Ensuring PostgreSQL is running"
if ! compose up -d postgres; then
err "Failed to start PostgreSQL for migration."
printf 'Next action: run ./dev logs infra\n'
return 1
fi
if ! wait_for_services_healthy postgres; then
return 1
fi
run_migrations
}
cmd_seed() {
cmd_init
info "Ensuring infrastructure is running"
if ! compose up -d; then
err "Failed to start infrastructure for seeding."
printf 'Next action: run ./dev logs infra\n'
return 1
fi
if ! wait_for_services_healthy; then
return 1
fi
if ! run_migrations; then
return 1
fi
run_seed_data
}
cmd_test() {
cmd_init
bootstrap_if_needed
if ! run_backend_tests; then
return 1
fi
run_frontend_tests_if_configured
}
cmd_up() {
local py_cmd
local run_seed=0
while (( $# > 0 )); do
case "$1" in
--seed) run_seed=1 ;;
*)
err "Unknown up option: $1"
printf 'Valid up options: --seed\n'
return 1
;;
esac
shift
done
cmd_init
if ! py_cmd="$(resolve_python_cmd)"; then
err "Python 3 is required to run the launcher."
return 1
fi
bootstrap_if_needed
info "Starting infrastructure"
if ! compose up -d; then
err "Failed to start infrastructure."
printf 'Next action: run ./dev doctor\n'
printf 'Where to look: ./dev logs infra\n'
return 1
fi
if ! wait_for_services_healthy; then
return 1
fi
if ! run_migrations; then
return 1
fi
if (( run_seed == 1 )); then
if ! run_seed_data; then
return 1
fi
fi
info "Success URLs"
printf ' Frontend: http://localhost:3000\n'
printf ' Backend API: http://localhost:8000\n'
printf ' API docs: http://localhost:8000/docs\n'
printf ' Verify: ./dev status\n'
info "Starting application services"
exec "$py_cmd" "$ROOT_DIR/run.py" --skip-infra-check
}
cmd_down() {
stop_monitor_daemon 1
info "Stopping infrastructure"
if compose down; then
ok "Infrastructure stopped"
else
err "Failed to stop infrastructure."
printf 'Next action: run "docker ps" and stop containers manually\n'
return 1
fi
if have_cmd lsof; then
if lsof -nP -iTCP:3000 -iTCP:8000 -sTCP:LISTEN >/dev/null 2>&1; then
warn "Listeners still active on ports 3000/8000."
printf 'Next action: stop the terminal running "./dev up" with Ctrl+C\n'
fi
fi
}
cmd_reset() {
local wipe_volumes=0
local assume_yes=0
local answer
local path
local cleanup_paths=(
"$LOG_DIR"
"$BACKEND_DIR/venv"
"$BACKEND_DIR/.pytest_cache"
"$FRONTEND_DIR/node_modules"
"$FRONTEND_DIR/.next"
)
while (( $# > 0 )); do
case "$1" in
--volumes) wipe_volumes=1 ;;
--yes|-y) assume_yes=1 ;;
*)
err "Unknown reset option: $1"
printf 'Valid reset options: --volumes --yes\n'
return 1
;;
esac
shift
done
printf '[WARN] Reset will remove local artifacts (venv, node_modules, logs, .next).\n'
if (( wipe_volumes == 1 )); then
printf '[WARN] Docker volumes will also be removed (database/object storage data loss).\n'
fi
if (( assume_yes == 0 )); then
printf 'Type RESET to continue: '
read -r answer
if [[ "$answer" != "RESET" ]]; then
info "Reset cancelled."
return 1
fi
fi
info "Stopping infrastructure"
if (( wipe_volumes == 1 )); then
if ! compose down -v --remove-orphans; then
err "Failed to stop/remove infrastructure volumes."
printf 'Next action: run docker compose down -v --remove-orphans manually\n'
return 1
fi
else
if ! compose down --remove-orphans; then
err "Failed to stop infrastructure."
printf 'Next action: run docker compose down --remove-orphans manually\n'
return 1
fi
fi
info "Removing local artifacts"
for path in "${cleanup_paths[@]}"; do
if [[ -e "$path" ]]; then
rm -rf "$path"
ok "Removed ${path#$ROOT_DIR/}"
fi
done
ok "Reset complete"
printf 'Next action: run ./dev init then ./dev up\n'
}
cmd_status() {
info "Infrastructure containers"
if resolve_docker_compose; then
compose ps
else
warn "Docker Compose not available; cannot show container status"
fi
printf '\n'
info "Application endpoints"
check_http "Backend liveness" "http://localhost:8000/health/live"
check_http "Backend readiness" "http://localhost:8000/health/ready"
check_http "Frontend" "http://localhost:3000"
printf '\n'
info "Active monitor"
local monitor_pid_running=0
if [[ -f "$MONITOR_PID_FILE" ]]; then
local monitor_pid
monitor_pid="$(cat "$MONITOR_PID_FILE" 2>/dev/null || true)"
if is_pid_running "$monitor_pid"; then
ok "Monitor daemon process detected (pid=$monitor_pid)"
monitor_pid_running=1
else
warn "Monitor pid file exists but process is not running"
printf 'Next action: run ./dev monitor --daemon\n'
fi
else
info "Monitor daemon not running (no pid file)"
fi
local backend_py
if ! backend_py="$(resolve_backend_python)"; then
warn "Backend Python not found; cannot query monitor status"
printf 'Next action: run ./dev init\n'
return 0
fi
if (( monitor_pid_running == 1 )) && [[ -f "$MONITOR_DAEMON_ENV_FILE" ]]; then
(
cd "$BACKEND_DIR"
set -a
# shellcheck source=/dev/null
source "$MONITOR_DAEMON_ENV_FILE"
set +a
PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m scripts.monitor status
) || warn "Unable to query monitor status from backend scripts.monitor"
else
(
cd "$BACKEND_DIR"
PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m scripts.monitor status
) || warn "Unable to query monitor status from backend scripts.monitor"
fi
}
cmd_monitor() {
local daemon=0
local once=0
local show_status=0
local stop_daemon=0
while (( $# > 0 )); do
case "$1" in
--daemon) daemon=1 ;;
--once) once=1 ;;
--status) show_status=1 ;;
--stop) stop_daemon=1 ;;
*)
err "Unknown monitor option: $1"
printf 'Valid monitor options: --daemon --once --status --stop\n'
return 1
;;
esac
shift
done
if (( daemon == 1 && once == 1 )); then
err "--daemon and --once cannot be used together"
return 1
fi
local backend_py
if ! backend_py="$(resolve_backend_python)"; then
err "Unable to find backend Python for monitor command."
printf 'Next action: run ./dev init (or ./setup.sh)\n'
return 1
fi
if (( stop_daemon == 1 )); then
stop_monitor_daemon 0
return 0
fi
if (( show_status == 1 )); then
local pid_running=0
if [[ -f "$MONITOR_PID_FILE" ]]; then
local status_pid
status_pid="$(cat "$MONITOR_PID_FILE" 2>/dev/null || true)"
if is_pid_running "$status_pid"; then
pid_running=1
fi
fi
if (( pid_running == 1 )) && [[ -f "$MONITOR_DAEMON_ENV_FILE" ]]; then
(
cd "$BACKEND_DIR"
set -a
# shellcheck source=/dev/null
source "$MONITOR_DAEMON_ENV_FILE"
set +a
PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m scripts.monitor status
)
else
(
cd "$BACKEND_DIR"
PATH="$(dirname "$backend_py"):$PATH" "$backend_py" -m scripts.monitor status
)
fi
return $?
fi
local monitor_enabled="${MONITOR_ENABLED:-true}"
local monitor_mode="${MONITOR_MODE:-observe}"
local monitor_dry_run="${MONITOR_DRY_RUN:-false}"
local monitor_interval="${MONITOR_INTERVAL_SECONDS:-15}"
local monitor_retries="${MONITOR_MAX_RETRIES:-3}"
local monitor_backoff="${MONITOR_BACKOFF_SECONDS:-10}"
local monitor_cb_threshold="${MONITOR_CIRCUIT_BREAKER_THRESHOLD:-5}"
if (( daemon == 1 )); then
mkdir -p "$LOG_DIR"
if [[ -f "$MONITOR_PID_FILE" ]]; then
local existing_pid
existing_pid="$(cat "$MONITOR_PID_FILE" 2>/dev/null || true)"
if is_pid_running "$existing_pid"; then
warn "Monitor daemon is already running (pid=$existing_pid)."
printf 'Next action: run ./dev monitor --status (or ./dev monitor --stop)\n'
return 0
fi
rm -f "$MONITOR_PID_FILE"
fi
cat >"$MONITOR_DAEMON_ENV_FILE" <<EOF
MONITOR_ENABLED=$monitor_enabled
MONITOR_MODE=$monitor_mode
MONITOR_DRY_RUN=$monitor_dry_run
MONITOR_INTERVAL_SECONDS=$monitor_interval
MONITOR_MAX_RETRIES=$monitor_retries
MONITOR_BACKOFF_SECONDS=$monitor_backoff
MONITOR_CIRCUIT_BREAKER_THRESHOLD=$monitor_cb_threshold
EOF
info "Starting monitor daemon"
(
cd "$BACKEND_DIR"
MONITOR_ENABLED="$monitor_enabled" \
MONITOR_MODE="$monitor_mode" \
MONITOR_DRY_RUN="$monitor_dry_run" \
MONITOR_INTERVAL_SECONDS="$monitor_interval" \
MONITOR_MAX_RETRIES="$monitor_retries" \
MONITOR_BACKOFF_SECONDS="$monitor_backoff" \
MONITOR_CIRCUIT_BREAKER_THRESHOLD="$monitor_cb_threshold" \
PATH="$(dirname "$backend_py"):$PATH" \
nohup "$backend_py" -m scripts.monitor run >>"$MONITOR_DAEMON_LOG_FILE" 2>&1 &
echo $! >"$MONITOR_PID_FILE"
)
sleep 1
local started_pid
started_pid="$(cat "$MONITOR_PID_FILE" 2>/dev/null || true)"
if is_pid_running "$started_pid"; then
ok "Monitor daemon started (pid=$started_pid)"
printf ' Status: ./dev monitor --status\n'
printf ' Stop: ./dev monitor --stop\n'
printf ' Logs: tail -f %s\n' "$MONITOR_DAEMON_LOG_FILE"
return 0
fi
err "Monitor daemon failed to start."
printf 'Next action: inspect %s\n' "$MONITOR_DAEMON_LOG_FILE"
return 1
fi
local monitor_args=(run)
if (( once == 1 )); then
monitor_args+=(--once)
fi
info "Running monitor in foreground (Ctrl+C to stop)"
(
cd "$BACKEND_DIR"
MONITOR_ENABLED="$monitor_enabled" \
MONITOR_MODE="$monitor_mode" \
MONITOR_DRY_RUN="$monitor_dry_run" \
MONITOR_INTERVAL_SECONDS="$monitor_interval" \
MONITOR_MAX_RETRIES="$monitor_retries" \
MONITOR_BACKOFF_SECONDS="$monitor_backoff" \
MONITOR_CIRCUIT_BREAKER_THRESHOLD="$monitor_cb_threshold" \
PATH="$(dirname "$backend_py"):$PATH" \
"$backend_py" -m scripts.monitor "${monitor_args[@]}"
)
}
cmd_logs() {
local target="${1:-app}"
case "$target" in
infra)
compose logs -f
;;
app)
local files=()
[[ -f "$LOG_DIR/backend.log" ]] && files+=("$LOG_DIR/backend.log")
[[ -f "$LOG_DIR/frontend.log" ]] && files+=("$LOG_DIR/frontend.log")
[[ -f "$LOG_DIR/celery.log" ]] && files+=("$LOG_DIR/celery.log")
if (( ${#files[@]} == 0 )); then
err "No app log files found in $LOG_DIR"
printf 'Next action: run ./dev up, then run ./dev logs app\n'
return 1
fi
tail -n 100 -f "${files[@]}"
;;
backend|frontend|celery)
local file="$LOG_DIR/${target}.log"
if [[ ! -f "$file" ]]; then
err "Log file not found: $file"
printf 'Next action: run ./dev up, then rerun ./dev logs %s\n' "$target"
return 1
fi
tail -n 100 -f "$file"
;;
monitor)
if [[ ! -f "$MONITOR_DAEMON_LOG_FILE" ]]; then
err "Monitor daemon log file not found: $MONITOR_DAEMON_LOG_FILE"
printf 'Next action: run ./dev monitor --daemon, then rerun ./dev logs monitor\n'
return 1
fi
tail -n 100 -f "$MONITOR_DAEMON_LOG_FILE"
;;
*)
err "Unknown logs target: $target"
printf 'Valid targets: app | infra | backend | frontend | celery | monitor\n'
return 1
;;
esac
}
cmd_doctor() {
local failures=0
local infra_warnings=0
local endpoint_warnings=0
info "Doctor: prerequisite checks"
if ! check_prereqs; then
failures=$((failures + 1))
fi
if [[ -f "$DOCKER_COMPOSE_FILE" ]]; then
ok "Found docker compose file"
else
err "Missing docker compose file: $DOCKER_COMPOSE_FILE"
printf 'Next action: restore docker-compose.yml, then rerun ./dev doctor\n'
failures=$((failures + 1))
fi
if [[ -f "$ROOT_DIR/run.py" ]]; then
ok "Found app launcher (run.py)"
else
err "Missing launcher: $ROOT_DIR/run.py"
printf 'Next action: restore run.py, then rerun ./dev doctor\n'
failures=$((failures + 1))
fi
if have_cmd docker; then
if docker info >/dev/null 2>&1; then
ok "Docker daemon reachable"
else
err "Docker daemon is not reachable."
printf 'Next action: start Docker Desktop/Engine, then rerun ./dev doctor\n'
failures=$((failures + 1))
fi
fi
if resolve_docker_compose; then
if compose config -q >/dev/null 2>&1; then
ok "Docker compose config is valid"
else
err "Docker compose config is invalid."
printf 'Next action: run "docker compose -f docker-compose.yml config" and fix errors\n'
failures=$((failures + 1))
fi
fi
if [[ -f "$ENV_FILE" ]]; then
ok "Found backend/.env"
else
err "Missing backend/.env"
printf 'Next action: run ./dev init\n'
failures=$((failures + 1))
fi
check_openai_key_notice
if [[ -x "$BACKEND_DIR/venv/bin/python" || -x "$BACKEND_DIR/venv/Scripts/python.exe" ]]; then
ok "Backend virtual environment detected"
else
warn "Backend virtual environment not found"
printf 'Next action: run ./dev up (or ./setup.sh) to bootstrap backend dependencies\n'