0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 19:53:47 +08:00
discourse/config/unicorn_launcher
Alan Guo Xiang Tan 63023145c2
DEV: Check RUN_PITCHFORK for explicit "1" value (#37264)
What is the problem?

We have a need to disable pitchfork via the `RUN_PITCHFORK` env.
However, `RUN_PITCHFORK` is checked for existence/non-emptiness in both
`bin/rails` and `config/unicorn_launcher`. This means setting
`RUN_PITCHFORK=0` enables pitchfork, which is counterintuitive.

What is the solution?

Change the checks to explicitly test for the value `"1"`. Now only
`RUN_PITCHFORK=1` enables pitchfork, while `0` or unset uses unicorn.
2026-01-23 09:30:28 +08:00

95 lines
2.2 KiB
Bash
Executable file
Vendored

#!/bin/bash
# This is a helper script you can use to supervise unicorn, it allows you to perform a live restart
# by sending it a USR2 signal
LOCAL_WEB="http://127.0.0.1:3000/"
function log() {
echo "($$) $1"
}
function on_exit() {
kill $UNICORN_PID
log "exiting"
}
function on_reload_unicorn() {
log "Reloading unicorn ($UNICORN_PID)"
kill -s USR2 $UNICORN_PID
unset NEW_UNICORN_PID
count=0
while [ "$count" -lt 180 -a -z "$NEW_UNICORN_PID" ]; do
NEW_UNICORN_PID=$(pgrep -f -P $UNICORN_PID "unicorn master")
log "Waiting for new unicorn master pid... $NEW_UNICORN_PID"
count=$((count + 1))
sleep 1
done
if [ -n "$NEW_UNICORN_PID" ]; then
count=0
while [ "$count" -lt 180 -a -z "$(ps -f --ppid $NEW_UNICORN_PID | grep worker | head -1 | awk '{ print $2 }')" ]; do
log "Waiting for new unicorn workers under $NEW_UNICORN_PID to start up..."
count=$((count + 1))
sleep 1
done
curl $LOCAL_WEB &>/dev/null
kill -s QUIT $UNICORN_PID
log "Old pid is: $UNICORN_PID New pid is: $NEW_UNICORN_PID"
UNICORN_PID=$NEW_UNICORN_PID
else
log "Unicorn is taking too long to reload...Sending TERM to $UNICORN_PID"
kill -s TERM $UNICORN_PID
fi
}
function on_reload_pitchfork() {
log "Reloading pitchfork ($UNICORN_PID)"
OLD_PID=$UNICORN_PID
pitchfork "${PITCHFORK_ARGS[@]}" &
UNICORN_PID=$!
log "Old pid is: $OLD_PID New pid is: $UNICORN_PID"
count=0
while [ "$count" -lt 180 -a -z "$(pgrep -f -P $UNICORN_PID worker)" ]; do
log "Waiting for new pitchfork workers under $UNICORN_PID to start up..."
count=$((count + 1))
sleep 1
done
kill $OLD_PID 2>/dev/null
}
function on_reopenlogs() {
log "Reopening logs"
kill -s USR1 $UNICORN_PID
}
export UNICORN_SUPERVISOR_PID=$$
if [[ "$RUN_PITCHFORK" == "1" ]]; then
PITCHFORK_ARGS=()
for arg in "$@"; do
PITCHFORK_ARGS+=("${arg/unicorn.conf.rb/pitchfork.conf.rb}")
done
fi
trap on_exit EXIT
if [[ "$RUN_PITCHFORK" != "1" ]]; then
trap on_reload_unicorn USR2 HUP
trap on_reopenlogs USR1
unicorn "$@" &
else
trap on_reload_pitchfork USR2 HUP
pitchfork "${PITCHFORK_ARGS[@]}" &
fi
UNICORN_PID=$!
echo "supervisor pid: $UNICORN_SUPERVISOR_PID unicorn pid: $UNICORN_PID"
while kill -0 $UNICORN_PID; do
sleep 1
done