Skip to content

Commit

Permalink
perf: set lower priority for background processes (#21841) (#22027)
Browse files Browse the repository at this point in the history
(cherry picked from commit ef51dde)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Aug 17, 2023
1 parent 59f33e0 commit f39bc79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion frappe/utils/background_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import frappe
import frappe.monitor
from frappe import _
from frappe.utils import cstr, get_bench_id
from frappe.utils import cint, cstr, get_bench_id
from frappe.utils.commands import log
from frappe.utils.redis_queue import RedisQueue

Expand Down Expand Up @@ -254,6 +254,7 @@ def start_worker(
if os.environ.get("CI"):
setup_loghandlers("ERROR")

set_niceness()
WorkerKlass = DEQUEUE_STRATEGIES.get(strategy, Worker)

with Connection(redis_connection):
Expand Down Expand Up @@ -448,3 +449,24 @@ def is_job_enqueued(job_id: str) -> str:
return False

return job.get_status() in ("queued", "started")


BACKGROUND_PROCESS_NICENESS = 10


def set_niceness():
"""Background processes should have slightly lower priority than web processes.
Calling this function increments the niceness of process by configured value or default.
Note: This function should be called only once in process' lifetime.
"""

conf = frappe.get_conf()
nice_increment = BACKGROUND_PROCESS_NICENESS

configured_niceness = conf.get("background_process_niceness")

if configured_niceness is not None:
nice_increment = cint(configured_niceness)

os.nice(nice_increment)
3 changes: 2 additions & 1 deletion frappe/utils/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# imports - module imports
import frappe
from frappe.utils import cint, get_datetime, get_sites, now_datetime
from frappe.utils.background_jobs import get_jobs
from frappe.utils.background_jobs import get_jobs, set_niceness

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"

Expand All @@ -35,6 +35,7 @@ def start_scheduler():
Specify scheduler_interval in seconds in common_site_config.json"""

tick = cint(frappe.get_conf().scheduler_tick_interval) or 60
set_niceness()

while True:
time.sleep(tick)
Expand Down

0 comments on commit f39bc79

Please sign in to comment.