Skip to content

Commit f1fda50

Browse files
committed
add min_ping_interval to ping
1 parent 777d7cc commit f1fda50

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

taskbadger/sdk.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import logging
23
import os
34
from typing import Any
@@ -392,9 +393,21 @@ def tag(self, tags: dict[str, str]):
392393
"""Add tags to the task."""
393394
self.update(tags=tags)
394395

395-
def ping(self):
396+
def ping(self, min_ping_interval=None):
396397
"""Update the task without changing any values. This can be used in conjunction
397-
with 'stale_timeout' to indicate that the task is still running."""
398+
with 'stale_timeout' to indicate that the task is still running.
399+
400+
Arguments:
401+
min_ping_interval: The minimum interval between pings in seconds. If set this will only
402+
update the task if the last update was more than `min_ping_interval` seconds ago.
403+
"""
404+
if min_ping_interval and self._task.updated:
405+
# tzinfo should always be set but for the sake of safety we check
406+
tz = None if self._task.updated.tzinfo is None else datetime.UTC
407+
now = datetime.datetime.now(tz)
408+
time_since = now - self._task.updated
409+
if time_since.total_seconds() < min_ping_interval:
410+
return
398411
self.update()
399412

400413
@property

tests/test_sdk.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from http import HTTPStatus
23
from unittest import mock
34

@@ -153,6 +154,26 @@ def test_update_data(settings, patched_update):
153154
_verify_update(settings, patched_update, data={"a": 1})
154155

155156

157+
def test_ping(settings, patched_update):
158+
task = Task(task_for_test())
159+
160+
updated_at = task.updated
161+
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test())
162+
task.ping(min_ping_interval=1)
163+
assert len(patched_update.call_args_list) == 0
164+
165+
task.ping()
166+
_verify_update(settings, patched_update)
167+
assert task.updated > updated_at
168+
169+
task.ping(min_ping_interval=1)
170+
assert len(patched_update.call_args_list) == 1
171+
172+
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
173+
task.ping(min_ping_interval=1)
174+
assert len(patched_update.call_args_list) == 2
175+
176+
156177
def test_increment_progress(settings, patched_update):
157178
api_task = task_for_test()
158179
task = Task(api_task)

tests/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
import datetime
22
from uuid import uuid4
33

44
from taskbadger.internal.models import Task as TaskInternal
@@ -12,8 +12,8 @@ def task_for_test(**kwargs):
1212
kwargs["url"] = None
1313
kwargs["public_url"] = None
1414
kwargs["value_percent"] = None
15-
kwargs["created"] = datetime.utcnow()
16-
kwargs["updated"] = datetime.utcnow()
15+
kwargs["created"] = datetime.datetime.now(datetime.UTC)
16+
kwargs["updated"] = datetime.datetime.now(datetime.UTC)
1717
return TaskInternal(
1818
task_id,
1919
"org",

0 commit comments

Comments
 (0)