Skip to content

Commit

Permalink
job abstract model
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Jan 7, 2025
1 parent 1447ba6 commit 371f3ce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
29 changes: 11 additions & 18 deletions src/django_celery_boost/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Meta:
TERMINATED_STATUSES = frozenset({states.REJECTED, states.REVOKED, states.FAILURE})

version = AutoIncVersionField()
description = models.CharField(max_length=255, blank=True, null=True)

curr_async_result_id = models.CharField(
max_length=36,
Expand Down Expand Up @@ -126,6 +127,9 @@ class Meta:

_celery_app: Optional[Celery] = None

def __str__(self):
return self.description or f"Background Job #{self.pk}"

@classproperty
def celery_app(cls) -> "celery.app.base.Celery":
if not cls._celery_app:
Expand Down Expand Up @@ -290,6 +294,13 @@ def task_info(self) -> "dict[str, Any]":
}
return ret

@property
def started(self) -> str:
try:
return self.task_info["started_at"]
except KeyError:
return "="

@classproperty
def task_handler(cls: "type[CeleryTaskModel]") -> "Callable[[Any], Any]":
"""Return the task assigned to this model"""
Expand Down Expand Up @@ -430,30 +441,12 @@ class JobType(models.TextChoices):
type = models.CharField(max_length=50, choices=JobType.choices)
config = models.JSONField(default=dict, blank=True)
action = models.CharField(max_length=500, blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
sentry_id = models.CharField(max_length=255, blank=True, null=True)

class Meta:
abstract = True
permissions = (("debug_job", "Can debug background jobs"),)

def __str__(self):
return self.description or f"Background Job #{self.pk}"

@property
def queue_position(self) -> int:
try:
return super().queue_position
except Exception:
return 0

@property
def started(self) -> str:
try:
return self.task_info["started_at"]
except Exception:
return "="

def execute(self):
sid = None
try:
Expand Down
18 changes: 13 additions & 5 deletions tests/demoapp/demo/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-12-17 16:42
# Generated by Django 5.1.1 on 2025-01-07 11:15

import concurrency.fields
import django.db.models.deletion
Expand Down Expand Up @@ -32,6 +32,10 @@ class Migration(migrations.Migration):
default=0, help_text="record revision number"
),
),
(
"description",
models.CharField(blank=True, max_length=255, null=True),
),
(
"curr_async_result_id",
models.CharField(
Expand Down Expand Up @@ -143,6 +147,10 @@ class Migration(migrations.Migration):
default=0, help_text="record revision number"
),
),
(
"description",
models.CharField(blank=True, max_length=255, null=True),
),
(
"curr_async_result_id",
models.CharField(
Expand Down Expand Up @@ -271,6 +279,10 @@ class Migration(migrations.Migration):
default=0, help_text="record revision number"
),
),
(
"description",
models.CharField(blank=True, max_length=255, null=True),
),
(
"curr_async_result_id",
models.CharField(
Expand Down Expand Up @@ -350,10 +362,6 @@ class Migration(migrations.Migration):
),
("config", models.JSONField(blank=True, default=dict)),
("action", models.CharField(blank=True, max_length=500, null=True)),
(
"description",
models.CharField(blank=True, max_length=255, null=True),
),
("sentry_id", models.CharField(blank=True, max_length=255, null=True)),
(
"owner",
Expand Down
8 changes: 4 additions & 4 deletions tests/test_celery.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
from time import sleep
from unittest.mock import patch

from django.contrib.auth.models import Group
import pytest
from demo.factories import JobFactory, GroupFactory
from demo.factories import GroupFactory, JobFactory
from demo.models import Job, MultipleJob
from django.contrib.auth.models import Group
from django.core.cache import cache

from django_celery_boost.models import AsyncJobModel
from django.core.cache import cache
from tests.demoapp.demo.factories import AsyncJobModelFactory
from unittest.mock import patch

pytest_plugins = ("celery.contrib.pytest",)

Expand Down
13 changes: 12 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from uuid import uuid4

from celery.result import AsyncResult
from demo.factories import JobFactory
from demo.factories import AsyncJobModelFactory, JobFactory
from demo.models import Job

from django_celery_boost.models import CeleryTaskModel


def test_model_initialize_new(db):
job: Job = Job()
Expand Down Expand Up @@ -171,3 +173,12 @@ def test_terminate(db):
m.return_value = Job.QUEUED
with mock.patch("demo.models.Job.celery_queue_entries", return_value=[]):
assert job1.terminate() == job1.CANCELED


def test_str(db):
description = "this is me"
async_job = AsyncJobModelFactory(description=description)
assert str(async_job) == description

async_job3 = AsyncJobModelFactory()
assert str(async_job3) == f"Background Job #{async_job3.pk}"

0 comments on commit 371f3ce

Please sign in to comment.