Skip to content

Commit

Permalink
Remove support for timedelta for run_after
Browse files Browse the repository at this point in the history
It's not obvious when the delta is evaluated, so could be a foot-gun
  • Loading branch information
RealOrangeOne committed Nov 29, 2024
1 parent ce43889 commit cf41181
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ These attributes (besides `enqueue_on_commit`) can also be modified at run-time
modified_task = calculate_meaning_of_life.using(priority=10)
```

In addition to the above attributes, `run_after` can be passed to specify a specific time the task should run. Both a timezone-aware `datetime` or `timedelta` may be passed.
In addition to the above attributes, `run_after` can be passed to specify a specific time the task should run.

### Enqueueing tasks

Expand Down
6 changes: 1 addition & 5 deletions django_tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from asgiref.sync import async_to_sync, sync_to_async
from django.db.models.enums import TextChoices
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from typing_extensions import ParamSpec, Self

Expand Down Expand Up @@ -110,10 +109,7 @@ def using(
if queue_name is not None:
changes["queue_name"] = queue_name
if run_after is not None:
if isinstance(run_after, timedelta):
changes["run_after"] = timezone.now() + run_after
else:
changes["run_after"] = run_after
changes["run_after"] = run_after
if backend is not None:
changes["backend"] = backend

Expand Down
6 changes: 1 addition & 5 deletions tests/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dataclasses
from datetime import datetime, timedelta
from datetime import datetime

from django.test import SimpleTestCase, override_settings
from django.utils import timezone
Expand Down Expand Up @@ -97,10 +97,6 @@ def test_using_run_after(self) -> None:

self.assertIsNone(test_tasks.noop_task.run_after)
self.assertEqual(test_tasks.noop_task.using(run_after=now).run_after, now)
self.assertIsInstance(
test_tasks.noop_task.using(run_after=timedelta(hours=1)).run_after,
datetime,
)
self.assertIsNone(test_tasks.noop_task.run_after)

def test_using_unknown_backend(self) -> None:
Expand Down

0 comments on commit cf41181

Please sign in to comment.