@@ -18,122 +18,41 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
1818# Cleanup function
1919cleanup () {
2020 echo " Cleaning up..."
21-
22- # 1. Gracefully stop Overmind if it's running
21+
22+ # First, gracefully stop Overmind if it's running
2323 if [ -S " ./.overmind.sock" ]; then
2424 echo " Stopping Overmind gracefully..."
25- if overmind quit; then
26- echo " Overmind stopped successfully"
27- else
28- echo " Warning: Overmind quit command failed"
29- fi
30-
31- # Wait for Overmind to fully shut down
32- max_wait=30 # Increased from 10 to 30 seconds
33- count=0
34- while [ -S " ./.overmind.sock" ] && [ $count -lt $max_wait ]; do
35- echo " Waiting for Overmind socket to close (attempt $count /$max_wait )..."
36- sleep 1
37- count=$(( count + 1 ))
38- done
39-
40- # Force remove the socket file if it still exists
41- if [ -S " ./.overmind.sock" ]; then
42- echo " Warning: Overmind socket still exists after waiting, forcefully removing..."
43- rm -f ./.overmind.sock
44- else
45- echo " Overmind socket closed successfully"
46- fi
25+ overmind quit || true
26+ sleep 5 # Give Overmind time to shut down
4727 fi
4828
49- # 2. Gracefully stop PostgreSQL using pg_ctl if possible
29+ # Stop PostgreSQL processes gracefully
5030 if pgrep -f " postgres" > /dev/null; then
51- echo " Attempting to stop PostgreSQL gracefully..."
52-
53- # Improved logic to find PostgreSQL data directory
54- PGDATA=$( ps aux | grep postgres | grep -v grep | grep -oP ' (?<= -D ).*?(?=\s)' || true)
55-
56- if [ -n " $PGDATA " ] && [ -d " $PGDATA " ]; then
57- # Use pg_ctl for graceful shutdown if available
58- if command -v " ${PSQLBIN} /pg_ctl" > /dev/null; then
59- echo " Using pg_ctl to stop PostgreSQL..."
60- " ${PSQLBIN} /pg_ctl" -D " $PGDATA " stop -m smart
61-
62- # Wait for PostgreSQL to shut down
63- max_wait=60 # Increased from 30 to 60 seconds
64- count=0
65- while pgrep -f " postgres" > /dev/null && [ $count -lt $max_wait ]; do
66- echo " Waiting for PostgreSQL to stop (attempt $count /$max_wait )..."
67- sleep 1
68- count=$(( count + 1 ))
69- done
70-
71- if pgrep -f " postgres" > /dev/null; then
72- echo " Warning: PostgreSQL did not shut down gracefully, attempting fast shutdown..."
73- " ${PSQLBIN} /pg_ctl" -D " $PGDATA " stop -m fast
74- sleep 5
75- fi
76- else
77- echo " Warning: pg_ctl not found, falling back to signal-based shutdown"
78- pkill -TERM postgres || true
79- sleep 5
80- fi
81- else
82- echo " Warning: Could not determine PostgreSQL data directory, using enhanced signal-based shutdown"
83-
84- # Enhanced shutdown with multiple signals
85- pkill -TERM postgres || true
86- sleep 5
87- if pgrep -f " postgres" > /dev/null; then
88- pkill -INT postgres || true
89- sleep 5
90- if pgrep -f " postgres" > /dev/null; then
91- pkill -KILL postgres || true
92- fi
93- fi
94- fi
31+ echo " Stopping PostgreSQL gracefully..."
32+ pkill -TERM postgres || true
33+ sleep 5 # Wait for PostgreSQL to shut down
9534 fi
9635
97- # 3. Clean up tmux sessions gracefully
36+ # Clean up any remaining tmux sessions
9837 if tmux ls 2> /dev/null | grep ' overmind' ; then
9938 echo " Cleaning up tmux sessions..."
100- tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | while IFS= read -r session; do
101- echo " Stopping tmux session: $session "
102- tmux send-keys -t " $session " C-c # Send Ctrl+C to gracefully stop processes
103- sleep 2
104- tmux kill-session -t " $session " || true
105- done
39+ tmux ls 2> /dev/null | grep ' overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
10640 sleep 2
10741 fi
10842
109- # 4. Final verification and cleanup
43+ # Final cleanup: Force kill if necessary (as a last resort)
11044 if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
111- echo " Warning: Some processes are still running after graceful shutdown attempts:"
112- ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
113-
114- # More aggressive cleanup
115- echo " Sending SIGTERM to remaining processes..."
116- pkill -TERM -f " (postgres|overmind|tmux.*postgresql)" || true
117- sleep 5
118-
119- if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
120- echo " Sending SIGINT to remaining processes..."
121- pkill -INT -f " (postgres|overmind|tmux.*postgresql)" || true
122- sleep 5
123-
124- if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
125- echo " Force killing remaining processes..."
126- pkill -KILL -f " (postgres|overmind|tmux.*postgresql)" || true
127- fi
128- fi
45+ echo " Force killing remaining processes..."
46+ pkill -9 -f " (postgres|overmind|tmux.*postgresql)" || true
47+ sleep 2
12948 fi
13049
13150 # Remove socket and Procfile
13251 rm -f .overmind.sock Procfile
13352
134- # Final verification
53+ # Verify cleanup
13554 if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
136- echo " Error: Cleanup incomplete. Remaining processes :"
55+ echo " Warning: Some processes could not be cleaned up :"
13756 ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
13857 return 1
13958 else
0 commit comments