Skip to content

Commit

Permalink
perf(Scheduling): add jitter to job scheduling (#25856)
Browse files Browse the repository at this point in the history
Addresses #19007

(cherry picked from commit aeec01c)

Co-authored-by: 18alantom <2.alan.tom@gmail.com>
  • Loading branch information
mergify[bot] and 18alantom committed Apr 8, 2024
1 parent 835e43f commit 2a88f87
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frappe/core/doctype/scheduled_job_type/scheduled_job_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# License: MIT. See LICENSE

import json
from datetime import datetime
from datetime import datetime, timedelta
from random import randint

import click
from croniter import CroniterBadCronError, croniter
Expand Down Expand Up @@ -97,7 +98,12 @@ def get_next_execution(self):
# immediately, even when it's meant to be daily.
# A dynamic fallback like current time might miss the scheduler interval and job will never start.
last_execution = get_datetime(self.last_execution or self.creation)
return croniter(self.cron_format, last_execution).get_next(datetime)
next_execution = croniter(self.cron_format, last_execution).get_next(datetime)

jitter = 0
if self.frequency in ("Hourly Long", "Daily Long"):
jitter = randint(1, 600)
return next_execution + timedelta(seconds=jitter)

def execute(self):
self.scheduler_log = None
Expand Down

0 comments on commit 2a88f87

Please sign in to comment.