# Generated by Django 6.0 on 2026-01-18 22:47

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="TaskWorkerLease",
            fields=[
                (
                    "worker_id",
                    models.CharField(
                        help_text="Unique identifier for the worker process",
                        max_length=255,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "hostname",
                    models.CharField(
                        help_text="Machine hostname where the worker is running", max_length=255
                    ),
                ),
                ("pid", models.PositiveIntegerField(help_text="Process ID of the worker")),
                (
                    "queue_name",
                    models.CharField(
                        db_index=True,
                        default="default",
                        help_text="Queue(s) this worker is processing (informational only)",
                        max_length=100,
                    ),
                ),
                (
                    "started_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now, help_text="When the worker started"
                    ),
                ),
                (
                    "last_heartbeat_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        help_text="Last heartbeat from the worker",
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        db_index=True,
                        default=True,
                        help_text="Whether the worker is currently active (False = shutdown or expired)",
                    ),
                ),
                (
                    "stopped_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="When the worker was stopped or marked inactive",
                        null=True,
                    ),
                ),
            ],
            options={
                "verbose_name": "Task Worker Lease",
                "verbose_name_plural": "Task Worker Leases",
            },
        ),
        migrations.CreateModel(
            name="RayTaskExecution",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "task_id",
                    models.CharField(
                        db_index=True, help_text="ID from Django Tasks", max_length=255
                    ),
                ),
                (
                    "callable_path",
                    models.CharField(help_text="Dotted path to the task callable", max_length=500),
                ),
                (
                    "queue_name",
                    models.CharField(
                        db_index=True,
                        default="default",
                        help_text="Queue this task belongs to",
                        max_length=100,
                    ),
                ),
                (
                    "state",
                    models.CharField(
                        choices=[
                            ("QUEUED", "Queued"),
                            ("RUNNING", "Running"),
                            ("SUCCEEDED", "Succeeded"),
                            ("FAILED", "Failed"),
                            ("CANCELLED", "Cancelled"),
                            ("CANCELLING", "Cancelling"),
                            ("LOST", "Lost"),
                        ],
                        db_index=True,
                        default="QUEUED",
                        max_length=20,
                    ),
                ),
                (
                    "attempt_number",
                    models.PositiveIntegerField(default=1, help_text="Current attempt number"),
                ),
                (
                    "ray_job_id",
                    models.CharField(
                        blank=True, db_index=True, help_text="Ray Job ID", max_length=255, null=True
                    ),
                ),
                (
                    "ray_address",
                    models.CharField(
                        blank=True, help_text="Ray cluster address used", max_length=255, null=True
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(db_index=True, default=django.utils.timezone.now),
                ),
                ("started_at", models.DateTimeField(blank=True, null=True)),
                ("finished_at", models.DateTimeField(blank=True, null=True)),
                ("last_heartbeat_at", models.DateTimeField(blank=True, null=True)),
                (
                    "run_after",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        help_text="Don't run before this time (for delayed/retry)",
                        null=True,
                    ),
                ),
                (
                    "timeout_seconds",
                    models.PositiveIntegerField(
                        blank=True,
                        help_text="Maximum execution time in seconds (None = no timeout)",
                        null=True,
                    ),
                ),
                (
                    "claimed_by_worker",
                    models.CharField(
                        blank=True,
                        help_text="Worker ID that claimed this task",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "args_json",
                    models.TextField(
                        default="[]", help_text="JSON-serialized positional arguments"
                    ),
                ),
                (
                    "kwargs_json",
                    models.TextField(default="{}", help_text="JSON-serialized keyword arguments"),
                ),
                (
                    "result_data",
                    models.TextField(
                        blank=True,
                        help_text="JSON-serialized result (for small results)",
                        null=True,
                    ),
                ),
                (
                    "result_reference",
                    models.CharField(
                        blank=True,
                        help_text="Reference to external result storage",
                        max_length=500,
                        null=True,
                    ),
                ),
                ("error_message", models.TextField(blank=True, null=True)),
                ("error_traceback", models.TextField(blank=True, null=True)),
            ],
            options={
                "verbose_name": "Ray Task Execution",
                "verbose_name_plural": "Ray Task Executions",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["state", "queue_name", "run_after"], name="ray_task_claimable_idx"
                    ),
                    models.Index(
                        fields=["state", "last_heartbeat_at"], name="ray_task_heartbeat_idx"
                    ),
                ],
            },
        ),
    ]
