Skip to content

: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.

fix(endpoints): revert stale materializations that only ever fail (#96043)

Jovan Sakovic7c9a366cc61c
diff --git a/products/endpoints/backend/tasks/tasks.py b/products/endpoints/backend/tasks/tasks.py
index 52ea79b22abe..1b7bd8c0edd0 100644
--- a/products/endpoints/backend/tasks/tasks.py
+++ b/products/endpoints/backend/tasks/tasks.py
@@ -60,7 +60,7 @@ def deactivate_stale_materializations() -> None:
This task finds endpoint versions where:
1. The version has an active materialization (saved_query.is_materialized = True)
- 2. The materialization has run in the past 24h (a completed job, or saved_query.last_run_at)
+ 2. The materialization has run in the past 24h (a finished job, or saved_query.last_run_at)
3. The materialization was enabled at least 30 days ago (saved_query.created_at)
4. The version was last executed over 30 days ago (via API key), or it is a superseded
version that was never executed
@@ -71,11 +71,11 @@ def deactivate_stale_materializations() -> None:
twenty_four_hours_ago = now - timedelta(hours=24)
stale_threshold = now - timedelta(days=STALE_THRESHOLD_DAYS)
+ # A still-running job does not count: reverting soft-deletes the saved query under a live workflow.
recent_job = DataModelingJob.objects.filter(
saved_query_id=OuterRef("saved_query_id"),
- status=DataModelingJobStatus.COMPLETED,
last_run_at__gte=twenty_four_hours_ago,
- )
+ ).exclude(status=DataModelingJobStatus.RUNNING)
ran_recently = Q(saved_query__last_run_at__gte=twenty_four_hours_ago) | Q(Exists(recent_job))
# Both stamps are written together on every API-key call, so a superseded version with no stamp
diff --git a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
index 569906d33af5..b5d084c50464 100644
--- a/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
+++ b/products/endpoints/backend/tests/test_deactivate_stale_materializations.py
@@ -222,8 +222,17 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
version.refresh_from_db()
assert version.saved_query is None
+ @parameterized.expand(
+ [
+ ("completed", DataModelingJob.Status.COMPLETED, True),
+ ("failed", DataModelingJob.Status.FAILED, True),
+ ("still_running", DataModelingJob.Status.RUNNING, False),
+ ]
+ )
@mock.patch("products.endpoints.backend.tasks.tasks._deactivate_version_materialization")
- def test_selects_stale_version_whose_saved_query_last_run_at_is_never_written(self, mock_deactivate):
+ def test_selects_stale_version_from_a_recent_finished_job(
+ self, _name, job_status, expect_deactivation, mock_deactivate
+ ):
now = timezone.now()
_, version = self._create_materialized_endpoint(
name="v2_scheduled",
@@ -234,12 +243,15 @@ class TestDeactivateStaleMaterializationsTask(BaseTest):
DataModelingJob.objects.create(
team=self.team,
saved_query=version.saved_query,
- status=DataModelingJob.Status.COMPLETED,
+ status=job_status,
last_run_at=now - timedelta(hours=1),
)
deactivate_stale_materializations()
+ if not expect_deactivation:
+ mock_deactivate.assert_not_called()
+ return
mock_deactivate.assert_called_once()
assert mock_deactivate.call_args[0][0].id == version.id