Skip to content

Commit 906ea5b

Browse files
committed
feat(dev): add automatic shutdown on bot failure
- Change restart policy to 'no' for all services to prevent infinite restarts - Add dev-watch.sh script for simple watch mode with cleanup - Add dev-monitor.sh script for advanced monitoring with automatic shutdown - Services will now stop automatically when bot fails to start - Prevents orphaned database containers running indefinitely
1 parent 451665f commit 906ea5b

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
container_name: tux-postgres
88
hostname: tux-postgres
99
image: postgres:15-alpine
10-
restart: unless-stopped
10+
restart: "no"
1111
environment:
1212
POSTGRES_DB: ${POSTGRES_DB:-tuxdb}
1313
POSTGRES_USER: ${POSTGRES_USER:-tuxuser}
@@ -74,7 +74,7 @@ services:
7474
POSTGRES_DB: ${POSTGRES_DB:-tuxdb}
7575
POSTGRES_USER: ${POSTGRES_USER:-tuxuser}
7676
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-ChangeThisToAStrongPassword123!}
77-
restart: unless-stopped
77+
restart: "no"
7878
depends_on:
7979
tux-postgres:
8080
condition: service_healthy
@@ -144,7 +144,7 @@ services:
144144
image: adminer:latest
145145
container_name: tux-adminer
146146
hostname: tux-adminer
147-
restart: unless-stopped
147+
restart: "no"
148148
depends_on:
149149
tux-postgres:
150150
condition: service_healthy

scripts/dev-monitor.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Advanced development monitor with automatic cleanup
5+
# Monitors the bot container and shuts down all services if it fails
6+
7+
echo "🚀 Starting Tux Development Monitor"
8+
echo "===================================="
9+
10+
# Configuration
11+
BOT_CONTAINER="tux"
12+
MAX_RESTART_ATTEMPTS=3
13+
RESTART_DELAY=5
14+
MONITOR_INTERVAL=10
15+
16+
# Function to cleanup all services
17+
cleanup() {
18+
echo ""
19+
echo "🧹 Cleaning up all services..."
20+
docker compose down
21+
echo "✅ Cleanup complete"
22+
}
23+
24+
# Function to check if bot container is running and healthy
25+
check_bot_health() {
26+
local container_status=$(docker inspect --format='{{.State.Status}}' "$BOT_CONTAINER" 2>/dev/null || echo "not_found")
27+
local exit_code=$(docker inspect --format='{{.State.ExitCode}}' "$BOT_CONTAINER" 2>/dev/null || echo "0")
28+
29+
if [ "$container_status" = "not_found" ]; then
30+
echo "❌ Bot container not found"
31+
return 1
32+
elif [ "$container_status" = "exited" ]; then
33+
echo "❌ Bot container exited with code: $exit_code"
34+
return 1
35+
elif [ "$container_status" = "running" ]; then
36+
echo "✅ Bot container is running"
37+
return 0
38+
else
39+
echo "⚠️ Bot container status: $container_status"
40+
return 1
41+
fi
42+
}
43+
44+
# Function to start services
45+
start_services() {
46+
echo "⏳ Starting services..."
47+
if ! docker compose up -d; then
48+
echo "❌ Failed to start services"
49+
return 1
50+
fi
51+
52+
# Wait for bot to start
53+
echo "⏳ Waiting for bot to start..."
54+
local attempts=0
55+
while [ $attempts -lt 30 ]; do
56+
if check_bot_health; then
57+
echo "✅ Bot started successfully"
58+
return 0
59+
fi
60+
sleep 2
61+
attempts=$((attempts + 1))
62+
done
63+
64+
echo "❌ Bot failed to start within timeout"
65+
return 1
66+
}
67+
68+
# Set up trap to cleanup on script exit
69+
trap cleanup EXIT INT TERM
70+
71+
# Start services
72+
if ! start_services; then
73+
echo "❌ Failed to start services"
74+
exit 1
75+
fi
76+
77+
# Monitor loop
78+
echo "👀 Starting monitor loop..."
79+
restart_attempts=0
80+
81+
while true; do
82+
if ! check_bot_health; then
83+
restart_attempts=$((restart_attempts + 1))
84+
echo "⚠️ Bot failure detected (attempt $restart_attempts/$MAX_RESTART_ATTEMPTS)"
85+
86+
if [ $restart_attempts -ge $MAX_RESTART_ATTEMPTS ]; then
87+
echo "❌ Maximum restart attempts reached. Shutting down all services."
88+
cleanup
89+
exit 1
90+
fi
91+
92+
echo "🔄 Restarting services in ${RESTART_DELAY} seconds..."
93+
sleep $RESTART_DELAY
94+
95+
if ! start_services; then
96+
echo "❌ Failed to restart services"
97+
cleanup
98+
exit 1
99+
fi
100+
else
101+
# Reset restart counter on successful health check
102+
restart_attempts=0
103+
fi
104+
105+
sleep $MONITOR_INTERVAL
106+
done

scripts/dev-watch.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Development watch script with automatic cleanup on failure
5+
# This script starts the bot with watch mode and automatically shuts down
6+
# all services if the bot fails to start or crashes
7+
8+
echo "🚀 Starting Tux with Docker Compose Watch"
9+
echo "=========================================="
10+
11+
# Function to cleanup on exit
12+
cleanup() {
13+
echo ""
14+
echo "🧹 Cleaning up services..."
15+
docker compose down
16+
echo "✅ Cleanup complete"
17+
}
18+
19+
# Set up trap to cleanup on script exit
20+
trap cleanup EXIT INT TERM
21+
22+
# Start services with watch mode
23+
echo "⏳ Starting services with watch mode..."
24+
if ! docker compose up --watch; then
25+
echo "❌ Services failed to start or crashed"
26+
echo "🛑 Automatic cleanup will occur on script exit"
27+
exit 1
28+
fi

0 commit comments

Comments
 (0)