@@ -18,122 +18,41 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
18
18
# Cleanup function
19
19
cleanup () {
20
20
echo " Cleaning up..."
21
-
22
- # 1. Gracefully stop Overmind if it's running
21
+
22
+ # First, gracefully stop Overmind if it's running
23
23
if [ -S " ./.overmind.sock" ]; then
24
24
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
47
27
fi
48
28
49
- # 2. Gracefully stop PostgreSQL using pg_ctl if possible
29
+ # Stop PostgreSQL processes gracefully
50
30
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
95
34
fi
96
35
97
- # 3. Clean up tmux sessions gracefully
36
+ # Clean up any remaining tmux sessions
98
37
if tmux ls 2> /dev/null | grep ' overmind' ; then
99
38
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
106
40
sleep 2
107
41
fi
108
42
109
- # 4. Final verification and cleanup
43
+ # Final cleanup: Force kill if necessary (as a last resort)
110
44
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
129
48
fi
130
49
131
50
# Remove socket and Procfile
132
51
rm -f .overmind.sock Procfile
133
52
134
- # Final verification
53
+ # Verify cleanup
135
54
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 :"
137
56
ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
138
57
return 1
139
58
else
0 commit comments