|
12 | 12 | )
|
13 | 13 |
|
14 | 14 | from django.db import models
|
15 |
| -from django.db.models import QuerySet |
| 15 | +from django.db.models import QuerySet, Case, When, IntegerField |
16 | 16 | from django.utils.translation import gettext_lazy
|
17 | 17 | from mots.config import FileConfig
|
18 | 18 | from mots.directory import Directory
|
|
25 | 25 | DEFAULT_GRACE_SECONDS = int(os.environ.get("DEFAULT_GRACE_SECONDS", 60 * 2))
|
26 | 26 |
|
27 | 27 |
|
| 28 | + |
28 | 29 | class LandingJobStatus(models.TextChoices):
|
29 |
| - SUBMITTED = "1_SUBMITTED", gettext_lazy("Submitted") |
30 |
| - IN_PROGRESS = "2_IN_PROGRESS", gettext_lazy("In progress") |
31 |
| - DEFERRED = "3_DEFERRED", gettext_lazy("Deferred") |
32 |
| - FAILED = "4_FAILED", gettext_lazy("Failed") |
33 |
| - LANDED = "5_LANDED", gettext_lazy("Landed") |
34 |
| - CANCELLED = "6_CANCELLED", gettext_lazy("Cancelled") |
| 30 | + SUBMITTED = "SUBMITTED", gettext_lazy("Submitted") |
| 31 | + IN_PROGRESS = "IN_PROGRESS", gettext_lazy("In progress") |
| 32 | + DEFERRED = "DEFERRED", gettext_lazy("Deferred") |
| 33 | + FAILED = "FAILED", gettext_lazy("Failed") |
| 34 | + LANDED = "LANDED", gettext_lazy("Landed") |
| 35 | + CANCELLED = "CANCELLED", gettext_lazy("Cancelled") |
35 | 36 |
|
36 | 37 |
|
37 | 38 | @enum.unique
|
@@ -217,7 +218,18 @@ def job_queue_query(
|
217 | 218 | # be a maximum of one (per repository). For
|
218 | 219 | # `LandingJobStatus.SUBMITTED` jobs, higher priority items come first
|
219 | 220 | # and then we order by creation time (older first).
|
220 |
| - q = q.order_by("-status", "-priority", "created_at") |
| 221 | + ordering = Case( |
| 222 | + When(status=LandingJobStatus.SUBMITTED, then=1), |
| 223 | + When(status=LandingJobStatus.IN_PROGRESS, then=2), |
| 224 | + When(status=LandingJobStatus.DEFERRED, then=3), |
| 225 | + When(status=LandingJobStatus.FAILED, then=4), |
| 226 | + When(status=LandingJobStatus.LANDED, then=5), |
| 227 | + When(status=LandingJobStatus.CANCELLED, then=6), |
| 228 | + default=0, |
| 229 | + output_field=IntegerField() |
| 230 | + ) |
| 231 | + |
| 232 | + q = q.annotate(status_order=ordering).order_by("-status_order", "-priority", "created_at") |
221 | 233 |
|
222 | 234 | return q
|
223 | 235 |
|
|
0 commit comments