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(data-warehouse): stamp last_run_at on failed and cancelled materialization runs (#76959)

Thiago Salvatore856ba357aa3a
diff --git a/posthog/temporal/data_modeling/activities/fail_materialization.py b/posthog/temporal/data_modeling/activities/fail_materialization.py
index f7121faa1319..889145c10b46 100644
--- a/posthog/temporal/data_modeling/activities/fail_materialization.py
+++ b/posthog/temporal/data_modeling/activities/fail_materialization.py
@@ -1,3 +1,4 @@
+import datetime as dt
import dataclasses
from typing import TYPE_CHECKING
from uuid import UUID
@@ -102,6 +103,7 @@ def _fail_node_and_data_modeling_job(inputs: FailMaterializationInputs):
job.status = DataModelingJobStatus.CANCELLED if inputs.cancelled else DataModelingJobStatus.FAILED
job.rows_materialized = 0
job.error = sanitized_error
+ job.last_run_at = dt.datetime.now(dt.UTC)
job.save()
return node, job
diff --git a/posthog/temporal/data_modeling/run_workflow.py b/posthog/temporal/data_modeling/run_workflow.py
index 86c645bce153..0eb383a91787 100644
--- a/posthog/temporal/data_modeling/run_workflow.py
+++ b/posthog/temporal/data_modeling/run_workflow.py
@@ -424,6 +424,7 @@ async def handle_error(
job.status = DataModelingJob.Status.FAILED
job.rows_materialized = 0
job.error = strip_hostname_from_error(error_str)
+ job.last_run_at = dt.datetime.now(dt.UTC)
await database_sync_to_async(job.save)()
await queue.put(
QueueMessage(status=ModelStatus.FAILED, label=model.label, error=strip_hostname_from_error(error_str))
@@ -444,6 +445,7 @@ async def handle_cancelled(
job.status = DataModelingJob.Status.CANCELLED
job.rows_materialized = 0
job.error = strip_hostname_from_error(error_str)
+ job.last_run_at = dt.datetime.now(dt.UTC)
await database_sync_to_async(job.save)()
await queue.put(
QueueMessage(status=ModelStatus.FAILED, label=model.label, error=strip_hostname_from_error(error_str))
@@ -766,6 +768,7 @@ async def mark_job_as_failed(job: DataModelingJob, error_message: str, logger: F
job.status = DataModelingJob.Status.FAILED
job.rows_materialized = 0
job.error = strip_hostname_from_error(error_message)
+ job.last_run_at = dt.datetime.now(dt.UTC)
await database_sync_to_async(job.save)()
@@ -1496,6 +1499,7 @@ def _preempt_running_jobs(team_id: int, saved_query_ids: list[str] | None = None
rows_materialized=0,
error="Preempted: This job did not complete before the next scheduled job was triggered.",
updated_at=dt.datetime.now(dt.UTC),
+ last_run_at=dt.datetime.now(dt.UTC),
)
return orphaned_jobs
@@ -1634,9 +1638,17 @@ async def cancel_jobs_activity(inputs: CancelJobsActivityInputs) -> None:
bind_contextvars(team_id=inputs.team_id)
logger = LOGGER.bind()
+ # updated_at is auto_now, which QuerySet.update() skips, so it has to be set by hand here to
+ # stay in step with last_run_at.
+ cancelled_at = dt.datetime.now(dt.UTC)
await database_sync_to_async(
DataModelingJob.objects.filter(workflow_id=inputs.workflow_id, workflow_run_id=inputs.workflow_run_id).update
- )(status=DataModelingJob.Status.CANCELLED, rows_materialized=0)
+ )(
+ status=DataModelingJob.Status.CANCELLED,
+ rows_materialized=0,
+ last_run_at=cancelled_at,
+ updated_at=cancelled_at,
+ )
await logger.ainfo(
"Cancelled data modeling jobs", workflow_id=inputs.workflow_id, workflow_run_id=inputs.workflow_run_id
)
diff --git a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
index 9949e7e8d223..072bf64593ab 100644
--- a/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
+++ b/posthog/temporal/tests/data_modeling/test_materialize_view_activities.py
@@ -82,19 +82,48 @@ class TestCreateDataModelingJobActivity:
class TestFailMaterializationActivity:
- async def test_marks_job_as_failed(self, activity_environment, ateam, anode, ajob, adag):
+ @pytest.mark.parametrize(
+ "cancelled,expected_status",
+ [(False, DataModelingJob.Status.FAILED), (True, DataModelingJob.Status.CANCELLED)],
+ )
+ async def test_marks_job_as_terminal(
+ self, activity_environment, ateam, anode, ajob, adag, cancelled, expected_status
+ ):
inputs = FailMaterializationInputs(
team_id=ateam.pk,
node_id=str(anode.id),
dag_id=str(adag.id),
job_id=str(ajob.id),
error="Test error message",
+ cancelled=cancelled,
)
await activity_environment.run(fail_materialization_activity, inputs)
await database_sync_to_async(ajob.refresh_from_db)()
- assert ajob.status == DataModelingJob.Status.FAILED
+ assert ajob.status == expected_status
assert ajob.rows_materialized == 0
assert ajob.error == "Test error message"
+ # The UI derives run duration and the log-search window from last_run_at, so a
+ # terminal transition must stamp it (the model default is the job's start time).
+ assert ajob.last_run_at > ajob.created_at
+
+ async def test_does_not_overwrite_already_terminal_job(self, activity_environment, ateam, anode, ajob, adag):
+ ajob.status = DataModelingJob.Status.COMPLETED
+ await database_sync_to_async(ajob.save)()
+ await database_sync_to_async(ajob.refresh_from_db)()
+ completed_last_run_at = ajob.last_run_at
+
+ inputs = FailMaterializationInputs(
+ team_id=ateam.pk,
+ node_id=str(anode.id),
+ dag_id=str(adag.id),
+ job_id=str(ajob.id),
+ error="Late failure",
+ )
+ await activity_environment.run(fail_materialization_activity, inputs)
+ await database_sync_to_async(ajob.refresh_from_db)()
+ assert ajob.status == DataModelingJob.Status.COMPLETED
+ assert ajob.error is None
+ assert ajob.last_run_at == completed_last_run_at
async def test_updates_node_system_properties(self, activity_environment, ateam, anode, ajob, adag):
inputs = FailMaterializationInputs(
diff --git a/products/data_warehouse/backend/presentation/views/data_modeling_job.py b/products/data_warehouse/backend/presentation/views/data_modeling_job.py
index 4e819640be46..9f851f704b15 100644
--- a/products/data_warehouse/backend/presentation/views/data_modeling_job.py
+++ b/products/data_warehouse/backend/presentation/views/data_modeling_job.py
@@ -12,6 +12,11 @@ DUCKGRES_SHADOW_FLAG = "duckgres-data-modeling-shadow"
class DataModelingJobSerializer(serializers.ModelSerializer):
+ updated_at = serializers.DateTimeField(
+ read_only=True,
+ help_text="When the job row last changed. For finished jobs this is when the run reached its terminal status.",
+ )
+
class Meta:
model = DataModelingJob
fields = [
@@ -22,6 +27,7 @@ class DataModelingJobSerializer(serializers.ModelSerializer):
"error",
"created_at",
"last_run_at",
+ "updated_at",
"workflow_id",
"workflow_run_id",
"rows_expected",
diff --git a/products/data_warehouse/backend/tests/api/test_datamodelingjob.py b/products/data_warehouse/backend/tests/api/test_datamodelingjob.py
index 574245a15cfc..7921b3a849fe 100644
--- a/products/data_warehouse/backend/tests/api/test_datamodelingjob.py
+++ b/products/data_warehouse/backend/tests/api/test_datamodelingjob.py
@@ -69,6 +69,8 @@ class TestDataModelingJob(APIBaseTest):
self.assertEqual(first_job["saved_query_id"], str(self.saved_query.id))
self.assertEqual(first_job["rows_materialized"], 0)
self.assertEqual(first_job["error"], "Something went wrong")
+ # The frontend derives failed-run duration and the log-search window from updated_at.
+ self.assertIsNotNone(first_job["updated_at"])
def test_retrieve_data_modeling_job(self):
response = self.client.get(f"/api/environments/{self.team.pk}/data_modeling_jobs/{self.job1.id}/")
diff --git a/products/data_warehouse/frontend/generated/api.schemas.ts b/products/data_warehouse/frontend/generated/api.schemas.ts
index 3d7fa4b7d428..4f38500f69bd 100644
--- a/products/data_warehouse/frontend/generated/api.schemas.ts
+++ b/products/data_warehouse/frontend/generated/api.schemas.ts
@@ -33,6 +33,8 @@ export interface DataModelingJobApi {
readonly error: string | null
readonly created_at: string
readonly last_run_at: string
+ /** When the job row last changed. For finished jobs this is when the run reached its terminal status. */
+ readonly updated_at: string
/** @nullable */
readonly workflow_id: string | null
/** @nullable */
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 32ab4899ae88..fce55ed385a7 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -17507,6 +17507,8 @@ export namespace Schemas {
readonly error: string | null;
readonly created_at: string;
readonly last_run_at: string;
+ /** When the job row last changed. For finished jobs this is when the run reached its terminal status. */
+ readonly updated_at: string;
/** @nullable */
readonly workflow_id: string | null;
/** @nullable */