:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
#!/bin/bash
set -e
# The worker can lose a SIGTERM (for example when its asyncio signal pipe is full), and then it
# keeps polling for new work until the pod is killed. The worker handles a repeated SIGTERM as a
# no-op, so this sends it again until the worker exits.
SIGTERM_RESEND_INTERVAL_SECONDS="${TEMPORAL_WORKER_SIGTERM_RESEND_INTERVAL_SECONDS:-30}"
resender_pid=""
resend_sigterm_until_exit() {
while kill -0 "$worker_pid" >/dev/null 2>&1; do
sleep "$SIGTERM_RESEND_INTERVAL_SECONDS"
kill -SIGTERM "$worker_pid" >/dev/null 2>&1 || true
done
}
cleanup() {
if kill -0 "$worker_pid" >/dev/null 2>&1; then
echo "Signaling worker to initialize graceful shutdown"
kill -SIGTERM "$worker_pid"
echo "Worker signaled for graceful shutdown"
if [ -z "$resender_pid" ]; then
resend_sigterm_until_exit &
resender_pid=$!
fi
else
echo "Could not initiate graceful shutdown as worker process is not running"
fi
}
trap cleanup SIGINT SIGTERM EXIT
echo "Initializing Temporal Worker"
python3 manage.py start_temporal_worker "$@" &
worker_pid=$!
while true; do
if wait $worker_pid; then
echo "Worker exited normally, exiting"
break
else
status=$?
# Worker process could finish before the trap handler yields control back to us.
# So, we first re-check if we are still running.
if ! kill -0 "$worker_pid" >/dev/null 2>&1; then
echo "Worker process no longer exists, exiting"
break
fi
# If we exit with SIGTERM, status will be 128 + 15.
# If we exit with SIGINT, status will be 128 + 2.
if [ $status -eq 143 ] || [ $status -eq 130 ]; then
echo "Received signal $(($status - 128)), waiting for worker to finish"
elif [ $status -eq 0 ]; then
echo "Worker exited normally, exiting"
break
else
echo "Worker exited with unexpected exit status $status, exiting"
break
fi
fi
done