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:
@@ -14,8 +14,24 @@ 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
|
||||||
@@ -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
|
||||||
@@ -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