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.

feat(data-warehouse): add --created-after window bound to unstick command (#69424)

Daniel RC276967783e3e
diff --git a/products/warehouse_sources/backend/management/commands/unstick_external_data_jobs.py b/products/warehouse_sources/backend/management/commands/unstick_external_data_jobs.py
index 349374a42cde..14fcac395885 100644
--- a/products/warehouse_sources/backend/management/commands/unstick_external_data_jobs.py
+++ b/products/warehouse_sources/backend/management/commands/unstick_external_data_jobs.py
@@ -80,6 +80,14 @@ class Command(BaseCommand):
required=True,
help="Only consider Running jobs created before this ISO-8601 timestamp (e.g. 2026-07-02T00:00:00Z)",
)
+ parser.add_argument(
+ "--created-after",
+ type=str,
+ help=(
+ "Only consider Running jobs created at or after this ISO-8601 timestamp. "
+ "Bounds the sweep to an incident window instead of the full historical backlog."
+ ),
+ )
parser.add_argument("--team-id", type=int, help="Scope by team")
parser.add_argument("--source-type", type=str, help="Scope by source type, e.g. Stripe")
parser.add_argument(
@@ -106,7 +114,7 @@ class Command(BaseCommand):
parser.add_argument("--yes", action="store_true", help="Skip interactive confirmation")
def handle(self, *args: Any, **options: Any) -> None:
- cutoff = self._parse_cutoff(options["created_before"])
+ cutoff = self._parse_cutoff(options["created_before"], flag="--created-before")
live_run: bool = options["live_run"]
reason: str = options["reason"]
terminate_healthy: bool = options["terminate_healthy"]
@@ -115,6 +123,8 @@ class Command(BaseCommand):
jobs = ExternalDataJob.objects.filter(status=ExternalDataJob.Status.RUNNING, created_at__lt=cutoff).order_by(
"created_at"
)
+ if options.get("created_after"):
+ jobs = jobs.filter(created_at__gte=self._parse_cutoff(options["created_after"], flag="--created-after"))
if options.get("team_id") is not None:
jobs = jobs.filter(team_id=options["team_id"])
if options.get("source_type"):
@@ -327,11 +337,11 @@ class Command(BaseCommand):
)
@staticmethod
- def _parse_cutoff(raw: str) -> datetime:
+ def _parse_cutoff(raw: str, *, flag: str) -> datetime:
try:
cutoff = datetime.fromisoformat(raw.replace("Z", "+00:00"))
except ValueError:
- raise CommandError(f"--created-before is not a valid ISO-8601 timestamp: {raw!r}")
+ raise CommandError(f"{flag} is not a valid ISO-8601 timestamp: {raw!r}")
if cutoff.tzinfo is None:
cutoff = cutoff.replace(tzinfo=UTC)
return cutoff
diff --git a/products/warehouse_sources/backend/tests/management/test_unstick_external_data_jobs.py b/products/warehouse_sources/backend/tests/management/test_unstick_external_data_jobs.py
index fa80a4f0d9a9..815bdd8199ff 100644
--- a/products/warehouse_sources/backend/tests/management/test_unstick_external_data_jobs.py
+++ b/products/warehouse_sources/backend/tests/management/test_unstick_external_data_jobs.py
@@ -251,6 +251,19 @@ class TestSafetyRails:
job.refresh_from_db()
assert job.status == ExternalDataJob.Status.RUNNING
+ def test_created_after_excludes_older_backlog(self, team, fake_temporal, queue_conn):
+ _, old_zombie = _create_stuck_job(team, created_at=datetime(2024, 9, 1, tzinfo=UTC))
+ schema, incident_job = _create_stuck_job(team, created_at=BEFORE_CUTOFF)
+ assert incident_job.workflow_run_id is not None
+ fake_temporal.describe_results[incident_job.workflow_run_id] = _wedged()
+
+ _call("--created-after", "2026-06-28T00:00:00Z", "--live-run", "--yes")
+
+ old_zombie.refresh_from_db()
+ incident_job.refresh_from_db()
+ assert old_zombie.status == ExternalDataJob.Status.RUNNING
+ assert incident_job.status == ExternalDataJob.Status.FAILED
+
def test_max_jobs_cap_aborts_before_any_write(self, team, fake_temporal, queue_conn):
_, job_a = _create_stuck_job(team)
_, job_b = _create_stuck_job(team)