Skip to content

Commit b3863c6

Browse files
committed
Pull worker name from RQ results
1 parent 0f87342 commit b3863c6

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

django_tasks/backends/rq.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections.abc import Iterable
2+
from importlib.metadata import version
23
from types import TracebackType
34
from typing import Any, TypeVar
45

@@ -47,10 +48,6 @@
4748

4849
class Job(BaseJob):
4950
def perform(self) -> Any:
50-
assert self.worker_name is not None
51-
self.meta.setdefault("_django_tasks_worker_ids", []).append(self.worker_name)
52-
self.save_meta() # type: ignore[no-untyped-call]
53-
5451
task_started.send(
5552
type(self.task_result.task.get_backend()), task_result=self.task_result
5653
)
@@ -113,14 +110,18 @@ def task_result(self) -> TaskResult:
113110
kwargs=self.kwargs,
114111
backend=self.meta["backend_name"],
115112
errors=[],
116-
worker_ids=self.meta.get("_django_tasks_worker_ids", []),
113+
worker_ids=[],
117114
)
118115

119116
exception_classes = self.meta.get("_django_tasks_exceptions", []).copy()
120117

118+
if self.worker_name and task_result.status == ResultStatus.RUNNING:
119+
task_result.worker_ids.append(self.worker_name)
120+
121121
rq_results = self.results()
122122

123123
for rq_result in rq_results:
124+
task_result.worker_ids.append(rq_result.worker_name)
124125
if rq_result.type == Result.Type.FAILED:
125126
task_result.errors.append(
126127
TaskError(
@@ -272,3 +273,9 @@ def check(self, **kwargs: Any) -> Iterable[messages.CheckMessage]:
272273
f"{queue_name!r} is not configured for django-rq",
273274
f"Add {queue_name!r} to RQ_QUEUES",
274275
)
276+
277+
if tuple(map(int, (version("rq").split(".")))) < (2, 5, 0):
278+
yield messages.Error(
279+
"Only rq >= 2.5.0 is supported, found " + version("rq"),
280+
"Install a newer version of rq",
281+
)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ postgres = [
6464
]
6565
rq = [
6666
"django-rq",
67+
"rq>=2.5",
6768
"rq_scheduler",
6869
]
6970

tests/tests/test_rq_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_fake_connection(
6161
},
6262
)
6363
@modify_settings(INSTALLED_APPS={"append": ["django_rq"]})
64-
class DatabaseBackendTestCase(TransactionTestCase):
64+
class RQBackendTestCase(TransactionTestCase):
6565
def setUp(self) -> None:
6666
super().setUp()
6767

0 commit comments

Comments
 (0)