Skip to content

Commit

Permalink
perf: set lower priority for background processes (frappe#21841)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush authored Jul 28, 2023
1 parent df4cc5c commit ef51dde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
26 changes: 25 additions & 1 deletion frappe/utils/background_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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.deprecations import deprecation_warning
from frappe.utils.redis_queue import RedisQueue
Expand Down Expand Up @@ -268,6 +268,8 @@ def start_worker(
if os.environ.get("CI"):
setup_loghandlers("ERROR")

set_niceness()

logging_level = "INFO"
if quiet:
logging_level = "WARNING"
Expand Down Expand Up @@ -305,6 +307,7 @@ def start_worker_pool(
if os.environ.get("CI"):
setup_loghandlers("ERROR")

set_niceness()
logging_level = "INFO"
if quiet:
logging_level = "WARNING"
Expand Down Expand Up @@ -519,3 +522,24 @@ def get_job(job_id: str) -> Job:
return Job.fetch(create_job_id(job_id), connection=get_redis_conn())
except NoSuchJobError:
return None


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 set_niceness

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

Expand All @@ -35,6 +35,7 @@ def start_scheduler() -> NoReturn:
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 ef51dde

Please sign in to comment.