Skip to content

Commit

Permalink
fix(temporal): Handle SIGTERM correctly on worker cleanup (#25505)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias authored Oct 10, 2024
1 parent 6276a31 commit cd98275
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 16 additions & 3 deletions bin/temporal-django-worker
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions posthog/temporal/common/worker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import signal
import sys
from datetime import timedelta

from temporalio.runtime import PrometheusConfig, Runtime, TelemetryConfig
Expand Down Expand Up @@ -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()

0 comments on commit cd98275

Please sign in to comment.