seitime-frappe/frappe/commands/redis_utils.py
Ankush Menat 8a37d6d278
perf: reduce memory usage of background processes (#21467)
* perf: defer translation.py imports

This indirectly imports babel which isn't really required most of the
time.

* perf: defer gzip import

* perf: move validate_and_sanitize_search_inputs

This causes all sorts of indirect imports and increases memory usage

* perf: defer requests module imports

* perf: defer system settings import

* perf: defer LOG_DOCTYPES import

Causes many indirect imports

* perf: defer update_site_config

* perf: defer notifications import

* perf: remove unused import

* perf: defer safe exec import

* test: memory usage overhead
2023-06-23 12:51:45 +05:30

74 lines
2 KiB
Python

import os
import click
import frappe
from frappe.utils.redis_queue import RedisQueue
@click.command("create-rq-users")
@click.option(
"--set-admin-password",
is_flag=True,
default=False,
help="Set new Redis admin(default user) password",
)
@click.option(
"--use-rq-auth", is_flag=True, default=False, help="Enable Redis authentication for sites"
)
def create_rq_users(set_admin_password=False, use_rq_auth=False):
"""Create Redis Queue users and add to acl and app configs.
acl config file will be used by redis server while starting the server
and app config is used by app while connecting to redis server.
"""
from frappe.installer import update_site_config
acl_file_path = os.path.abspath("../config/redis_queue.acl")
with frappe.init_site():
acl_list, user_credentials = RedisQueue.gen_acl_list(set_admin_password=set_admin_password)
with open(acl_file_path, "w") as f:
f.writelines([acl + "\n" for acl in acl_list])
sites_path = os.getcwd()
common_site_config_path = os.path.join(sites_path, "common_site_config.json")
update_site_config(
"rq_username",
user_credentials["bench"][0],
validate=False,
site_config_path=common_site_config_path,
)
update_site_config(
"rq_password",
user_credentials["bench"][1],
validate=False,
site_config_path=common_site_config_path,
)
update_site_config(
"use_rq_auth", use_rq_auth, validate=False, site_config_path=common_site_config_path
)
click.secho(
"* ACL and site configs are updated with new user credentials. "
"Please restart Redis Queue server to enable namespaces.",
fg="green",
)
if set_admin_password:
env_key = "RQ_ADMIN_PASWORD"
click.secho(
"* Redis admin password is successfully set up. "
"Include below line in .bashrc file for system to use",
fg="green",
)
click.secho(f"`export {env_key}={user_credentials['default'][1]}`")
click.secho(
"NOTE: Please save the admin password as you "
"can not access redis server without the password",
fg="yellow",
)
commands = [create_rq_users]