Skip to content

Commit

Permalink
Ensure modified tasks get the same signal
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Sep 13, 2024
1 parent ab099d5 commit 2d5f481
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ Additionally, each `Task` has its own signals. These signals are only sent for t

- `Task.finished`: Called when the task finishes (`COMPLETE` or `FAILED`). There is no sender (`None`), however the handler is called with the finished `task_result`.

Tasks which are modified by `.using` have the same signal.

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to contribute.
5 changes: 3 additions & 2 deletions django_tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from django.db.models.enums import TextChoices
from django.dispatch import Signal
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from typing_extensions import ParamSpec, Self
Expand Down Expand Up @@ -186,7 +187,7 @@ def get_backend(self) -> "BaseTaskBackend":
def module_path(self) -> str:
return get_module_path(self.func)

@property
@cached_property
def original(self) -> Self:
return cast(Self, import_string(self.module_path))

Expand All @@ -200,7 +201,7 @@ def is_modified(self) -> bool:
@property
def finished(self) -> Signal:
"""A signal, fired when the task finished"""
return _get_task_signal(self, "finished")
return _get_task_signal(self.original, "finished")


# Bare decorator usage
Expand Down
4 changes: 4 additions & 0 deletions tests/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ def test_finished_signal(self) -> None:
self.assertIs(
test_tasks.noop_task.using().finished, test_tasks.noop_task.finished
)
self.assertIs(
test_tasks.noop_task.using(priority=10).finished,
test_tasks.noop_task.finished,
)
self.assertIs(
deepcopy(test_tasks.noop_task).finished, test_tasks.noop_task.finished
)

0 comments on commit 2d5f481

Please sign in to comment.