: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
fix(temporal): feed the liveness tracker on every task queue (#75410)
Tom Owers72c70b11bfae
diff --git a/posthog/management/commands/start_temporal_worker.py b/posthog/management/commands/start_temporal_worker.pyindex d0850888aaa6..a63a9469ddf2 100644--- a/posthog/management/commands/start_temporal_worker.py+++ b/posthog/management/commands/start_temporal_worker.py@@ -17,7 +17,7 @@ from posthog.temporal.common.open_telemetry import initialize_otelwith workflow.unsafe.imports_passed_through():from django.conf import settings- from django.core.management.base import BaseCommand+ from django.core.management.base import BaseCommand, CommandErrorfrom posthog.clickhouse.query_tagging import tag_queriesfrom posthog.temporal.ai import AI_ACTIVITIES, AI_WORKFLOWS, POSTHOG_CODE_SLACK_ACTIVITIES, POSTHOG_CODE_SLACK_WORKFLOWS@@ -42,7 +42,8 @@ from posthog.temporal.cleanup_property_definitions import (WORKFLOWS as CLEANUP_PROPDEFS_WORKFLOWS,)from posthog.temporal.common.health_server import HealthCheckServer-from posthog.temporal.common.liveness_tracker import get_liveness_tracker+from posthog.temporal.common.interceptor import is_task_queue_supported+from posthog.temporal.common.liveness_tracker import LivenessInterceptor, get_liveness_trackerfrom posthog.temporal.common.logger import configure_logger, get_loggerfrom posthog.temporal.common.worker import ManagedWorker, create_workerfrom posthog.temporal.data_modeling import (@@ -785,6 +786,20 @@ class Command(BaseCommand):# Create and start health check serverif health_port and health_max_idle_seconds:+ # Without the liveness interceptor nothing ever feeds the tracker, so `idle_seconds`+ # is just process uptime and the probe 503s at `max_idle_seconds` on a perfectly+ # healthy worker — k8s then reaps every replica on a fixed cycle. Refuse to serve a+ # probe that can only ever fail; a worker that won't start is far louder than one+ # that restarts forever.+ if not is_task_queue_supported(task_queue, LivenessInterceptor):+ raise CommandError(+ f"Refusing to start the health server: task queue '{task_queue}' is not covered by "+ f"LivenessInterceptor, so /healthz would report process uptime as idle time and fail "+ f"after {health_max_idle_seconds}s regardless of worker health. Add the queue to "+ f"LivenessInterceptor.task_queue, or unset TEMPORAL_HEALTH_PORT / "+ f"TEMPORAL_HEALTH_MAX_IDLE_SECONDS for this deployment."+ )+health_server = HealthCheckServer(port=health_port,liveness_tracker=get_liveness_tracker(),diff --git a/posthog/temporal/common/liveness_tracker.py b/posthog/temporal/common/liveness_tracker.pyindex d7b5497e3361..ece94fdfd82c 100644--- a/posthog/temporal/common/liveness_tracker.py+++ b/posthog/temporal/common/liveness_tracker.py@@ -2,8 +2,6 @@ import timeimport threadingfrom typing import Any-from django.conf import settings-from temporalio.worker import (ActivityInboundInterceptor,ExecuteActivityInput,@@ -13,6 +11,8 @@ from temporalio.worker import (WorkflowInterceptorClassInput,)+from posthog.temporal.common.interceptor import ALL_TASK_QUEUES+class LivenessTracker:"""Tracks the last time the worker successfully executed a workflow or activity.@@ -121,7 +121,14 @@ class _LivenessWorkflowInterceptor(WorkflowInboundInterceptor):class LivenessInterceptor(Interceptor):"""Interceptor that tracks worker liveness for health checks."""- task_queue = (settings.DATA_WAREHOUSE_TASK_QUEUE, settings.MAX_AI_TASK_QUEUE, settings.TASKS_TASK_QUEUE)+ # Every queue, deliberately. This interceptor is what feeds the tracker behind `/healthz`, and+ # the health server is already opt-in per deployment (`TEMPORAL_HEALTH_PORT` ++ # `TEMPORAL_HEALTH_MAX_IDLE_SECONDS`). An allowlist here is a second, invisible opt-in that has+ # to be kept in sync with chart values in another repo, and when the two disagree the tracker is+ # never fed: `idle_seconds` silently becomes process uptime, so the liveness probe reaps every+ # replica at `max_idle_seconds` however busy it is. `start_temporal_worker` refuses to start a+ # health server on a queue this interceptor does not cover.+ task_queue = ALL_TASK_QUEUESdef intercept_activity(self, next: ActivityInboundInterceptor) -> ActivityInboundInterceptor:return _LivenessActivityInboundInterceptor(super().intercept_activity(next))diff --git a/posthog/temporal/tests/common/test_liveness_tracker.py b/posthog/temporal/tests/common/test_liveness_tracker.pyindex 5acf6e5a47c2..3d492d2cbe55 100644--- a/posthog/temporal/tests/common/test_liveness_tracker.py+++ b/posthog/temporal/tests/common/test_liveness_tracker.py@@ -153,16 +153,21 @@ class TestLivenessTracker:@pytest.mark.parametrize(- "task_queue,supported",+ "task_queue",(- (settings.DATA_WAREHOUSE_TASK_QUEUE, True),- (settings.MAX_AI_TASK_QUEUE, True),- (settings.TASKS_TASK_QUEUE, True),- ("some-other-queue", False),+ settings.DATA_WAREHOUSE_TASK_QUEUE,+ settings.MAX_AI_TASK_QUEUE,+ settings.TASKS_TASK_QUEUE,+ settings.SESSION_REPLAY_TASK_QUEUE,+ settings.BATCH_EXPORTS_TASK_QUEUE,+ settings.GENERAL_PURPOSE_TASK_QUEUE,+ "a-queue-that-does-not-exist-yet",),)-def test_liveness_interceptor_supports_expected_task_queues(task_queue, supported):- assert is_task_queue_supported(task_queue, LivenessInterceptor) is supported+def test_liveness_interceptor_covers_every_task_queue(task_queue):+ # Narrowing this back to an allowlist leaves the omitted queue's tracker unfed, so+ # /healthz reports process uptime and k8s reaps every replica at max_idle_seconds.+ assert is_task_queue_supported(task_queue, LivenessInterceptor) is True@pytest.mark.asyncio