: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(data-imports): retry the revenue analytics ready check on a transient db error (#87446)
Tom Owers062d64d60327
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/load.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/load.pyindex 4cd5d6696a1d..0f87c2d143c5 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/load.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/load.py@@ -111,7 +111,6 @@ async def notify_revenue_analytics_that_sync_has_completed(try:- @database_sync_to_async_pooldef _check_and_notify():if (schema.name == STRIPE_CHARGE_RESOURCE_NAME@@ -133,7 +132,7 @@ async def notify_revenue_analytics_that_sync_has_completed(schema.team.revenue_analytics_config.notified_first_sync = Trueschema.team.revenue_analytics_config.save()- await _check_and_notify()+ await database_sync_to_async_pool(retry_on_operational_error(_check_and_notify))()except Exception as e:# Silently fail, we don't want this to crash the pipeline# Sending an email is not critical to the pipelinediff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_load.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_load.pyindex 7285542f3c4c..fbfc488568c1 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_load.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/common/test/test_load.py@@ -15,10 +15,15 @@ from products.warehouse_sources.backend.temporal.data_imports.external_data_jobfrom products.warehouse_sources.backend.temporal.data_imports.pipelines.common.load import (IncrementalFieldMissingFromDataError,get_incremental_field_value,+ notify_revenue_analytics_that_sync_has_completed,run_post_load_operations,update_job_row_count,)from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.maintenance import DeltaMaintenance+from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.constants import (+ CHARGE_RESOURCE_NAME as STRIPE_CHARGE_RESOURCE_NAME,+)+from products.warehouse_sources.backend.types import ExternalDataSourceType_LOAD_MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.common.load"_DB_RETRY_MODULE = "products.warehouse_sources.backend.temporal.data_imports.pipelines.common.db_retry"@@ -404,3 +409,39 @@ class TestUpdateJobRowCount:assert update.call_count == 2close.assert_called_once()sleep.assert_called_once_with(2)+++class TestNotifyRevenueAnalyticsThatSyncHasCompleted:+ @pytest.mark.asyncio+ async def test_retries_transient_operational_error_then_notifies(self):+ # Opening a fresh pooled Postgres connection from the Temporal worker's thread pool can+ # hit a momentary DNS resolution blip; retrying it is safe and avoids silently skipping+ # the "revenue analytics ready" notification over a transient failure.+ attempts = MagicMock(side_effect=[OperationalError("Name or service not known"), True])++ class _RevenueAnalyticsConfig:+ @property+ def enabled(self):+ return attempts()++ source = MagicMock(+ source_type=ExternalDataSourceType.STRIPE, revenue_analytics_config=_RevenueAnalyticsConfig()+ )+ schema = MagicMock()+ schema.name = STRIPE_CHARGE_RESOURCE_NAME+ schema.team.revenue_analytics_config.notified_first_sync = False+ schema.team.all_users_with_access.return_value = []+ logger = MagicMock(aexception=AsyncMock())++ with (+ patch(f"{_DB_RETRY_MODULE}.close_old_connections") as close,+ patch(f"{_DB_RETRY_MODULE}.time.sleep") as sleep,+ ):+ await notify_revenue_analytics_that_sync_has_completed(schema, source, logger)++ assert attempts.call_count == 2+ close.assert_called_once()+ sleep.assert_called_once_with(2)+ assert schema.team.revenue_analytics_config.notified_first_sync is True+ schema.team.revenue_analytics_config.save.assert_called_once()+ logger.aexception.assert_not_called()