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(warehouse): finalize zero-batch data-import jobs via workflow_run_id (#70443)

estefaniabe1bdfb83a0c
diff --git a/posthog/temporal/tests/external_data/test_external_data_job.py b/posthog/temporal/tests/external_data/test_external_data_job.py
index 68f277f0dec3..ad2128756f47 100644
--- a/posthog/temporal/tests/external_data/test_external_data_job.py
+++ b/posthog/temporal/tests/external_data/test_external_data_job.py
@@ -411,6 +411,64 @@ async def test_update_external_job_activity(activity_environment, team, **kwargs
assert schema.status == ExternalDataJob.Status.COMPLETED
[email protected]_db(transaction=True)
+async def test_update_external_job_activity_resolves_job_by_workflow_run_id_when_job_id_missing(
+ activity_environment, team, **kwargs
+):
+ # A schema can carry several stranded RUNNING rows, so finalizing a zero-batch run (job_id=None)
+ # must resolve this run's job by workflow_run_id, not the newest RUNNING job for the schema.
+ new_source = await sync_to_async(ExternalDataSource.objects.create)(
+ source_id=str(uuid.uuid4()),
+ connection_id=str(uuid.uuid4()),
+ destination_id=str(uuid.uuid4()),
+ team=team,
+ status="running",
+ source_type="Slack",
+ )
+ schema = await sync_to_async(ExternalDataSchema.objects.create)(
+ name="C09M2GF0M8R",
+ team_id=team.id,
+ source_id=new_source.pk,
+ should_sync=True,
+ )
+
+ # This run's job, created first so it is NOT the newest RUNNING row for the schema.
+ this_run_job = await _create_external_data_job(
+ team_id=team.id,
+ external_data_source_id=new_source.pk,
+ workflow_id=activity_environment.info.workflow_id,
+ workflow_run_id=activity_environment.info.workflow_run_id,
+ external_data_schema_id=schema.id,
+ )
+ # A newer, unrelated RUNNING job for the same schema — what the positional heuristic would pick.
+ other_run_job = await _create_external_data_job(
+ team_id=team.id,
+ external_data_source_id=new_source.pk,
+ workflow_id=str(uuid.uuid4()),
+ workflow_run_id=str(uuid.uuid4()),
+ external_data_schema_id=schema.id,
+ )
+
+ inputs = UpdateExternalDataJobStatusInputs(
+ job_id=None,
+ status=ExternalDataJob.Status.COMPLETED,
+ latest_error=None,
+ internal_error=None,
+ schema_id=str(schema.pk),
+ source_id=str(new_source.pk),
+ team_id=team.id,
+ workflow_run_id=activity_environment.info.workflow_run_id,
+ )
+
+ await activity_environment.run(update_external_data_job_model, inputs)
+ await sync_to_async(this_run_job.refresh_from_db)()
+ await sync_to_async(other_run_job.refresh_from_db)()
+
+ assert this_run_job.status == ExternalDataJob.Status.COMPLETED
+ assert other_run_job.status == ExternalDataJob.Status.RUNNING
+
+
@pytest.mark.django_db(transaction=True)
@pytest.mark.asyncio
async def test_update_external_job_activity_with_retryable_error(activity_environment, team, **kwargs):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py b/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
index 13f64a73f309..79a739d6cb94 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/external_data_job.py
@@ -128,6 +128,9 @@ class UpdateExternalDataJobStatusInputs:
status: str
internal_error: str | None
latest_error: str | None
+ # Run id stamped on the job row by the create-job activity, so finalization can resolve this
+ # run's own job when job_id never made it back. Optional for mixed-version workers mid-rollout.
+ workflow_run_id: str | None = None
@property
def properties_to_log(self) -> dict[str, typing.Any]:
@@ -137,6 +140,7 @@ class UpdateExternalDataJobStatusInputs:
"schema_id": self.schema_id,
"source_id": self.source_id,
"status": self.status,
+ "workflow_run_id": self.workflow_run_id,
}
@@ -154,15 +158,34 @@ async def update_external_data_job_model(inputs: UpdateExternalDataJobStatusInpu
await finish_row_tracking(inputs.team_id, inputs.schema_id)
if inputs.job_id is None:
- job: ExternalDataJob | None = await database_sync_to_async_pool(
- lambda: (
+
+ def _resolve_job() -> ExternalDataJob | None:
+ # Resolve this run's own job by run id; the finally-block update is the only finalizer for
+ # zero-batch runs (e.g. quiet Slack channels) that never send a batch to complete the job.
+ if inputs.workflow_run_id is not None:
+ job = (
+ ExternalDataJob.objects.filter(team_id=inputs.team_id, workflow_run_id=inputs.workflow_run_id)
+ .order_by("-created_at")
+ .first()
+ )
+ if job is not None:
+ return job
+ # Legacy fallback for runs started before workflow_run_id existed; racy under concurrent runs.
+ return (
ExternalDataJob.objects.filter(schema_id=inputs.schema_id, status=ExternalDataJob.Status.RUNNING)
.order_by("-created_at")
.first()
)
- )()
+
+ job: ExternalDataJob | None = await database_sync_to_async_pool(_resolve_job)()
if job is None:
- logger.info("No job to update status on")
+ # A FAILED finalization with no resolvable job means an early activity (e.g. create-job)
+ # failed before a row was committed — nothing is stranded and that failure is already
+ # reported on its own, so don't double-alarm. A non-FAILED finalization that can't find
+ # its job is a real anomaly (work we think succeeded has nowhere to record it) — surface it.
+ logger.warning("No job to update status on", workflow_run_id=inputs.workflow_run_id)
+ if inputs.status != ExternalDataJob.Status.FAILED:
+ capture_exception(Exception("Data import finalization could not resolve a job to update"))
return
job_id = str(job.pk)
@@ -306,6 +329,8 @@ class ExternalDataJobWorkflow(PostHogWorkflow):
team_id=inputs.team_id,
schema_id=str(inputs.external_data_schema_id),
source_id=str(inputs.external_data_source_id),
+ # Deterministic and available immediately, so the finalizer can resolve this run's job.
+ workflow_run_id=workflow.info().run_id,
)
source_type = None