Skip to content

Commit 632c68b

Browse files
author
Anze
committed
Allow jobs time to be aligned to specified parameter (start_ts)
1 parent 4dae6ad commit 632c68b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

grafoleancollector/collector.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ class MultipleIntervalsTrigger(BaseTrigger):
2525
- multiple intervals, when aligned, cause only a single job invocation
2626
- remembers which intervals have caused the invocation; the list is cleared after
2727
`forget_affecting_after` seconds
28+
- if start_ts is specified, it allows aligning jobs' start time; start_ts should be in the past
2829
"""
2930
__slots__ = 'intervals', 'start_ts', 'affecting_intervals', 'forget_affecting_after'
3031

31-
def __init__(self, intervals, forget_affecting_after=300):
32+
def __init__(self, intervals, forget_affecting_after=300, start_ts=None):
3233
if not intervals:
3334
raise Exception("At least one interval must be specified")
3435
# we only operate in whole seconds, and only care about unique values:
3536
self.intervals = list(set([int(i) for i in intervals]))
3637
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
3843
self.affecting_intervals = {}
3944

4045
def get_next_fire_time(self, previous_fire_time, now):
@@ -308,14 +313,22 @@ def fetch_job_configs(self, protocol):
308313

309314
def refresh_jobs(self):
310315
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
312325
wanted_jobs.add(job_id)
313326
# if the existing job's configuration is the same, leave it alone, otherwise the trigger will be reset:
314327
if self.known_jobs.get(job_id) == job_data:
315328
continue
316329
self.known_jobs[job_id] = job_data
317330

318-
trigger = MultipleIntervalsTrigger(intervals)
331+
trigger = MultipleIntervalsTrigger(intervals, start_ts=start_ts)
319332
logging.info(f"Adding job: {job_id}")
320333
self.scheduler.add_job(job_func, id=job_id, trigger=trigger, executor='iaexecutor', kwargs=job_data, replace_existing=True)
321334

0 commit comments

Comments
 (0)