: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(batch-exports): skip on-demand failure emails (#86976)
Rosse91eefe843c6
diff --git a/posthog/tasks/email.py b/posthog/tasks/email.pyindex e558ed214e5a..24becaa536f5 100644--- a/posthog/tasks/email.py+++ b/posthog/tasks/email.py@@ -769,6 +769,10 @@ def send_batch_export_run_failure("batch_export__team", "batch_export_on_demand__team").get(id=batch_export_run_id)batch_export = batch_export_run.parent+ # On-demand exports do not have a page for this email to link to.+ if not isinstance(batch_export, BatchExport):+ return+team: Team = batch_export.teampipeline_id = f"batch_export:{batch_export.id}"@@ -783,11 +787,7 @@ def send_batch_export_run_failure(campaign_key: str = f"batch_export_run_email_batch_export_{batch_export.id}_last_updated_at_{last_updated_at_date}"- subject = (- f"PostHog: {batch_export.name} batch export run failure"- if isinstance(batch_export, BatchExport)- else "PostHog: batch export on demand run failure"- )+ subject = f"PostHog: {batch_export.name} batch export run failure"message = EmailMessage(campaign_key=campaign_key,subject=subject,@@ -796,7 +796,7 @@ def send_batch_export_run_failure("time": batch_export_run.last_updated_at.strftime("%I:%M%p %Z on %B %d"),"team": team,"id": batch_export.id,- "name": batch_export.name if isinstance(batch_export, BatchExport) else "",+ "name": batch_export.name,},)logger.info("Prepared notification email for campaign %s", campaign_key)diff --git a/posthog/tasks/test/test_email.py b/posthog/tasks/test/test_email.pyindex dd096f57bb60..cce62d79f166 100644--- a/posthog/tasks/test/test_email.py+++ b/posthog/tasks/test/test_email.py@@ -19,6 +19,7 @@ from posthog.models.instance_setting import set_instance_settingfrom posthog.models.messaging import MessagingRecord, get_email_hashesfrom posthog.models.organization import OrganizationMembershipfrom posthog.models.organization_invite import OrganizationInvite+from posthog.models.scoping import team_scopefrom posthog.tasks.email import (get_members_to_notify_for_pipeline_error,login_from_new_device_notification,@@ -49,7 +50,12 @@ from posthog.tasks.email import (from posthog.tasks.test.utils_email_tests import mock_email_messagesfrom posthog.test.api_keys import create_project_secret_api_key-from products.batch_exports.backend.models.batch_export import BatchExport, BatchExportDestination, BatchExportRun+from products.batch_exports.backend.models.batch_export import (+ BatchExport,+ BatchExportDestination,+ BatchExportOnDemand,+ BatchExportRun,+)from products.cdp.backend.models.hog_functions.hog_function import HogFunctionfrom products.cdp.backend.models.plugin import Plugin, PluginConfigfrom products.data_modeling.backend.facade.models import DataModelingJob, DataModelingJobEngine, DataWarehouseSavedQuery@@ -587,6 +593,25 @@ class TestEmail(APIBaseTest, ClickhouseTestMixin):assert mocked_email_messages[0].send.call_count == 1assert mocked_email_messages[0].html_body+ def test_does_not_send_batch_export_run_failure_for_on_demand_export(self, MockEmailMessage: MagicMock) -> None:+ mocked_email_messages = mock_email_messages(MockEmailMessage)+ destination = BatchExportDestination.objects.create(+ type=BatchExportDestination.Destination.S3, config={"bucket_name": "my_production_s3_bucket"}+ )+ with team_scope(team_id=self.team.pk, canonical=True):+ on_demand_export = BatchExportOnDemand.objects.create(team=self.team, destination=destination)+ now = dt.datetime.now()+ batch_export_run = BatchExportRun.objects.create(+ batch_export_on_demand=on_demand_export,+ status=BatchExportRun.Status.FAILED,+ data_interval_start=now - dt.timedelta(hours=1),+ data_interval_end=now,+ )++ send_batch_export_run_failure(batch_export_run.id)++ assert mocked_email_messages == []+def test_send_batch_export_run_failure_with_settings(self, MockEmailMessage: MagicMock) -> None:mocked_email_messages = mock_email_messages(MockEmailMessage)batch_export_destination = BatchExportDestination.objects.create(