Skip to content

Commit

Permalink
chore: allow other scripts to stop the start-server cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
samrose committed Feb 6, 2025
1 parent 5dc3f99 commit cd87dcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 7 additions & 10 deletions nix/tools/dbmate-tool.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@

# Start PostgreSQL using nix
start_postgres() {
echo "Starting PostgreSQL..."
nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations --daemonize
DATDIR=$(mktemp -d)
echo "Starting PostgreSQL in directory: $DATDIR" # Create the DATDIR if it doesn't exist
nix run "$FLAKE_URL#start-server" -- "$PSQL_VERSION" --skip-migrations --daemonize --datdir "$DATDIR"
echo "PostgreSQL started."
}

Expand All @@ -31,16 +32,15 @@ cleanup() {
if pgrep -f "postgres" >/dev/null; then
echo "Stopping PostgreSQL gracefully..."

# Use a more specific signal handling approach
pkill -15 -f "postgres" # Send SIGTERM
# Use pg_ctl to stop PostgreSQL
pg_ctl -D "$DATDIR" stop

# Wait a bit for graceful shutdown
sleep 5

# If processes are still running, force kill
# Check if processes are still running
if pgrep -f "postgres" >/dev/null; then
echo "Forcing PostgreSQL processes to stop..."
pkill -9 -f "postgres" # Send SIGKILL if SIGTERM didn't work
echo "Warning: Some PostgreSQL processes could not be stopped gracefully."
fi
else
echo "PostgreSQL is not running, skipping stop."
Expand All @@ -53,9 +53,6 @@ cleanup() {
else
echo "Cleanup completed successfully"
fi

# Explicitly exit with 0 to prevent workflow failure
exit 0
}


Expand Down
14 changes: 12 additions & 2 deletions nix/tools/run-server.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ stop_postgres() {

trap 'stop_postgres' SIGINT SIGTERM

# Parse arguments
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -108,6 +107,15 @@ while [[ "$#" -gt 0 ]]; do
print_help
exit 0
;;
--datdir)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
DATDIR="$2"
shift 2
else
echo "Error: --datadir requires a directory path"
exit 1
fi
;;
*)
if [[ "$1" =~ ^- ]]; then
echo "Unknown option: $1"
Expand Down Expand Up @@ -165,7 +173,9 @@ STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@
MECAB_LIB=@MECAB_LIB@

# Setup directories and locale settings
DATDIR=$(mktemp -d)
if [[ -z "$DATDIR" ]]; then
DATDIR=$(mktemp -d)
fi
LOCALE_ARCHIVE=@LOCALES@
CURRENT_SYSTEM=@CURRENT_SYSTEM@

Expand Down

0 comments on commit cd87dcb

Please sign in to comment.