Skip to content

Commit

Permalink
Use default interval to stagger new jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Aug 2, 2024
1 parent 027e1a6 commit 72902a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/337.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use default intervall from pollfile to stagger new jobs
9 changes: 5 additions & 4 deletions src/zino/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ def init_state_for_devices(devices: Sequence[PollDevice]):
async def load_and_schedule_polldevs(polldevs_conf: str):
new_devices, deleted_devices, changed_devices, defaults = load_polldevs(polldevs_conf)
deschedule_devices(deleted_devices | changed_devices)
schedule_devices(new_devices | changed_devices)
default_interval_minutes = defaults.get("interval", DEFAULT_INTERVAL_MINUTES)
schedule_devices(new_devices | changed_devices, int(default_interval_minutes))


def schedule_devices(devices: Sequence[str]):
devices = sorted((state.polldevs[name] for name in devices), key=operator.attrgetter("priority"), reverse=True)
def schedule_devices(new_devices: Sequence[str], default_interval_minutes: int = DEFAULT_INTERVAL_MINUTES):
devices = sorted((state.polldevs[name] for name in new_devices), key=operator.attrgetter("priority"), reverse=True)
if not devices:
return

Expand All @@ -94,7 +95,7 @@ def schedule_devices(devices: Sequence[str]):
scheduler = get_scheduler()

# Spread poll jobs evenly across the entire default interval
stagger_factor = (DEFAULT_INTERVAL_MINUTES * 60) / len(devices)
stagger_factor = (default_interval_minutes * 60) / len(devices)
for index, device in enumerate(devices):
first_run_time = datetime.now() + timedelta(seconds=index * stagger_factor)

Expand Down

0 comments on commit 72902a6

Please sign in to comment.