49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from django.db import models
|
|
|
|
|
|
class ScheduledTask(models.Model):
|
|
class ScheduleType(models.TextChoices):
|
|
CRON = "cron", "Cron"
|
|
INTERVAL = "interval", "Interval"
|
|
DATE = "date", "Date"
|
|
MANUAL = "manual", "Manual"
|
|
|
|
name = models.CharField(max_length=200, unique=True)
|
|
task_path = models.CharField(max_length=500)
|
|
schedule_type = models.CharField(max_length=20, choices=ScheduleType.choices)
|
|
schedule_config = models.JSONField(default=dict)
|
|
args = models.JSONField(default=list)
|
|
kwargs = models.JSONField(default=dict)
|
|
is_active = models.BooleanField(default=True)
|
|
queue = models.CharField(max_length=100, blank=True)
|
|
priority = models.IntegerField(default=0)
|
|
|
|
class Meta:
|
|
db_table = "scheduler_task"
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class TaskExecution(models.Model):
|
|
class Status(models.TextChoices):
|
|
PENDING = "pending", "Pending"
|
|
RUNNING = "running", "Running"
|
|
SUCCESS = "success", "Success"
|
|
FAILURE = "failure", "Failure"
|
|
|
|
task = models.ForeignKey(ScheduledTask, on_delete=models.CASCADE, related_name="executions")
|
|
celery_task_id = models.CharField(max_length=255, blank=True)
|
|
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
|
|
started_at = models.DateTimeField(null=True, blank=True)
|
|
completed_at = models.DateTimeField(null=True, blank=True)
|
|
result = models.TextField(blank=True)
|
|
error_traceback = models.TextField(blank=True)
|
|
worker_hostname = models.CharField(max_length=255, blank=True)
|
|
|
|
class Meta:
|
|
db_table = "scheduler_execution"
|
|
ordering = ["-started_at"]
|
|
|
|
def __str__(self):
|
|
return f"{self.task.name}#{self.pk}"
|