: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): exclude deleted sources and dedupe digest (#66000)
Marcus Hofb31eae743bc8
diff --git a/products/data_warehouse/backend/external_data_source/notifications.py b/products/data_warehouse/backend/external_data_source/notifications.pyindex 5bdc15b9218f..c211b0fad5e5 100644--- a/products/data_warehouse/backend/external_data_source/notifications.py+++ b/products/data_warehouse/backend/external_data_source/notifications.py@@ -45,6 +45,7 @@ def get_team_ids_with_recent_sync_failures(lookback: dt.timedelta = dt.timedelta).filter(Q(schema__last_error_notified_at__isnull=True) | Q(finished_at__gt=OuterRef("last_error_notified_at")))return list(ExternalDataSchema.objects.exclude(deleted=True)+ .exclude(source__deleted=True).filter(status=ExternalDataSchema.Status.FAILED).filter(Exists(unnotified_failed_job)).values_list("team_id", flat=True)@@ -53,17 +54,28 @@ def get_team_ids_with_recent_sync_failures(lookback: dt.timedelta = dt.timedeltadef notify_external_data_sync_failures(team_id: int) -> None:- """Email the team a digest of every currently-failing external data schema.-- Runs inside the digest Celery task; exceptions are swallowed so a notification- problem never crash-loops the task. Throttling to one email per team per digest- day happens in the email layer via the MessagingRecord campaign key, so+ """Email the team a digest of failing external data schemas with an un-communicated failure.++ A failure is reported once: a schema is listed only if it was never notified or+ has a failed run newer than its last notification. A stuck-but-stopped sync+ (deleted source, paused schema) has no newer run, so it drops out after the first+ digest instead of riding every later one. Schemas of a deleted source are excluded+ entirely. Runs inside the digest Celery task; exceptions are swallowed so a+ notification problem never crash-loops the task. Throttling to one email per team+ per digest day happens in the email layer via the MessagingRecord campaign key, soscheduling this for every failed job is safe."""try:+ newer_failed_job = ExternalDataJob.objects.filter(+ schema_id=OuterRef("id"),+ status=ExternalDataJob.Status.FAILED,+ finished_at__gt=OuterRef("last_error_notified_at"),+ )failing_schemas = list(ExternalDataSchema.objects.exclude(deleted=True)+ .exclude(source__deleted=True).filter(team_id=team_id, status=ExternalDataSchema.Status.FAILED)+ .filter(Q(last_error_notified_at__isnull=True) | Exists(newer_failed_job)).select_related("source")# Paused (should_sync=False) schemas first — they need user action..order_by("should_sync", "name")diff --git a/products/data_warehouse/backend/external_data_source/test/test_notifications.py b/products/data_warehouse/backend/external_data_source/test/test_notifications.pyindex e1ac2032b23b..0ee59d37c3a6 100644--- a/products/data_warehouse/backend/external_data_source/test/test_notifications.py+++ b/products/data_warehouse/backend/external_data_source/test/test_notifications.py@@ -213,6 +213,50 @@ class TestNotifyExternalDataSyncFailures:schema.refresh_from_db()assert schema.last_error_notified_at is None+ def test_excludes_schemas_of_deleted_source(self):+ team, source = _create_team_and_source()+ ExternalDataSource.objects.filter(id=source.id).update(deleted=True)+ ExternalDataSchema.objects.create(+ name="Charge",+ team=team,+ source=source,+ status=ExternalDataSchema.Status.FAILED,+ latest_error="boom",+ )++ with patch(SENDER_PATH) as mock_sender:+ notify_external_data_sync_failures(team.pk)++ mock_sender.assert_not_called()++ @pytest.mark.parametrize(+ "run_offset,expected_sent",+ [+ (dt.timedelta(hours=1), True), # failed run after the last notification -> report again+ (dt.timedelta(hours=-1), False), # no run since the last notification -> stay silent+ ],+ )+ def test_already_notified_failure_renotifies_only_on_newer_run(self, run_offset, expected_sent):+ team, source = _create_team_and_source()+ notified_at = dt.datetime.now(dt.UTC) - dt.timedelta(hours=2)+ schema = ExternalDataSchema.objects.create(+ name="Charge",+ team=team,+ source=source,+ status=ExternalDataSchema.Status.FAILED,+ latest_error="boom",+ last_error_notified_at=notified_at,+ )+ job = ExternalDataJob.objects.create(+ team=team, pipeline=source, schema=schema, status=ExternalDataJob.Status.FAILED+ )+ ExternalDataJob.objects.filter(id=job.id).update(finished_at=notified_at + run_offset)++ with patch(SENDER_PATH) as mock_sender:+ notify_external_data_sync_failures(team.pk)++ assert mock_sender.called == expected_sent+class TestGetTeamIdsWithRecentSyncFailures:def _create_schema_with_job(@@ -221,9 +265,12 @@ class TestGetTeamIdsWithRecentSyncFailures:schema_status: str,job_finished_at: dt.datetime,schema_deleted: bool = False,+ source_deleted: bool = False,last_error_notified_at: dt.datetime | None = None,) -> Team:team, source = _create_team_and_source()+ if source_deleted:+ ExternalDataSource.objects.filter(id=source.id).update(deleted=True)schema = ExternalDataSchema.objects.create(name="Charge",team=team,@@ -251,18 +298,20 @@ class TestGetTeamIdsWithRecentSyncFailures:assert get_team_ids_with_recent_sync_failures() == [team.pk]@pytest.mark.parametrize(- "schema_status,job_age,schema_deleted",+ "schema_status,job_age,schema_deleted,source_deleted",[- (ExternalDataSchema.Status.FAILED, dt.timedelta(hours=30), False),- (ExternalDataSchema.Status.COMPLETED, dt.timedelta(hours=2), False),- (ExternalDataSchema.Status.FAILED, dt.timedelta(hours=2), True),+ (ExternalDataSchema.Status.FAILED, dt.timedelta(hours=30), False, False),+ (ExternalDataSchema.Status.COMPLETED, dt.timedelta(hours=2), False, False),+ (ExternalDataSchema.Status.FAILED, dt.timedelta(hours=2), True, False),+ (ExternalDataSchema.Status.FAILED, dt.timedelta(hours=2), False, True),],)- def test_excludes_non_actionable_teams(self, schema_status, job_age, schema_deleted):+ def test_excludes_non_actionable_teams(self, schema_status, job_age, schema_deleted, source_deleted):self._create_schema_with_job(schema_status=schema_status,job_finished_at=dt.datetime.now(dt.UTC) - job_age,schema_deleted=schema_deleted,+ source_deleted=source_deleted,)assert get_team_ids_with_recent_sync_failures() == []