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:
@@ -13,9 +13,25 @@ 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
|
||||||
@@ -47,7 +63,7 @@ if [ "$ENV_TYPE" == "single" ]; then
|
|||||||
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
|
||||||
@@ -81,7 +97,7 @@ elif [ "$ENV_TYPE" == "multisite" ]; then
|
|||||||
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
|
||||||
@@ -120,7 +136,7 @@ elif [ "$ENV_TYPE" == "playground-single" ]; then
|
|||||||
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": {
|
||||||
@@ -152,6 +168,8 @@ EOF
|
|||||||
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 &
|
||||||
|
PYTHON_PID=$!
|
||||||
|
echo "Started Python HTTP server with PID: $PYTHON_PID"
|
||||||
echo "Opening WordPress Playground in your browser..."
|
echo "Opening WordPress Playground in your browser..."
|
||||||
if command_exists open; then
|
if command_exists open; then
|
||||||
open http://localhost:8888/index.html
|
open http://localhost:8888/index.html
|
||||||
@@ -187,7 +205,7 @@ elif [ "$ENV_TYPE" == "playground-multisite" ]; then
|
|||||||
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": {
|
||||||
@@ -259,6 +277,8 @@ EOF
|
|||||||
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 &
|
||||||
|
PYTHON_PID=$!
|
||||||
|
echo "Started Python HTTP server with PID: $PYTHON_PID"
|
||||||
echo "Opening WordPress Playground in your browser..."
|
echo "Opening WordPress Playground in your browser..."
|
||||||
if command_exists open; then
|
if command_exists open; then
|
||||||
open http://localhost:8888/multisite.html
|
open http://localhost:8888/multisite.html
|
||||||
|
|||||||
Reference in New Issue
Block a user