Files
tabdeal-job-queue/backend/jobs/migrations/0001_initial.py

129 lines
5.5 KiB
Python

# Generated manually for the interview project.
import uuid
import django.db.models.deletion
from django.db import migrations, models
from django.db.models import Q
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Job",
fields=[
("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
("type", models.CharField(max_length=120)),
("payload", models.JSONField(blank=True, default=dict)),
(
"status",
models.CharField(
choices=[
("queued", "Queued"),
("running", "Running"),
("succeeded", "Succeeded"),
("failed", "Failed"),
],
db_index=True,
default="queued",
max_length=20,
),
),
("priority", models.SmallIntegerField(default=50)),
("available_at", models.DateTimeField(db_index=True)),
("attempts", models.PositiveIntegerField(default=0)),
("max_attempts", models.PositiveIntegerField(default=3)),
("idempotency_key", models.CharField(blank=True, max_length=255, null=True)),
("locked_by", models.CharField(blank=True, max_length=255, null=True)),
("locked_until", models.DateTimeField(blank=True, null=True)),
("last_error", models.TextField(blank=True, default="")),
("result", models.JSONField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("finished_at", models.DateTimeField(blank=True, null=True)),
],
options={"ordering": ("-created_at",)},
),
migrations.CreateModel(
name="JobEvent",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
(
"type",
models.CharField(
choices=[
("created", "Created"),
("claimed", "Claimed"),
("progress", "Progress"),
("lease_renewed", "Lease renewed"),
("succeeded", "Succeeded"),
("retry_scheduled", "Retry scheduled"),
("failed", "Failed"),
("timeout_requeued", "Timeout requeued"),
("manual_retry", "Manual retry"),
],
db_index=True,
max_length=40,
),
),
("attempt", models.PositiveIntegerField(default=0)),
("worker_id", models.CharField(blank=True, max_length=255, null=True)),
("message", models.TextField(blank=True, default="")),
("data", models.JSONField(blank=True, default=dict)),
("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
("job", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="events", to="jobs.job")),
],
options={"ordering": ("id",)},
),
migrations.AddConstraint(
model_name="job",
constraint=models.CheckConstraint(
name="job_status_valid_shape",
condition=(
Q(finished_at__isnull=True, locked_by__isnull=True, locked_until__isnull=True, status="queued")
| Q(finished_at__isnull=True, locked_by__isnull=False, locked_until__isnull=False, status="running")
| Q(finished_at__isnull=False, locked_by__isnull=True, locked_until__isnull=True, status__in=["succeeded", "failed"])
),
),
),
migrations.AddConstraint(
model_name="job",
constraint=models.CheckConstraint(
name="job_attempts_valid",
condition=Q(attempts__gte=0, attempts__lte=models.F("max_attempts"), max_attempts__gte=1),
),
),
migrations.AddConstraint(
model_name="job",
constraint=models.UniqueConstraint(
condition=Q(idempotency_key__isnull=False),
fields=("idempotency_key",),
name="job_idempotency_key_unique",
),
),
migrations.AddIndex(
model_name="job",
index=models.Index(
condition=Q(status="queued"),
fields=["available_at", "-priority", "created_at", "id"],
name="job_claim_idx",
),
),
migrations.AddIndex(
model_name="job",
index=models.Index(condition=Q(status="running"), fields=["locked_until"], name="job_timeout_idx"),
),
migrations.AddIndex(
model_name="jobevent",
index=models.Index(fields=["job", "id"], name="job_event_job_id_idx"),
),
migrations.AddIndex(
model_name="jobevent",
index=models.Index(fields=["id"], name="job_event_id_idx"),
),
]