mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-21 13:39:22 +01:00
fix(temporal): Handle SIGTERM correctly on worker cleanup (#25505)
This commit is contained in:
parent
6276a319c5
commit
cd98275030
@ -2,8 +2,21 @@
|
||||
|
||||
set -e
|
||||
|
||||
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
|
||||
cleanup() {
|
||||
echo "Stopping worker..."
|
||||
if kill -0 "$worker_pid" >/dev/null 2>&1; then
|
||||
kill "$worker_pid"
|
||||
else
|
||||
echo "Worker process is not running."
|
||||
fi
|
||||
}
|
||||
|
||||
python3 manage.py start_temporal_worker "$@"
|
||||
trap cleanup SIGINT SIGTERM EXIT
|
||||
|
||||
wait
|
||||
python3 manage.py start_temporal_worker "$@" &
|
||||
|
||||
worker_pid=$!
|
||||
|
||||
wait $worker_pid
|
||||
|
||||
cleanup
|
||||
|
@ -1,5 +1,5 @@
|
||||
import asyncio
|
||||
import signal
|
||||
import sys
|
||||
from datetime import timedelta
|
||||
|
||||
from temporalio.runtime import PrometheusConfig, Runtime, TelemetryConfig
|
||||
@ -43,10 +43,10 @@ async def start_worker(
|
||||
|
||||
# catch the TERM signal, and stop the worker gracefully
|
||||
# https://github.com/temporalio/sdk-python#worker-shutdown
|
||||
async def signal_handler(sig, frame):
|
||||
async def shutdown_worker():
|
||||
await worker.shutdown()
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.add_signal_handler(signal.SIGTERM, lambda: asyncio.create_task(shutdown_worker()))
|
||||
|
||||
await worker.run()
|
||||
|
Loading…
Reference in New Issue
Block a user