Skip to content

Commit d9cb673

Browse files
czueclaude
andcommitted
Auto-clear stale warehouse runs that block future executions
If a warehouse run is killed mid-execution, the ReportRun record is left with complete=False, blocking all future runs. This adds a self-healing check that marks runs older than 72 hours as failed before checking the lock. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a022e1b commit d9cb673

File tree

1 file changed

+11
-1
lines changed
  • ex-submodules/django-datawarehouse/warehouse

1 file changed

+11
-1
lines changed

ex-submodules/django-datawarehouse/warehouse/runner.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import print_function
22
from __future__ import unicode_literals
33
from builtins import object
4-
from datetime import datetime
4+
from datetime import datetime, timedelta
55
from logistics_project.utils.modules import to_function
66
from django.conf import settings
77
from warehouse.models import ReportRun
88
from django.db import transaction
99
from django.db.utils import DatabaseError
1010

11+
STALE_RUN_TIMEOUT_HOURS = 72
12+
1113

1214
class WarehouseRunner(object):
1315
"""
@@ -71,6 +73,14 @@ def update_warehouse(start_date=None, end_date=None, cleanup=False):
7173
if cleanup:
7274
runner.cleanup(start_date, end_date)
7375

76+
# Mark any runs that have been "in progress" for too long as failed,
77+
# since they were likely killed mid-execution and will block all future runs.
78+
stale_threshold = datetime.utcnow() - timedelta(hours=STALE_RUN_TIMEOUT_HOURS)
79+
stale_runs = ReportRun.objects.filter(complete=False, start_run__lt=stale_threshold)
80+
if stale_runs.exists():
81+
print("Marking %s stale run(s) as failed" % stale_runs.count())
82+
stale_runs.update(complete=True, has_error=True, end_run=datetime.utcnow())
83+
7484
running = ReportRun.objects.filter(complete=False)
7585
if running.count() > 0:
7686
raise Exception("Warehouse already running, will do nothing...")

0 commit comments

Comments
 (0)