|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from django.db import models |
| 4 | +from django.utils import timezone |
| 5 | + |
| 6 | +from ..json import JSONEncoder |
| 7 | +# from common.json import JSONEncoder |
| 8 | + |
| 9 | +class TaskHistoryQuerySet(models.QuerySet): |
| 10 | + |
| 11 | + def failed(self, within=None): |
| 12 | + """ |
| 13 | + :param within: A timedelta object |
| 14 | + :return: A queryset of CompletedTasks that failed within the given timeframe (e.g. less than 1h ago) |
| 15 | + """ |
| 16 | + qs = self.filter( |
| 17 | + failed_at__isnull=False, |
| 18 | + ) |
| 19 | + if within: |
| 20 | + if not isinstance(within, timedelta): |
| 21 | + within = timedelta(seconds=within) |
| 22 | + time_limit = timezone.now() - within |
| 23 | + qs = qs.filter(failed_at__gt=time_limit) |
| 24 | + return qs |
| 25 | + |
| 26 | + def succeeded(self, within=None): |
| 27 | + """ |
| 28 | + :param within: A timedelta object |
| 29 | + :return: A queryset of CompletedTasks that completed successfully within the given timeframe |
| 30 | + (e.g. less than 1h ago) |
| 31 | + """ |
| 32 | + qs = self.filter( |
| 33 | + failed_at__isnull=True, |
| 34 | + ) |
| 35 | + if within: |
| 36 | + if not isinstance(within, timedelta): |
| 37 | + within = timedelta(seconds=within) |
| 38 | + time_limit = timezone.now() - within |
| 39 | + qs = qs.filter(end_at__gt=time_limit) |
| 40 | + return qs |
| 41 | + |
| 42 | + |
| 43 | +class TaskHistory(models.Model): |
| 44 | + # the "name" of the task/function to be run |
| 45 | + name = models.CharField(max_length=190, db_index=True) |
| 46 | + task_id = models.CharField(max_length=40, db_index=True) |
| 47 | + # the json encoded parameters to pass to the task |
| 48 | + task_params = models.JSONField(default=dict, encoder=JSONEncoder) |
| 49 | + |
| 50 | + verbose_name = models.CharField(max_length=255, null=True, blank=True) |
| 51 | + |
| 52 | + start_at = models.DateTimeField(db_index=True, null=True, blank=True) |
| 53 | + # when the task was completed |
| 54 | + end_at = models.DateTimeField(default=timezone.now, db_index=True) |
| 55 | + |
| 56 | + # what priority the task had |
| 57 | + priority = models.IntegerField(default=0, db_index=True) |
| 58 | + # the "name" of the queue this is to be run on |
| 59 | + queue = models.CharField(max_length=190, db_index=True, null=True, blank=True) |
| 60 | + |
| 61 | + # how many times the task has been tried |
| 62 | + attempts = models.IntegerField(default=0, db_index=True) |
| 63 | + # when the task last failed |
| 64 | + failed_at = models.DateTimeField(db_index=True, null=True, blank=True) |
| 65 | + # details of the error that occurred |
| 66 | + last_error = models.TextField(blank=True) |
| 67 | + |
| 68 | + repeat = models.BigIntegerField(default=0) |
| 69 | + repeat_until = models.DateTimeField(null=True, blank=True) |
| 70 | + |
| 71 | + objects = TaskHistoryQuerySet.as_manager() |
| 72 | + |
| 73 | + def save(self, *args, **kwargs): |
| 74 | + self.queue = self.queue or None |
| 75 | + self.verbose_name = self.verbose_name or None |
| 76 | + return super().save(*args, **kwargs) |
| 77 | + |
| 78 | + def has_error(self): |
| 79 | + """ |
| 80 | + Check if the last_error field is empty. |
| 81 | + """ |
| 82 | + return bool(self.last_error) |
| 83 | + |
| 84 | + def __str__(self): |
| 85 | + return u'{} - {}'.format( |
| 86 | + self.verbose_name or self.task_name, |
| 87 | + self.end_at, |
| 88 | + ) |
| 89 | + |
| 90 | + |
0 commit comments