Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-000] fix calculate task delay in seconds #52

Merged
merged 2 commits into from
Jun 27, 2024
Merged
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
12 changes: 6 additions & 6 deletions django_cloud_tasks/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,20 @@ def later(cls, task_kwargs: dict, eta: int | timedelta | datetime, queue: str =
)

@staticmethod
def _calculate_delay_in_seconds(eta: int | timedelta | datetime) -> int:
if isinstance(eta, int):
def _calculate_delay_in_seconds(eta: int | timedelta | datetime) -> float | int:
if isinstance(eta, int) or isinstance(eta, float):
return eta
elif isinstance(eta, timedelta):
return int(eta.total_seconds())
diegodfreire marked this conversation as resolved.
Show resolved Hide resolved
return eta.total_seconds()
elif isinstance(eta, datetime):
return int((eta - now()).total_seconds())
return (eta - now()).total_seconds()
else:
raise ValueError(
f"Unsupported schedule {eta} of type {eta.__class__.__name__}. Must be int, timedelta or datetime."
)

@staticmethod
def _validate_delay(delay_in_seconds: int):
def _validate_delay(delay_in_seconds: int | float):
max_eta_task = get_config("tasks_max_eta")
if max_eta_task is not None and delay_in_seconds > max_eta_task:
raise ValueError(f"Invalid delay time {delay_in_seconds}, maximum is {max_eta_task}")
Expand All @@ -268,7 +268,7 @@ def push(
task_kwargs: dict,
headers: dict | None = None,
queue: str | None = None,
delay_in_seconds: int | None = None,
delay_in_seconds: int | float | None = None,
):
payload = serialize(value=task_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-google-cloud-tasks"
version = "2.16.1"
version = "2.16.2"
description = "Async Tasks with HTTP endpoints"
authors = ["Joao Daher <[email protected]>"]
packages = [
Expand Down
17 changes: 17 additions & 0 deletions sample_project/sample_app/tests/tests_tasks/tests_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ def test_task_later_time(self):
)
push.assert_called_once_with(**expected_call)

@freeze_time("2020-01-01T00:00:00.902728")
def test_task_later_time_with_milliseconds(self):
task_eta = now() + timedelta(seconds=10, milliseconds=100)

with self.patch_push() as push:
task_kwargs = dict(price=30, quantity=4, discount=0.2)
tasks.CalculatePriceTask.later(eta=task_eta, task_kwargs=task_kwargs)

expected_call = dict(
delay_in_seconds=10.1,
queue_name="tasks",
url="http://localhost:8080/tasks/CalculatePriceTask",
payload=json.dumps({"price": 30, "quantity": 4, "discount": 0.2}),
headers={"X-CloudTasks-Projectname": "potato-dev"},
)
push.assert_called_once_with(**expected_call)

def test_task_later_error(self):
with self.patch_push() as push:
with self.assertRaisesRegex(expected_exception=ValueError, expected_regex="Unsupported schedule"):
Expand Down
Loading