Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ A few [Signals](https://docs.djangoproject.com/en/stable/topics/signals/) are pr
Whilst signals are available, they may not be the most maintainable approach.

- `django_tasks.signals.task_enqueued`: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued `task_result`.
- `django_tasks.signals.task_starting`: Called right before a task is started. The sender is the backend class. Also called with the enqueued `task_result`.
- `django_tasks.signals.task_finished`: Called when a task finishes (`SUCCEEDED` or `FAILED`). The sender is the backend class. Also called with the finished `task_result`.

## Contributing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django_tasks.backends.database.models import DBTaskResult
from django_tasks.backends.database.utils import exclusive_transaction
from django_tasks.exceptions import InvalidTaskBackendError
from django_tasks.signals import task_finished
from django_tasks.signals import task_finished, task_starting
from django_tasks.task import DEFAULT_QUEUE_NAME

package_logger = logging.getLogger("django_tasks")
Expand Down Expand Up @@ -130,11 +130,8 @@ def run_task(self, db_task_result: DBTaskResult) -> None:
task = db_task_result.task
task_result = db_task_result.task_result

logger.info(
"Task id=%s path=%s state=%s",
db_task_result.id,
db_task_result.task_path,
task_result.status,
task_starting.send(
sender=type(task.get_backend()), task_result=db_task_result.task_result
)
return_value = task.call(*task_result.args, **task_result.kwargs)

Expand Down
3 changes: 2 additions & 1 deletion django_tasks/backends/immediate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.utils import timezone
from typing_extensions import ParamSpec

from django_tasks.signals import task_enqueued, task_finished
from django_tasks.signals import task_enqueued, task_finished, task_starting
from django_tasks.task import ResultStatus, Task, TaskResult
from django_tasks.utils import get_exception_traceback, get_random_id, json_normalize

Expand Down Expand Up @@ -38,6 +38,7 @@ def _execute_task(self, task_result: TaskResult) -> None:
)

object.__setattr__(task_result, "started_at", timezone.now())
task_starting.send(type(self), task_result=task_result)
try:
object.__setattr__(
task_result,
Expand Down
12 changes: 11 additions & 1 deletion django_tasks/signal_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django_tasks import BaseTaskBackend, ResultStatus, TaskResult

from .signals import task_enqueued, task_finished
from .signals import task_enqueued, task_finished, task_starting

logger = logging.getLogger("django_tasks")

Expand Down Expand Up @@ -34,6 +34,16 @@ def log_task_enqueued(
task_result.backend,
)

@receiver(task_starting)
def log_task_starting(
sender: type[BaseTaskBackend], task_result: TaskResult, **kwargs: dict
) -> None:
logger.info(
"Task id=%s path=%s state=%s",
task_result.id,
task_result.task.module_path,
task_result.status,
)

@receiver(task_finished)
def log_task_finished(
Expand Down
1 change: 1 addition & 0 deletions django_tasks/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.dispatch import Signal

task_enqueued = Signal()
task_starting = Signal()
task_finished = Signal()