fix: add graceful termination for Python HTTP server in playground modes (#62)

Adds PYTHON_PID variable and cleanup() function with EXIT/INT/TERM trap
to ensure the background Python HTTP server is always stopped when the
script exits, whether normally or due to an unexpected interruption.

Applies to both playground-single and playground-multisite branches.

Closes #30
This commit is contained in:
2026-03-16 23:08:34 +00:00
committed by GitHub
parent 595855ce10
commit a8f968562c

View File

@@ -5,122 +5,138 @@ chmod +x "$0"
# Check if environment type is provided # Check if environment type is provided
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Usage: $0 [single|multisite|playground-single|playground-multisite]" echo "Usage: $0 [single|multisite|playground-single|playground-multisite]"
exit 1 exit 1
fi fi
ENV_TYPE=$1 ENV_TYPE=$1
# Function to check if a command exists # Function to check if a command exists
command_exists() { command_exists() {
command -v "$1" &> /dev/null command -v "$1" &>/dev/null
return $?
} }
# PID of the background Python HTTP server (set when started).
PYTHON_PID=""
# Function to clean up resources on exit.
cleanup() {
if [ -n "$PYTHON_PID" ]; then
echo "Stopping Python HTTP server (PID: $PYTHON_PID)..."
kill "$PYTHON_PID" 2>/dev/null || true
fi
return 0
}
# Trap EXIT, INT, and TERM so the server is always stopped on script exit.
trap cleanup EXIT INT TERM
# Function to install wp-env if needed # Function to install wp-env if needed
install_wp_env() { install_wp_env() {
if ! command_exists wp-env; then if ! command_exists wp-env; then
echo "wp-env is not installed. Installing..." echo "wp-env is not installed. Installing..."
npm install -g @wordpress/env npm install -g @wordpress/env
fi fi
} }
# Function to install wp-playground if needed # Function to install wp-playground if needed
install_wp_playground() { install_wp_playground() {
# Check if we have a local installation # Check if we have a local installation
if [ ! -d "node_modules/@wp-playground" ]; then if [ ! -d "node_modules/@wp-playground" ]; then
echo "WordPress Playground is not installed locally. Installing..." echo "WordPress Playground is not installed locally. Installing..."
npm install --save-dev @wp-playground/client @wp-playground/blueprints npm install --save-dev @wp-playground/client @wp-playground/blueprints
fi fi
} }
if [ "$ENV_TYPE" == "single" ]; then if [ "$ENV_TYPE" == "single" ]; then
echo "Setting up single site environment..." echo "Setting up single site environment..."
# Install wp-env if needed # Install wp-env if needed
install_wp_env install_wp_env
# Start the environment # Start the environment
wp-env start wp-env start
# Wait for WordPress to be ready with a timeout # Wait for WordPress to be ready with a timeout
MAX_ATTEMPTS=30 MAX_ATTEMPTS=30
ATTEMPT=0 ATTEMPT=0
echo "Waiting for WordPress to be ready..." echo "Waiting for WordPress to be ready..."
until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT+1)) ATTEMPT=$((ATTEMPT + 1))
echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..." echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..."
sleep 2 sleep 2
done done
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo "Timed out waiting for WordPress to be ready." echo "Timed out waiting for WordPress to be ready."
exit 1 exit 1
fi fi
# Activate our plugin # Activate our plugin
if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding; then if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding; then
echo "Failed to activate plugin. Exiting." echo "Failed to activate plugin. Exiting."
exit 1 exit 1
fi fi
echo "WordPress Single Site environment is ready!" echo "WordPress Single Site environment is ready!"
echo "Site: http://localhost:8888" echo "Site: http://localhost:8888"
echo "Admin login: admin / password" echo "Admin login: admin / password"
elif [ "$ENV_TYPE" == "multisite" ]; then elif [ "$ENV_TYPE" == "multisite" ]; then
echo "Setting up multisite environment..." echo "Setting up multisite environment..."
# Install wp-env if needed # Install wp-env if needed
install_wp_env install_wp_env
# Start the environment with multisite configuration # Start the environment with multisite configuration
wp-env start --config=.wp-env.multisite.json wp-env start --config=.wp-env.multisite.json
# Wait for WordPress to be ready with a timeout # Wait for WordPress to be ready with a timeout
MAX_ATTEMPTS=30 MAX_ATTEMPTS=30
ATTEMPT=0 ATTEMPT=0
echo "Waiting for WordPress to be ready..." echo "Waiting for WordPress to be ready..."
until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do until wp-env run cli wp core is-installed || [ $ATTEMPT -ge $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT+1)) ATTEMPT=$((ATTEMPT + 1))
echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..." echo "Attempt $ATTEMPT/$MAX_ATTEMPTS..."
sleep 2 sleep 2
done done
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo "Timed out waiting for WordPress to be ready." echo "Timed out waiting for WordPress to be ready."
exit 1 exit 1
fi fi
# Create a test site # Create a test site
if ! wp-env run cli wp site create --slug=testsite --title="Test Site" --email=admin@example.com; then if ! wp-env run cli wp site create --slug=testsite --title="Test Site" --email=admin@example.com; then
echo "Failed to create test site. Exiting." echo "Failed to create test site. Exiting."
exit 1 exit 1
fi fi
# Network activate our plugin # Network activate our plugin
if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding --network; then if ! wp-env run cli wp plugin activate wp-plugin-starter-template-for-ai-coding --network; then
echo "Failed to activate plugin. Exiting." echo "Failed to activate plugin. Exiting."
exit 1 exit 1
fi fi
echo "WordPress Multisite environment is ready!" echo "WordPress Multisite environment is ready!"
echo "Main site: http://localhost:8888" echo "Main site: http://localhost:8888"
echo "Test site: http://localhost:8888/testsite" echo "Test site: http://localhost:8888/testsite"
echo "Admin login: admin / password" echo "Admin login: admin / password"
elif [ "$ENV_TYPE" == "playground-single" ]; then elif [ "$ENV_TYPE" == "playground-single" ]; then
echo "Setting up WordPress Playground single site environment..." echo "Setting up WordPress Playground single site environment..."
# Install wp-playground if needed # Install wp-playground if needed
install_wp_playground install_wp_playground
# Create plugin zip # Create plugin zip
echo "Creating plugin zip..." echo "Creating plugin zip..."
mkdir -p dist mkdir -p dist
zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*" zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*"
# Update blueprint to use local plugin # Update blueprint to use local plugin
cat > playground/blueprint.json << EOF cat >playground/blueprint.json <<EOF
{ {
"landingPage": "/wp-admin/", "landingPage": "/wp-admin/",
"preferredVersions": { "preferredVersions": {
@@ -148,46 +164,48 @@ elif [ "$ENV_TYPE" == "playground-single" ]; then
} }
EOF EOF
# Start WordPress Playground # Start WordPress Playground
echo "Starting WordPress Playground..." echo "Starting WordPress Playground..."
if command_exists python3; then if command_exists python3; then
python3 -m http.server 8888 --directory playground & python3 -m http.server 8888 --directory playground &
echo "Opening WordPress Playground in your browser..." PYTHON_PID=$!
if command_exists open; then echo "Started Python HTTP server with PID: $PYTHON_PID"
open http://localhost:8888/index.html echo "Opening WordPress Playground in your browser..."
elif command_exists xdg-open; then if command_exists open; then
xdg-open http://localhost:8888/index.html open http://localhost:8888/index.html
elif command_exists start; then elif command_exists xdg-open; then
start http://localhost:8888/index.html xdg-open http://localhost:8888/index.html
else elif command_exists start; then
echo "Please open http://localhost:8888/index.html in your browser" start http://localhost:8888/index.html
fi else
else echo "Please open http://localhost:8888/index.html in your browser"
echo "Python3 is not installed. Please open playground/index.html in your browser." fi
fi else
echo "Python3 is not installed. Please open playground/index.html in your browser."
fi
# Wait for WordPress Playground to be ready # Wait for WordPress Playground to be ready
echo "Waiting for WordPress Playground to be ready..." echo "Waiting for WordPress Playground to be ready..."
sleep 5 sleep 5
echo "WordPress Playground Single Site environment is ready!" echo "WordPress Playground Single Site environment is ready!"
echo "Site: http://localhost:8888" echo "Site: http://localhost:8888"
echo "Admin login: admin / password" echo "Admin login: admin / password"
echo "Press Ctrl+C to stop the server when done." echo "Press Ctrl+C to stop the server when done."
elif [ "$ENV_TYPE" == "playground-multisite" ]; then elif [ "$ENV_TYPE" == "playground-multisite" ]; then
echo "Setting up WordPress Playground multisite environment..." echo "Setting up WordPress Playground multisite environment..."
# Install wp-playground if needed # Install wp-playground if needed
install_wp_playground install_wp_playground
# Create plugin zip # Create plugin zip
echo "Creating plugin zip..." echo "Creating plugin zip..."
mkdir -p dist mkdir -p dist
zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*" zip -r dist/plugin.zip . -x "node_modules/*" "dist/*" ".git/*"
# Update blueprint to use local plugin # Update blueprint to use local plugin
cat > playground/multisite-blueprint.json << EOF cat >playground/multisite-blueprint.json <<EOF
{ {
"landingPage": "/wp-admin/network/", "landingPage": "/wp-admin/network/",
"preferredVersions": { "preferredVersions": {
@@ -255,35 +273,37 @@ elif [ "$ENV_TYPE" == "playground-multisite" ]; then
} }
EOF EOF
# Start WordPress Playground # Start WordPress Playground
echo "Starting WordPress Playground..." echo "Starting WordPress Playground..."
if command_exists python3; then if command_exists python3; then
python3 -m http.server 8888 --directory playground & python3 -m http.server 8888 --directory playground &
echo "Opening WordPress Playground in your browser..." PYTHON_PID=$!
if command_exists open; then echo "Started Python HTTP server with PID: $PYTHON_PID"
open http://localhost:8888/multisite.html echo "Opening WordPress Playground in your browser..."
elif command_exists xdg-open; then if command_exists open; then
xdg-open http://localhost:8888/multisite.html open http://localhost:8888/multisite.html
elif command_exists start; then elif command_exists xdg-open; then
start http://localhost:8888/multisite.html xdg-open http://localhost:8888/multisite.html
else elif command_exists start; then
echo "Please open http://localhost:8888/multisite.html in your browser" start http://localhost:8888/multisite.html
fi else
else echo "Please open http://localhost:8888/multisite.html in your browser"
echo "Python3 is not installed. Please open playground/multisite.html in your browser." fi
fi else
echo "Python3 is not installed. Please open playground/multisite.html in your browser."
fi
# Wait for WordPress Playground to be ready # Wait for WordPress Playground to be ready
echo "Waiting for WordPress Playground to be ready..." echo "Waiting for WordPress Playground to be ready..."
sleep 5 sleep 5
echo "WordPress Playground Multisite environment is ready!" echo "WordPress Playground Multisite environment is ready!"
echo "Main site: http://localhost:8888" echo "Main site: http://localhost:8888"
echo "Test site: http://localhost:8888/testsite" echo "Test site: http://localhost:8888/testsite"
echo "Admin login: admin / password" echo "Admin login: admin / password"
echo "Press Ctrl+C to stop the server when done." echo "Press Ctrl+C to stop the server when done."
else else
echo "Invalid environment type. Use 'single', 'multisite', 'playground-single', or 'playground-multisite'." echo "Invalid environment type. Use 'single', 'multisite', 'playground-single', or 'playground-multisite'."
exit 1 exit 1
fi fi