Skip to content

Commit e69089b

Browse files
committed
return bool from rate limited functions
1 parent e35b691 commit e69089b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

taskbadger/sdk.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def increment_progress(self, amount: int):
334334
DeprecationWarning,
335335
stacklevel=2,
336336
)
337-
return self.increment_value(amount)
337+
self.increment_value(amount)
338338

339339
def increment_value(self, amount: int):
340340
"""Increment the task progress by adding the specified amount to the current value.
@@ -353,14 +353,17 @@ def update_progress(self, value: int, value_step: int = None, rate_limit: int =
353353
)
354354
self.update_value(value, value_step, rate_limit)
355355

356-
def update_value(self, value: int, value_step: int = None, rate_limit: int = None):
356+
def update_value(self, value: int, value_step: int = None, rate_limit: int = None) -> bool:
357357
"""Update task progress.
358358
359359
Arguments:
360360
value: The new value to set.
361361
value_step: The minimum change in value required to trigger an update.
362362
rate_limit: The minimum interval between updates in seconds.
363363
364+
Returns:
365+
bool: True if the task was updated, False otherwise
366+
364367
If either `value_step` or `rate_limit` is set, the task will only be updated if the
365368
specified conditions are met. If both are set, the task will be updated if either
366369
condition is met.
@@ -370,6 +373,8 @@ def update_value(self, value: int, value_step: int = None, rate_limit: int = Non
370373
value_check = value_step and self._check_update_value_interval(value, value_step)
371374
if skip_check or time_check or value_check:
372375
self.update(value=value)
376+
return True
377+
return False
373378

374379
def set_value_max(self, value_max: int):
375380
"""Set the `value_max`."""
@@ -424,16 +429,21 @@ def tag(self, tags: dict[str, str]):
424429
"""Add tags to the task."""
425430
self.update(tags=tags)
426431

427-
def ping(self, rate_limit=None):
432+
def ping(self, rate_limit=None) -> bool:
428433
"""Update the task without changing any values. This can be used in conjunction
429434
with 'stale_timeout' to indicate that the task is still running.
430435
431436
Arguments:
432437
rate_limit: The minimum interval between pings in seconds. If set this will only
433438
update the task if the last update was more than `rate_limit` seconds ago.
439+
440+
Returns:
441+
bool: True if the task was updated, False otherwise
434442
"""
435443
if self._check_update_time_interval(rate_limit):
436444
self.update()
445+
return True
446+
return False
437447

438448
@property
439449
def tags(self):

tests/test_sdk.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,18 @@ def test_ping(settings, patched_update):
172172

173173
updated_at = task.updated
174174
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test())
175-
task.ping(rate_limit=1)
175+
assert not task.ping(rate_limit=1)
176176
assert len(patched_update.call_args_list) == 0
177177

178-
task.ping()
178+
assert task.ping()
179179
_verify_update(settings, patched_update)
180180
assert task.updated > updated_at
181181

182-
task.ping(rate_limit=1)
182+
assert not task.ping(rate_limit=1)
183183
assert len(patched_update.call_args_list) == 1
184184

185185
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
186-
task.ping(rate_limit=1)
186+
assert task.ping(rate_limit=1)
187187
assert len(patched_update.call_args_list) == 2
188188

189189

@@ -192,18 +192,18 @@ def test_update_progress_rate_limit(settings, patched_update):
192192

193193
updated_at = task.updated
194194
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test())
195-
task.update_value(2, rate_limit=1)
195+
assert not task.update_value(2, rate_limit=1)
196196
assert len(patched_update.call_args_list) == 0
197197

198-
task.update_value(2)
198+
assert task.update_value(2)
199199
_verify_update(settings, patched_update, value=2)
200200
assert task.updated > updated_at
201201

202-
task.update_value(3, rate_limit=1)
202+
assert not task.update_value(3, rate_limit=1)
203203
assert len(patched_update.call_args_list) == 1
204204

205205
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
206-
task.update_value(3, rate_limit=1)
206+
assert task.update_value(3, rate_limit=1)
207207
assert len(patched_update.call_args_list) == 2
208208

209209

0 commit comments

Comments
 (0)