Skip to content

Commit e8d2342

Browse files
samrosesoedirgo
authored andcommitted
fix: a better cleanup process
1 parent d442769 commit e8d2342

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

nix/tools/dbmate-tool.sh.in

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,49 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
1919
cleanup() {
2020
echo "Cleaning up..."
2121

22-
# Kill postgres processes first
22+
# First, gracefully stop Overmind if it's running
23+
if [ -S "./.overmind.sock" ]; then
24+
echo "Stopping Overmind gracefully..."
25+
overmind quit || true
26+
sleep 5 # Give Overmind time to shut down
27+
fi
28+
29+
# Stop PostgreSQL processes gracefully
2330
if pgrep -f "postgres" >/dev/null; then
31+
echo "Stopping PostgreSQL gracefully..."
2432
pkill -TERM postgres || true
25-
sleep 2
33+
sleep 5 # Wait for PostgreSQL to shut down
2634
fi
2735

28-
# Then kill overmind
29-
if [ -S "./.overmind.sock" ]; then
30-
overmind kill || true
36+
# Clean up any remaining tmux sessions
37+
if tmux ls 2>/dev/null | grep 'overmind'; then
38+
echo "Cleaning up tmux sessions..."
39+
tmux ls 2>/dev/null | grep 'overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
3140
sleep 2
3241
fi
3342

34-
# Kill tmux sessions explicitly
35-
pkill -f "tmux.*overmind.*postgresql" || true
36-
tmux ls 2>/dev/null | grep 'overmind' | cut -d: -f1 | xargs -I{} tmux kill-session -t {} || true
43+
# Final cleanup: Force kill if necessary (as a last resort)
44+
if ps aux | grep -E "(postgres|overmind|tmux.*postgresql)" | grep -v grep >/dev/null; then
45+
echo "Force killing remaining processes..."
46+
pkill -9 -f "(postgres|overmind|tmux.*postgresql)" || true
47+
sleep 2
48+
fi
3749

38-
# Force kill any stragglers
39-
pkill -9 -f "(postgres|tmux.*overmind.*postgresql)" || true
40-
50+
# Remove socket and Procfile
4151
rm -f .overmind.sock Procfile
4252

43-
# Final verification
53+
# Verify cleanup
4454
if ps aux | grep -E "(postgres|overmind|tmux.*postgresql)" | grep -v grep >/dev/null; then
55+
echo "Warning: Some processes could not be cleaned up:"
4556
ps aux | grep -E "(postgres|overmind|tmux.*postgresql)" | grep -v grep
4657
return 1
58+
else
59+
echo "Cleanup completed successfully"
4760
fi
4861
}
4962

5063
# Set up trap for cleanup on script exit
51-
64+
trap cleanup EXIT INT TERM
5265
# Function to display help
5366
print_help() {
5467
echo "Usage: nix run .#dbmate-tool -- [options]"

0 commit comments

Comments
 (0)