Skip to content

Commit 037c169

Browse files
committed
Lint issues, return task result
1 parent 9219759 commit 037c169

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

Diff for: django_tasks/backends/celery/app.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
from django_tasks.task import DEFAULT_QUEUE_NAME
66

77
# Set the default Django settings module for the 'celery' program.
8-
django_settings = os.environ.get('DJANGO_SETTINGS_MODULE')
8+
django_settings = os.environ.get("DJANGO_SETTINGS_MODULE")
99
if django_settings is None:
10-
raise ValueError('DJANGO_SETTINGS_MODULE environment variable is not set')
10+
raise ValueError("DJANGO_SETTINGS_MODULE environment variable is not set")
1111

12-
app = Celery('django_tasks')
12+
app = Celery("django_tasks")
1313

1414
# Using a string here means the worker doesn't have to serialize
1515
# the configuration object to child processes.
1616
# - namespace='CELERY' means all celery-related configuration keys
1717
# should have a `CELERY_` prefix.
18-
app.config_from_object('django.conf:settings', namespace='CELERY')
18+
app.config_from_object("django.conf:settings", namespace="CELERY")
1919

2020
app.conf.task_default_queue = DEFAULT_QUEUE_NAME
2121

Diff for: django_tasks/backends/celery/backend.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,70 @@
1+
from dataclasses import dataclass
12
from typing import TypeVar
23

34
from celery import shared_task
45
from celery.app import default_app
56
from celery.local import Proxy as CeleryTaskProxy
7+
from django.utils import timezone
68
from typing_extensions import ParamSpec
79

810
from django_tasks.backends.base import BaseTaskBackend
9-
from django_tasks.task import Task, TaskResult
11+
from django_tasks.task import ResultStatus, TaskResult
12+
from django_tasks.task import Task as BaseTask
13+
from django_tasks.utils import json_normalize
1014

1115
if not default_app:
1216
from django_tasks.backends.celery.app import app as celery_app
17+
1318
celery_app.set_default()
1419

1520

1621
T = TypeVar("T")
1722
P = ParamSpec("P")
1823

1924

20-
class CeleryTask(Task):
21-
22-
celery_task: CeleryTaskProxy
25+
@dataclass
26+
class Task(BaseTask[P, T]):
27+
celery_task: CeleryTaskProxy = None
2328
"""Celery proxy to the task in the current celery app task registry."""
2429

2530
def __post_init__(self) -> None:
26-
# TODO: allow passing extra celery specific parameters?
2731
celery_task = shared_task()(self.func)
2832
self.celery_task = celery_task
2933
return super().__post_init__()
3034

3135

3236
class CeleryBackend(BaseTaskBackend):
33-
task_class = CeleryTask
37+
task_class = Task
3438
supports_defer = True
3539

3640
def enqueue(
37-
self, task: Task[P, T], args: P.args, kwargs: P.kwargs
41+
self,
42+
task: Task[P, T], # type: ignore[override]
43+
args: P.args,
44+
kwargs: P.kwargs,
3845
) -> TaskResult[T]:
3946
self.validate_task(task)
4047

41-
apply_async_kwargs = {
48+
apply_async_kwargs: P.kwargs = {
4249
"eta": task.run_after,
4350
}
4451
if task.queue_name:
4552
apply_async_kwargs["queue"] = task.queue_name
4653
if task.priority:
4754
apply_async_kwargs["priority"] = task.priority
48-
task.celery_task.apply_async(args, kwargs=kwargs, **apply_async_kwargs)
55+
56+
# TODO: a Celery result backend is required to get additional information
57+
async_result = task.celery_task.apply_async(
58+
args, kwargs=kwargs, **apply_async_kwargs
59+
)
60+
task_result = TaskResult[T](
61+
task=task,
62+
id=async_result.id,
63+
status=ResultStatus.NEW,
64+
enqueued_at=timezone.now(),
65+
finished_at=None,
66+
args=json_normalize(args),
67+
kwargs=json_normalize(kwargs),
68+
backend=self.alias,
69+
)
70+
return task_result

0 commit comments

Comments
 (0)