: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(warehouse-sources): read CDC schedule state without crashing (#88684)
Daniel RC1b7c4414467e
diff --git a/posthog/temporal/common/schedule.py b/posthog/temporal/common/schedule.pyindex 82b1758b2855..964489d47543 100644--- a/posthog/temporal/common/schedule.py+++ b/posthog/temporal/common/schedule.py@@ -3,7 +3,14 @@ from __future__ import annotationsfrom typing import TYPE_CHECKINGfrom asgiref.sync import async_to_sync-from temporalio.client import Client, Schedule, ScheduleOverlapPolicy, ScheduleUpdate, ScheduleUpdateInput+from temporalio.client import (+ Client,+ Schedule,+ ScheduleDescription,+ ScheduleOverlapPolicy,+ ScheduleUpdate,+ ScheduleUpdateInput,+)from temporalio.service import RPCError, RPCStatusCodeif TYPE_CHECKING:@@ -124,6 +131,12 @@ async def describe_schedule(temporal: Client, schedule_id: str):return await handle.describe()+async def a_describe_schedule(temporal: Client, schedule_id: str) -> ScheduleDescription:+ """Async describe a Temporal Schedule."""+ handle = temporal.get_schedule_handle(schedule_id)+ return await handle.describe()++@async_to_syncasync def pause_schedule(temporal: Client, schedule_id: str, note: str | None = None) -> None:"""Pause a Temporal Schedule."""diff --git a/products/data_warehouse/backend/logic/data_load/service.py b/products/data_warehouse/backend/logic/data_load/service.pyindex 10770192d83b..1947abdea77c 100644--- a/products/data_warehouse/backend/logic/data_load/service.py+++ b/products/data_warehouse/backend/logic/data_load/service.py@@ -30,13 +30,13 @@ from posthog.temporal.common.client import async_connect, sync_connectfrom posthog.temporal.common.schedule import (a_create_schedule,a_delete_schedule,+ a_describe_schedule,a_schedule_exists,a_trigger_schedule,a_unpause_schedule,a_update_schedule,create_schedule,delete_schedule,- describe_schedule,pause_schedule,schedule_exists,trigger_schedule,@@ -618,7 +618,7 @@ async def is_cdc_extraction_schedule_paused(source_id: str) -> bool:schedule_id = _get_cdc_extraction_schedule_id(source_id)temporal = await async_connect()try:- desc = await describe_schedule(temporal, schedule_id=schedule_id)+ desc = await a_describe_schedule(temporal, schedule_id=schedule_id)except temporalio.service.RPCError as e:if e.status == temporalio.service.RPCStatusCode.NOT_FOUND:return False@@ -637,7 +637,7 @@ async def cdc_extraction_schedule_has_running_action(source_id: str) -> bool:schedule_id = _get_cdc_extraction_schedule_id(source_id)temporal = await async_connect()try:- desc = await describe_schedule(temporal, schedule_id=schedule_id)+ desc = await a_describe_schedule(temporal, schedule_id=schedule_id)except temporalio.service.RPCError as e:if e.status == temporalio.service.RPCStatusCode.NOT_FOUND:return Falsediff --git a/products/data_warehouse/backend/logic/data_load/test/test_service.py b/products/data_warehouse/backend/logic/data_load/test/test_service.pyindex da44f21bf625..7d2606eef1fa 100644--- a/products/data_warehouse/backend/logic/data_load/test/test_service.py+++ b/products/data_warehouse/backend/logic/data_load/test/test_service.py@@ -2,6 +2,8 @@ import uuidimport randomimport loggingimport datetime as dt+from collections.abc import Callable+from typing import Anyimport pytestfrom unittest.mock import AsyncMock, MagicMock, patch@@ -29,9 +31,11 @@ from products.data_warehouse.backend.logic.data_load.service import (a_unpause_external_data_schedule,bulk_sync_cdc_extraction_schedules,bulk_update_external_data_job_schedules,+ cdc_extraction_schedule_has_running_action,cdc_min_interval,get_discover_schemas_schedule,get_sync_schedule,+ is_cdc_extraction_schedule_paused,pause_external_data_schedule,unpause_external_data_schedule,)@@ -354,6 +358,49 @@ def test_cdc_min_interval(intervals, expected):assert cdc_min_interval(intervals) == expected+# --- CDC extraction schedule reads ---+#+# Both reads are `@async_to_sync`, so the name is bound to a sync callable and these tests call it+# without `await`. Awaiting one would try to await the bool it returns.+++def _temporal_with_schedule(desc: MagicMock | None = None, describe_side: BaseException | None = None) -> Any:+ # Patched at the Temporal client, not at the schedule helper: a mocked helper is exactly what+ # hid a sync helper being awaited inside the event loop these functions run in.+ handle = MagicMock()+ handle.describe = AsyncMock(return_value=desc, side_effect=describe_side)+ client = MagicMock()+ client.get_schedule_handle.return_value = handle+ return patch(f"{SERVICE}.async_connect", AsyncMock(return_value=client))++[email protected]("paused", [True, False])+def test_cdc_schedule_paused_reports_the_schedule_state(paused: bool) -> None:+ desc = MagicMock()+ desc.schedule.state.paused = paused++ with _temporal_with_schedule(desc):+ assert is_cdc_extraction_schedule_paused("01a0393e-a79f-0000-0361-2cabb703888f") is paused++[email protected]("running_actions,expected", [([], False), ([MagicMock()], True)])+def test_cdc_schedule_running_action_reports_an_executing_run(running_actions: list[MagicMock], expected: bool) -> None:+ desc = MagicMock()+ desc.info.running_actions = running_actions++ with _temporal_with_schedule(desc):+ assert cdc_extraction_schedule_has_running_action("01a0393e-a79f-0000-0361-2cabb703888f") is expected+++ "read",+ [is_cdc_extraction_schedule_paused, cdc_extraction_schedule_has_running_action],+)+def test_a_missing_schedule_reads_as_neither_paused_nor_running(read: Callable[[str], bool]) -> None:+ with _temporal_with_schedule(describe_side=_not_found()):+ assert read("01a0393e-a79f-0000-0361-2cabb703888f") is False++# --- bulk_sync_cdc_extraction_schedules (upsert: update, else create+trigger) ---diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.pyindex a48b7404048c..1d81c569b041 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/errors.py@@ -107,10 +107,11 @@ _CATEGORY_DEFAULTS: dict[CDCErrorCategory, tuple[str, bool]] = {True,),CDCErrorCategory.HOST_UNREACHABLE: (- "PostHog has no network route to the source database host, so it can't be reached. Check "- "that the host and port are correct and reachable from the public internet (PostHog's IP "- "addresses allowed through, and the host not resolving to a private or unreachable "- "address), then re-enable change data capture.",+ "PostHog couldn't open a network connection to the source database. Check that the host "+ "and port are correct, and that the database accepts connections from the public internet "+ "with PostHog's IP addresses allowed through. If all of that looks right, contact support: "+ "some hosts resolve to an address PostHog can't reach, which is ours to fix. Re-enable "+ "change data capture once it's reachable.",False,),CDCErrorCategory.SSH_TUNNEL_FAILED: (