perf: ~2x faster scheduling (#33027)

* perf: Use cached settings

* perf: Cache parsed crons, ~2x faster scheduling
This commit is contained in:
Ankush Menat 2025-06-20 18:13:22 +05:30 committed by GitHub
parent 81ebf219c3
commit 18ecd6603b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 5 deletions

View file

@ -4,6 +4,7 @@
import hashlib
import json
from datetime import datetime, timedelta
from functools import lru_cache
import click
from croniter import CroniterBadCronError, croniter
@ -14,6 +15,8 @@ from frappe.model.document import Document
from frappe.utils import get_datetime, now_datetime
from frappe.utils.background_jobs import enqueue, is_job_enqueued
parse_cron = lru_cache(croniter) # Cache parsed cron-expressions
class ScheduledJobType(Document):
# begin: auto-generated types
@ -132,10 +135,10 @@ class ScheduledJobType(Document):
# 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)
next_execution = croniter(self.cron_format, last_execution).get_next(datetime)
next_execution = parse_cron(self.cron_format).get_next(datetime, start_time=last_execution)
if self.frequency in ("Hourly Maintenance", "Daily Maintenance"):
next_execution += timedelta(minutes=maintenance_offset)
return croniter(self.cron_format, last_execution).get_next(datetime)
return parse_cron(self.cron_format).get_next(datetime, start_time=last_execution)
def execute(self):
if frappe.job:

View file

@ -170,9 +170,7 @@ def is_scheduler_disabled(verbose=True) -> bool:
cprint(f"{frappe.local.site}: frappe.conf.disable_scheduler is SET")
return True
scheduler_disabled = not frappe.utils.cint(
frappe.db.get_single_value("System Settings", "enable_scheduler")
)
scheduler_disabled = not frappe.get_system_settings("enable_scheduler")
if scheduler_disabled:
if verbose:
cprint(f"{frappe.local.site}: SystemSettings.enable_scheduler is UNSET")