|
1 | 1 | from __future__ import print_function |
2 | 2 | from __future__ import unicode_literals |
3 | 3 | from builtins import object |
4 | | -from datetime import datetime |
| 4 | +from datetime import datetime, timedelta |
5 | 5 | from logistics_project.utils.modules import to_function |
6 | 6 | from django.conf import settings |
7 | 7 | from warehouse.models import ReportRun |
8 | 8 | from django.db import transaction |
9 | 9 | from django.db.utils import DatabaseError |
10 | 10 |
|
| 11 | +STALE_RUN_TIMEOUT_HOURS = 72 |
| 12 | + |
11 | 13 |
|
12 | 14 | class WarehouseRunner(object): |
13 | 15 | """ |
@@ -71,6 +73,14 @@ def update_warehouse(start_date=None, end_date=None, cleanup=False): |
71 | 73 | if cleanup: |
72 | 74 | runner.cleanup(start_date, end_date) |
73 | 75 |
|
| 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 | + |
74 | 84 | running = ReportRun.objects.filter(complete=False) |
75 | 85 | if running.count() > 0: |
76 | 86 | raise Exception("Warehouse already running, will do nothing...") |
|
0 commit comments