Skip to content

Commit

Permalink
Refactor php-fpm scripts to use supervisord
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlemke committed Mar 26, 2020
1 parent f63c48d commit b5a5b42
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 74 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ENV FLOWNATIVE_LIB_PATH="/opt/flownative/lib" \
PATH="/opt/flownative/php/bin:$PATH" \
LOG_DEBUG="true"

USER root
COPY --from=docker.pkg.github.com/flownative/bash-library/bash-library:1 /lib $FLOWNATIVE_LIB_PATH

COPY root-files /
Expand Down
22 changes: 14 additions & 8 deletions root-files/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ set -o pipefail

# Load lib
. "${FLOWNATIVE_LIB_PATH}/banner.sh"
. "${FLOWNATIVE_LIB_PATH}/validation.sh"
. "${FLOWNATIVE_LIB_PATH}/php-fpm.sh"
. "${FLOWNATIVE_LIB_PATH}/supervisor.sh"

banner_flownative PHP

eval "$(php_fpm_env)"
eval "$(supervisor_env)"

banner_flownative PHP
php_fpm_initialize

if [[ "$*" = *"run"* ]]; then
php_fpm_initialize
supervisor_initialize
supervisor_start

trap 'php_fpm_stop' SIGINT SIGTERM
php_fpm_start
trap 'supervisor_stop' SIGINT SIGTERM

wait "$(php_fpm_get_pid)"
# This line will not be reached, because a trap handles termination
if [[ "$*" = *"run"* ]]; then
supervisor_pid=$(supervisor_get_pid)
info "Entrypoint: Start up complete"
# We can't use "wait" because supervisord is not a direct child of this shell:
while [ -e "/proc/${supervisor_pid}" ]; do sleep 1.1; done
info "Good bye 👋"
else
"$@"
fi
69 changes: 3 additions & 66 deletions root-files/opt/flownative/lib/php-fpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,72 +36,6 @@ export PHP_FPM_MAX_CHILDREN="${PHP_FPM_MAX_CHILDREN:-20}"
EOF
}

# ---------------------------------------------------------------------------------------
# php_fpm_get_pid() - Return the php process id
#
# @global PHP_* The PHP_ environment variables
# @return Returns the PHP process id, if it is running, otherwise 0
#
php_fpm_get_pid() {
local pid
pid=$(process_get_pid_from_file "${PHP_TMP_PATH}/php-fpm.pid")

if [[ -n "${pid}" ]]; then
echo "${pid}"
else
false
fi
}

# ---------------------------------------------------------------------------------------
# php_fpm_has_pid() - Checks if a PID file exists
#
# @global PHP_* The PHP_ environment variables
# @return Returns false if no PID file exists
#
php_fpm_has_pid() {
if [[ ! -f "${PHP_TMP_PATH}/php-fpm.pid" ]]; then
false
fi
}

# ---------------------------------------------------------------------------------------
# php_fpm_start() - Start PHP
#
# @global PHP_* The PHP_ environment variables
# @return void
#
php_fpm_start() {
local pid

info "PHP-FPM: Starting ..."
"${PHP_BASE_PATH}/sbin/php-fpm" 2>&1 | (sed 's/^/PHP-FPM: /' | output) &
sleep 1

with_backoff "php_fpm_has_pid" || (error "PHP-FPM: Could not retrieve PID of the PHP-FPM process, maybe it failed during start-up?"; exit 1)
pid=$(php_fpm_get_pid)

info "PHP-FPM: Running as process #${pid}"
}

# ---------------------------------------------------------------------------------------
# php_fpm_stop() - Stop the php process based on the current PID
#
# @global PHP_* The PHP_ evnironment variables
# @return void
#
php_fpm_stop() {
local pid

pid=$(php_fpm_get_pid)
is_process_running "${pid}" || (info "PHP-FPM: Could not stop, because the process was not running (detected pid: ${pid})" && return);
info "PHP-FPM: Stopping ..."

process_stop "${pid}"

info "PHP-FPM: Process stopped, good-bye ... 👋"
}

# ---------------------------------------------------------------------------------------
# php_fpm_conf_validate() - Validates configuration options passed as PHP_* env vars
#
Expand All @@ -126,4 +60,7 @@ php_fpm_initialize() {

info "PHP-FPM: Initializing configuration ..."
envsubst < "${PHP_CONF_PATH}/php-fpm.conf.template" > "${PHP_CONF_PATH}/php-fpm.conf"

# Create a file descriptor for the PHP-FPM log output and clean up the log lines a bit:
exec 4> >(sed -e "s/^\([0-9\/-]* [0-9:,]*\)/\1 OUTPUT PHP-FPM:/")
}
10 changes: 10 additions & 0 deletions root-files/opt/flownative/supervisor/etc/conf.d/php-fpm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[program:php-fpm]
process_name=%(program_name)s
command="%(ENV_PHP_BASE_PATH)s/sbin/php-fpm"
autostart=true
autorestart=true
numprocs=1

stdout_logfile=/proc/self/fd/4
stdout_logfile_maxbytes=0
redirect_stderr=true

0 comments on commit b5a5b42

Please sign in to comment.