From e27a41e9ddc329411828e39b6a5451d4c32cc7ce Mon Sep 17 00:00:00 2001 From: Lukas Wingerberg Date: Tue, 31 Oct 2023 23:01:59 +0100 Subject: [PATCH] simplify the init script a bit to lessen code duplication --- docker/init_scripts/init | 76 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/docker/init_scripts/init b/docker/init_scripts/init index ba39ef7e9..0dd15946d 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -18,25 +18,31 @@ INIT_DEBUG="${INIT_DEBUG:="false"}" # switch to backend directory cd /backend || { echo "/backend directory doesn't seem to exist"; exit 1; } +# print debug log output if enabled debug_log () { if [ "${INIT_DEBUG}" == "true" ]; then echo "DEBUG: [init][$(date +"%Y-%m-%d %T")]" "${@}" fi } +# print debug log output if enabled +info_log () { + echo "INFO: [init][$(date +"%Y-%m-%d %T")]" "${@}" +} + # function that runs or main process and creates a corresponding PID file, # sadly uvicorn can not do that itself -start_uvicorn () { +start_bin_uvicorn () { # Commands to start our main application and store its PID to check for crashes - debug_log "starting uvicorn" + info_log "starting uvicorn" uvicorn main:app --proxy-headers --host 0.0.0.0 --port 5000 --uds /tmp/uvicorn.sock --workers 2 & UVICORN_PID=$! echo $UVICORN_PID > /tmp/uvicorn.pid } # Commands to start nginx (handling PID creation internally) -start_nginx () { - debug_log "starting nginx" +start_bin_nginx () { + info_log "starting nginx" if [ "$EUID" -ne 0 ]; then nginx else @@ -45,45 +51,35 @@ start_nginx () { fi } -# function that runs our watcher process and creates a corresponding PID file, -# we might be able to do that ourself in the future -start_watcher () { - debug_log "starting watcher" - python3 watcher.py & +# function that runs our independent python scripts and creates corresponding PID files, +start_python () { + SCRIPT="${1}" + info_log "starting ${SCRIPT}.py" + python3 "${SCRIPT}.py" & WATCHER_PID=$! - echo $WATCHER_PID > /tmp/watcher.pid -} - -# function that runs our worker process and creates a corresponding PID file, -# we might be able to do that ourself in the future -start_worker () { - debug_log "starting worker" - python3 worker.py & - WORKER_PID=$! - echo $WORKER_PID > /tmp/worker.pid -} - -# function that runs our scheduler process and creates a corresponding PID file, -# we might be able to do that ourself in the future -start_scheduler () { - debug_log "starting scheduler" - python3 scheduler.py & - SCHEDULER_PID=$! - echo $SCHEDULER_PID > /tmp/scheduler.pid + echo $WATCHER_PID > "/tmp/${SCRIPT}.pid" } watchdog_process_pid () { - PROCESS=$1 - PIDFILE=$2 - if [ -f "${PIDFILE}" ]; then + TYPE=$1 + PROCESS=$2 + if [ -f "/tmp/${PROCESS}.pid" ]; then # check if the pid we last wrote to our state file is actually active - if [ -d "/proc/$(cat "${PIDFILE}")" ]; then + if [ -d "/proc/$(cat "/tmp/${PROCESS}.pid")" ]; then debug_log "${PROCESS} still running, no need to start" else - start_"${PROCESS}" + if [ "${TYPE}" == "bin" ]; then + start_bin_"${PROCESS}" + elif [ "${TYPE}" == "python" ]; then + start_python "${PROCESS}" + fi fi else - start_"${PROCESS}" + if [ "${TYPE}" == "bin" ]; then + start_bin_"${PROCESS}" + elif [ "${TYPE}" == "python" ]; then + start_python "${PROCESS}" + fi fi } @@ -104,22 +100,22 @@ while true; do fi # Start nginx if we dont have a corresponding PID file - watchdog_process_pid nginx /tmp/nginx.pid + watchdog_process_pid bin nginx # Start uvicorn if we dont have a corresponding PID file - watchdog_process_pid uvicorn /tmp/uvicorn.pid + watchdog_process_pid bin uvicorn # Start watcher if we dont have a corresponding PID file - watchdog_process_pid watcher /tmp/watcher.pid + watchdog_process_pid python watcher # Start background worker processes when we have a REDIS configured # ENABLE_EXPERIMENTAL_REDIS is defaulted to false, unless its set from our docker env if [[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]]; then - debug_log "REDIS true, starting worker and scheduler" + debug_log "ENABLE_EXPERIMENTAL_REDIS true, starting worker and scheduler" # Start worker if we dont have a corresponding PID file - watchdog_process_pid worker /tmp/worker.pid + watchdog_process_pid python worker # Start scheduler if we dont have a corresponding PID file - watchdog_process_pid scheduler /tmp/scheduler.pid + watchdog_process_pid python scheduler fi # check for died processes every 5 seconds