Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/cb.project/php/_waitfile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

filename=$1
timeout=$2

CMD="
while [ ! -f ${filename} ]
do
echo -n .
sleep 0.2
done
echo
"

# Return when file exists with timeout
bash -c "${CMD}" & \
sleep ${timeout};

# Try killning bash shell
kill $! &> /dev/null
RES=$?

# If killing succeeds then fail,
# succeed if killing fails (process already dead)
if [ ${RES} = 0 ]; then
exit 1
else
exit 0
fi
3 changes: 3 additions & 0 deletions core/cb.project/php/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Use HHVM if it exists
if [[ ! -z "$(which hhvm)" ]]; then
bash -c "cd ${WORKSPACE} && hhvm -m server -p ${PORT}"
# Use apache if it exists on current system
elif [[ -f "/usr/sbin/apachectl" ]]; then
${DIR}/run_apache.sh "${WORKSPACE}" ${PORT}
# Fallback to builtin PHP server
else
php -S "0.0.0.0:${PORT}" -t ${WORKSPACE}
Expand Down
38 changes: 32 additions & 6 deletions core/cb.project/php/run_apache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
WORKSPACE=$1
PORT=$2

# Dir of current script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Detect current platform
# We need this to customize configuration differently for OS X and Linux
platform="$(uname)"
Expand Down Expand Up @@ -43,12 +46,15 @@ fi
mkdir -p ${FOLDER}
mkdir -p "${FOLDER}/logs"

PID_FILE="${FOLDER}/httpd.pid"
LOCK_FILE="${FOLDER}/accept.lock"

# Generate the apache config
cat <<EOF > "${FOLDER}/${CONF}"
ServerName localhost
Listen ${PORT}
PidFile ${FOLDER}/httpd.pid
LockFile ${FOLDER}/accept.lock
PidFile ${PID_FILE}
LockFile ${LOCK_FILE}

# Start only one server
StartServers 1
Expand All @@ -71,19 +77,39 @@ ${EXTRA_CONF}

EOF


# Wait for a process or group of processes
function anywait() {
for pid in "$@"; do
while kill -0 "$pid" &> /dev/null; do
sleep 0.5
done
done
}

function cleanup {
if [[ -f "${FOLDER}/http.pid" ]]; then
if [[ -f ${PID_FILE} ]]; then
echo "Killed process"
kill -s KILL $(cat "${FOLDER}/http.pid")
# Kill process and all children
/bin/kill -s KILL -$(cat ${PID_FILE})
fi
# Remove folder on exit
echo "Cleaning up ${FOLDER}"
rm -rf ${FOLDER}
}

# Cleanup when killed
trap cleanup EXIT INT
trap cleanup EXIT INT KILL

# Run apache process in foreground
echo "Running apache2 on ${WORKSPACE} (${FOLDER})"
/usr/sbin/apachectl -d ${FOLDER} -f ${CONF} -e info -D FOREGROUND
/usr/sbin/apachectl -d ${FOLDER} -f ${CONF} -e info

# Wait for PID_FILE to appear, timeout after 5s
${DIR}/_waitfile.sh ${PID_FILE} 5

# Wait for Apache process
PID=$(cat ${PID_FILE})
echo "Waiting for Apache2 process : ${PID}"
anywait ${PID}
echo "Apache is dead (pid=${PID})"