@@ -25,16 +25,21 @@ class MultipleIntervalsTrigger(BaseTrigger):
25
25
- multiple intervals, when aligned, cause only a single job invocation
26
26
- remembers which intervals have caused the invocation; the list is cleared after
27
27
`forget_affecting_after` seconds
28
+ - if start_ts is specified, it allows aligning jobs' start time; start_ts should be in the past
28
29
"""
29
30
__slots__ = 'intervals' , 'start_ts' , 'affecting_intervals' , 'forget_affecting_after'
30
31
31
- def __init__ (self , intervals , forget_affecting_after = 300 ):
32
+ def __init__ (self , intervals , forget_affecting_after = 300 , start_ts = None ):
32
33
if not intervals :
33
34
raise Exception ("At least one interval must be specified" )
34
35
# we only operate in whole seconds, and only care about unique values:
35
36
self .intervals = list (set ([int (i ) for i in intervals ]))
36
37
self .forget_affecting_after = forget_affecting_after
37
- self .start_ts = int (time .time ())
38
+ now = int (time .time ())
39
+ self .start_ts = now if start_ts is None else int (start_ts )
40
+ if self .start_ts > now :
41
+ logging .warning ("Job aligning with start_ts failed! Parameter start_ts must be in the past, never in the future." )
42
+ self .start_ts = now
38
43
self .affecting_intervals = {}
39
44
40
45
def get_next_fire_time (self , previous_fire_time , now ):
@@ -308,14 +313,22 @@ def fetch_job_configs(self, protocol):
308
313
309
314
def refresh_jobs (self ):
310
315
wanted_jobs = set ()
311
- for job_id , intervals , job_func , job_data in self .jobs ():
316
+ for next_job in self .jobs ():
317
+ # We didn't anticipate that we might need another parameter (start_ts)... since this is a library, we can't simply
318
+ # change it now until all bots change this too. But we need another parameter and it would be ugly to put it elsewhere,
319
+ # so we detect different number of returned parameters and act accordingly:
320
+ if len (next_job ) == 4 :
321
+ job_id , intervals , job_func , job_data = next_job
322
+ start_ts = None
323
+ else :
324
+ job_id , intervals , job_func , job_data , start_ts = next_job
312
325
wanted_jobs .add (job_id )
313
326
# if the existing job's configuration is the same, leave it alone, otherwise the trigger will be reset:
314
327
if self .known_jobs .get (job_id ) == job_data :
315
328
continue
316
329
self .known_jobs [job_id ] = job_data
317
330
318
- trigger = MultipleIntervalsTrigger (intervals )
331
+ trigger = MultipleIntervalsTrigger (intervals , start_ts = start_ts )
319
332
logging .info (f"Adding job: { job_id } " )
320
333
self .scheduler .add_job (job_func , id = job_id , trigger = trigger , executor = 'iaexecutor' , kwargs = job_data , replace_existing = True )
321
334
0 commit comments