@@ -327,18 +327,33 @@ def update_status(self, status: StatusEnum):
327327 """Update the task status"""
328328 self .update (status = status )
329329
330- def increment_progress (self , amount : int ):
331- """Increment the task progress.
330+ def increment_progress (self , amount : int , min_value_interval : int = None , min_time_interval : int = None ):
331+ """Increment the task progress by adding the specified amount to the current value .
332332 If the task value is not set it will be set to `amount`.
333+
334+ Arguments:
335+ amount: The amount to increment the task value by.
336+ min_value_interval: The minimum change in value required to trigger an update.
337+ min_time_interval: The minimum interval between updates in seconds.
333338 """
334339 value = self ._task .value
335340 value_norm = value if value is not UNSET and value is not None else 0
336341 new_amount = value_norm + amount
337- self .update ( value = new_amount )
342+ self .update_progress ( new_amount , min_value_interval , min_time_interval )
338343
339- def update_progress (self , value : int ):
340- """Update task progress."""
341- self .update (value = value )
344+ def update_progress (self , value : int , min_value_interval : int = None , min_time_interval : int = None ):
345+ """Update task progress.
346+
347+ Arguments:
348+ value: The new value to set.
349+ min_value_interval: The minimum change in value required to trigger an update.
350+ min_time_interval: The minimum interval between updates in seconds.
351+ """
352+ skip_check = not (min_value_interval or min_time_interval )
353+ time_check = min_time_interval and self ._check_update_time_interval (min_time_interval )
354+ value_check = min_value_interval and self ._check_update_value_interval (value , min_value_interval )
355+ if skip_check or time_check or value_check :
356+ self .update (value = value )
342357
343358 def set_value_max (self , value_max : int ):
344359 """Set the `value_max`."""
@@ -393,22 +408,16 @@ def tag(self, tags: dict[str, str]):
393408 """Add tags to the task."""
394409 self .update (tags = tags )
395410
396- def ping (self , min_ping_interval = None ):
411+ def ping (self , min_time_interval = None ):
397412 """Update the task without changing any values. This can be used in conjunction
398413 with 'stale_timeout' to indicate that the task is still running.
399414
400415 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.
416+ min_time_interval : The minimum interval between pings in seconds. If set this will only
417+ update the task if the last update was more than `min_time_interval ` seconds ago.
403418 """
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
411- self .update ()
419+ if self ._check_update_time_interval (min_time_interval ):
420+ self .update ()
412421
413422 @property
414423 def tags (self ):
@@ -423,6 +432,20 @@ def safe_update(self, **kwargs):
423432 except Exception as e :
424433 log .warning ("Error updating task '%s': %s" , self ._task .id , e )
425434
435+ def _check_update_time_interval (self , min_time_interval : int = None ):
436+ if min_time_interval and self ._task .updated :
437+ # tzinfo should always be set but for the sake of safety we check
438+ tz = None if self ._task .updated .tzinfo is None else datetime .UTC
439+ now = datetime .datetime .now (tz )
440+ time_since = now - self ._task .updated
441+ return time_since .total_seconds () >= min_time_interval
442+ return True
443+
444+ def _check_update_value_interval (self , new_value , min_value_interval : int = None ):
445+ if min_value_interval and self ._task .value :
446+ return new_value - self ._task .value >= min_value_interval
447+ return True
448+
426449
427450def _none_to_unset (value ):
428451 return UNSET if value is None else value
0 commit comments