: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
feat(data-modeling): notify in-app on every failed manual run (#97292)
Jovan Sakovic3a49c115e06b
diff --git a/posthog/temporal/data_modeling/activities/notify_materialization_failure.py b/posthog/temporal/data_modeling/activities/notify_materialization_failure.pyindex e00a407c4e37..73d9b6e8a191 100644--- a/posthog/temporal/data_modeling/activities/notify_materialization_failure.py+++ b/posthog/temporal/data_modeling/activities/notify_materialization_failure.py@@ -131,27 +131,31 @@ class _PrecomputedViewers(RecipientsResolver):def maybe_notify_materialization_failure(job: DataModelingJob, saved_query: DataWarehouseSavedQuery, team_id: int) -> bool:- """Notify on the first failure of a streak; repeats of an ongoing streak stay quiet."""+ """Email on the first failure of a streak; a run started by hand also notifies in-app every time."""# An idempotent retry can land here with a job another path already completed or cancelled.if job.status != DataModelingJobStatus.FAILED:return False- if not starts_a_failure_streak(saved_query.id, job):- return False- # The email task dedupes per (recipient, job) via MessagingRecord, so an activity- # retry that already sent the in-app notification still can't double-send email.- send_matview_failure_immediate_email.delay(team_id, str(saved_query.id), str(job.id))+ if starts_a_failure_streak(saved_query.id, job):+ # The email task dedupes per (recipient, job) via MessagingRecord, so an activity+ # retry that already sent the in-app notification still can't double-send email.+ send_matview_failure_immediate_email.delay(team_id, str(saved_query.id), str(job.id))if job.parent_workflow_id:# The DAG run this belongs to notifies for every view it broke, once, at the end.return False+ # A run with no parent was started by hand, usually to check a fix, and the person who started+ # it may have moved on. Silence mid-streak would read as success, so every failure is told.create_notification(_failure_notification(team_id=team_id,views=[_FailedView(job=job, saved_query=saved_query)],resolver=_SavedQueryViewers(saved_query),source_id=str(job.id),+ # Someone asked for this run and is waiting on its result, so it outranks the+ # scheduled failures a person did not ask for.+ priority=Priority.CRITICAL,))return True@@ -183,7 +187,12 @@ def _dedupe_key(views: list[_FailedView]) -> str:def _failure_notification(- *, team_id: int, views: list[_FailedView], resolver: RecipientsResolver, source_id: str+ *,+ team_id: int,+ views: list[_FailedView],+ resolver: RecipientsResolver,+ source_id: str,+ priority: Priority = Priority.NORMAL,) -> NotificationData:title, body = _failure_copy(views)source_url = f"/project/{team_id}/sql"@@ -192,7 +201,7 @@ def _failure_notification(return NotificationData(team_id=team_id,notification_type=NotificationType.MATERIALIZATION_FAILURE,- priority=Priority.NORMAL,+ priority=priority,title=title[:255],body=body[:400],target_type=TargetType.TEAM,diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.pyindex c86d5beb777c..fca89a55e280 100644--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py@@ -59,7 +59,7 @@ from products.data_modeling.backend.facade.models import (NodeType,)from products.data_warehouse.backend.facade.api import CreateTableResult-from products.notifications.backend.facade.api import NotificationType, TargetType+from products.notifications.backend.facade.api import NotificationType, Priority, TargetTypefrom products.warehouse_sources.backend.facade.hooks import (AccountPropertySourceProjection,PersonPropertySourceProjection,@@ -201,20 +201,32 @@ class TestFailMaterializationActivity:assert is_node_suspended(anode, DataModelingJobEngine.CLICKHOUSE) is True@pytest.mark.parametrize(- "previous_status,expect_notification",+ "previous_status,parent_workflow_id,expect_email,expect_in_app",[- (None, True),- (DataModelingJob.Status.COMPLETED, True),- (DataModelingJob.Status.FAILED, False),+ (None, None, True, True),+ (DataModelingJob.Status.COMPLETED, None, True, True),+ (DataModelingJob.Status.FAILED, None, False, True),+ (DataModelingJob.Status.FAILED, "execute-dag-workflow", False, False),],)- async def test_notifies_only_on_first_failure_of_streak(- self, activity_environment, ateam, anode, asaved_query, adag, previous_status, expect_notification+ async def test_emails_at_streak_start_and_notifies_in_app_on_every_manual_run(+ self,+ activity_environment,+ ateam,+ anode,+ asaved_query,+ adag,+ previous_status,+ parent_workflow_id,+ expect_email,+ expect_in_app,):if previous_status is not None:error = "boom" if previous_status == DataModelingJob.Status.FAILED else Noneawait _make_job(ateam, asaved_query, previous_status, error=error)- current_job = await _make_job(ateam, asaved_query, DataModelingJob.Status.RUNNING)+ current_job = await _make_job(+ ateam, asaved_query, DataModelingJob.Status.RUNNING, parent_workflow_id=parent_workflow_id+ )inputs = FailMaterializationInputs(team_id=ateam.pk,@@ -223,15 +235,22 @@ class TestFailMaterializationActivity:job_id=str(current_job.id),error="Some non-timeout error",)- with unittest.mock.patch(- "posthog.temporal.data_modeling.activities.notify_materialization_failure.create_notification"- ) as mock_create:+ with (+ unittest.mock.patch(+ "posthog.temporal.data_modeling.activities.notify_materialization_failure.create_notification"+ ) as mock_create,+ unittest.mock.patch(+ "posthog.temporal.data_modeling.activities.notify_materialization_failure.send_matview_failure_immediate_email"+ ) as mock_email,+ ):await activity_environment.run(fail_materialization_activity, inputs)- if expect_notification:+ assert mock_email.delay.called == expect_email+ if expect_in_app:mock_create.assert_called_once()data = mock_create.call_args.args[0]assert data.notification_type == NotificationType.MATERIALIZATION_FAILURE+ assert data.priority == Priority.CRITICALassert data.target_id == str(ateam.pk)assert data.resource_id == str(asaved_query.id)else:@@ -253,11 +272,11 @@ class TestFailMaterializationActivity:error="Some non-timeout error",)with unittest.mock.patch(- "posthog.temporal.data_modeling.activities.notify_materialization_failure.create_notification"- ) as mock_create:+ "posthog.temporal.data_modeling.activities.notify_materialization_failure.send_matview_failure_immediate_email"+ ) as mock_email:await activity_environment.run(fail_materialization_activity, inputs)- mock_create.assert_not_called()+ mock_email.delay.assert_not_called()async def test_notifies_when_recovery_raises(self, activity_environment, ateam, anode, asaved_query, adag):current_job = await _make_job(ateam, asaved_query, DataModelingJob.Status.RUNNING)diff --git a/posthog/temporal/tests/data_modeling/test_notify_materialization_failure.py b/posthog/temporal/tests/data_modeling/test_notify_materialization_failure.pyindex e6292cb19045..85b36bb582d8 100644--- a/posthog/temporal/tests/data_modeling/test_notify_materialization_failure.py+++ b/posthog/temporal/tests/data_modeling/test_notify_materialization_failure.py@@ -13,7 +13,7 @@ from posthog.temporal.data_modeling.activities import (from posthog.temporal.data_modeling.activities.notify_materialization_failure import _FailedView, _failure_copyfrom products.data_modeling.backend.facade.models import DataModelingJob, DataModelingJobEngine, DataWarehouseSavedQuery-from products.notifications.backend.facade.api import TargetType+from products.notifications.backend.facade.api import Priority, TargetTypeRUN_STARTED_AT = "2026-08-12T10:00:00+00:00"# The shape a tier schedule produces, rather than an arbitrary long string.@@ -94,6 +94,7 @@ class TestNotifyDAGMaterializationFailures:data = mock_create.call_args.args[0]assert data.title == "3 views failed to materialize"assert data.body == "alpha, beta, gamma"+ assert data.priority == Priority.NORMALasync def test_an_earlier_run_under_the_same_workflow_id_is_left_out(self, activity_environment, ateam, adag, auser, aorganization