def make_demo(context, site, domain='Manufacturing', days=100, resume=False, reinstall=False): "Reinstall site and setup demo" from frappe.commands.site import _reinstall from frappe.installer import install_app site = get_site(context) if resume: with frappe.init_site(site): frappe.connect() from erpnext.demo import demo demo.simulate(days=days) else: if reinstall: _reinstall(site, yes=True) with frappe.init_site(site=site): frappe.connect() if not 'erpnext' in frappe.get_installed_apps(): install_app('erpnext') # import needs site from erpnext.demo import demo demo.make(domain, days)
def _set_limits(context, site, limits): import datetime if not limits: return if not site: site = get_site(context) with frappe.init_site(site): new_limits = {} for limit, value in limits: if limit not in ('emails', 'space', 'users', 'email_group', 'expiry', 'support_email', 'support_chat', 'upgrade_url'): frappe.throw('Invalid limit {0}'.format(limit)) if limit == 'expiry': try: datetime.datetime.strptime(value, '%Y-%m-%d') except ValueError: raise ValueError( "Incorrect data format, should be YYYY-MM-DD") elif limit == 'space': value = float(value) elif limit in ('users', 'emails', 'email_group'): value = int(value) new_limits[limit] = value update_limits(new_limits)
def _set_limits(context, site, limits): import datetime if not limits: return if not site: site = get_site(context) with frappe.init_site(site): frappe.connect() new_limits = {} for limit, value in limits: if limit not in ('daily_emails', 'emails', 'space', 'users', 'email_group', 'expiry', 'support_email', 'support_chat', 'upgrade_url'): frappe.throw(_('Invalid limit {0}').format(limit)) if limit=='expiry' and value: try: datetime.datetime.strptime(value, '%Y-%m-%d') except ValueError: raise ValueError("Incorrect data format, should be YYYY-MM-DD") elif limit=='space': value = float(value) elif limit in ('users', 'emails', 'email_group', 'daily_emails'): value = int(value) new_limits[limit] = value update_limits(new_limits)
def _set_limits(context, site, limits): import datetime if not limits: return if not site: site = get_site(context) with frappe.init_site(site): frappe.connect() new_limits = {} for limit, value in limits: if limit not in ('daily_emails', 'emails', 'space', 'users', 'email_group', 'currency', 'expiry', 'support_email', 'support_chat', 'upgrade_url', 'subscription_id', 'subscription_type', 'current_plan', 'subscription_base_price', 'upgrade_plan', 'upgrade_base_price', 'cancellation_url'): frappe.throw(_('Invalid limit {0}').format(limit)) if limit=='expiry' and value: try: datetime.datetime.strptime(value, '%Y-%m-%d') except ValueError: raise ValueError("Incorrect data format, should be YYYY-MM-DD") elif limit in ('space', 'subscription_base_price', 'upgrade_base_price'): value = float(value) elif limit in ('users', 'emails', 'email_group', 'daily_emails'): value = int(value) new_limits[limit] = value update_limits(new_limits)
def disable_user(context, email): site = get_site(context) with frappe.init_site(site): frappe.connect() user = frappe.get_doc("User", email) user.enabled = 0 user.save(ignore_permissions=True) frappe.db.commit()
def add_to_email_queue(context, email): "Add an email to the Email Queue" site = get_site(context) with frappe.init_site(site): frappe.connect() kwargs = json.loads(email) kwargs['delayed'] = True frappe.sendmail(**kwargs) frappe.db.commit()
def schedule_all_tasks(): with frappe.init_site(): sites = get_sites() for site in sites: get_all_tasks_site(site) while True: time.sleep(10)
def start_worker(queue=None): '''Wrapper to start rq worker. Connects to redis and monitors these queues.''' with frappe.init_site(): # empty init is required to get redis_queue from common_site_config.json redis_connection = get_redis_conn() with Connection(redis_connection): queues = get_queue_list(queue) Worker(queues, name=get_worker_name(queue)).work()
def show_pending_jobs(context, site=None): "Get diagnostic info about background jobs" from frappe.utils.doctor import pending_jobs as _pending_jobs if not site: site = get_site(context) with frappe.init_site(site): pending_jobs = _pending_jobs(site=site) return pending_jobs
def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued''' with frappe.init_site(): jobs_per_site = get_jobs() sites = get_sites() for site in sites: try: enqueue_events_for_site(site=site, queued_jobs=jobs_per_site[site]) except: # it should try to enqueue other sites print(frappe.get_traceback())
def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued''' with frappe.init_site(): jobs_per_site = get_jobs() sites = get_sites() for site in sites: try: enqueue_events_for_site(site=site, queued_jobs=jobs_per_site[site]) except: # it should try to enqueue other sites print frappe.get_traceback()
def add_to_email_queue(context, email_path): "Add an email to the Email Queue" site = get_site(context) if os.path.isdir(email_path): with frappe.init_site(site): frappe.connect() for email in os.listdir(email_path): with open(os.path.join(email_path, email)) as email_data: kwargs = json.load(email_data) kwargs['delayed'] = True frappe.sendmail(**kwargs) frappe.db.commit()
def start_worker(queue=None, quiet=False): '''Wrapper to start rq worker. Connects to redis and monitors these queues.''' with frappe.init_site(): # empty init is required to get redis_queue from common_site_config.json redis_connection = get_redis_conn() if os.environ.get('CI'): setup_loghandlers('ERROR') with Connection(redis_connection): queues = get_queue_list(queue) logging_level = "INFO" Worker(queues, name=get_worker_name(queue)).work(logging_level=logging_level)
def start_worker(queue=None, quiet = False): '''Wrapper to start rq worker. Connects to redis and monitors these queues.''' with frappe.init_site(): # empty init is required to get redis_queue from common_site_config.json redis_connection = get_redis_conn() if os.environ.get('CI'): setup_loghandlers('ERROR') with Connection(redis_connection): queues = get_queue_list(queue) logging_level = "INFO" if quiet: logging_level = "WARNING" Worker(queues, name=get_worker_name(queue)).work(logging_level = logging_level)
def test_schedule(): jobs_per_site={} if os.path.exists(os.path.join('.', '.restarting')): # Don't add task to queue if webserver is in restart mode return with frappe.init_site(): jobs_per_site = get_jobs() sites = get_sites() #print(jobs_per_site) for site in sites: conf = frappe.get_site_config("/home/frappe/frappe-bench/sites","/home/frappe/frappe-bench/sites/"+site) if conf.db_name!="db_commeta": continue print(site) print(conf.db_name)
def clear_limits(context, site, limits): """Clears given limit from the site config, and removes limit from site config if its empty""" from frappe.limits import clear_limit as _clear_limit if not limits: return if not site: site = get_site(context) with frappe.init_site(site): _clear_limit(limits) # Remove limits from the site_config, if it's empty limits = get_limits() if not limits: update_site_config('limits', 'None', validate=False)
def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued''' if os.path.exists(os.path.join('.', '.restarting')): # Don't add task to queue if webserver is in restart mode return with frappe.init_site(): sites = get_sites() for site in sites: try: enqueue_events_for_site(site=site) except: # it should try to enqueue other sites print(frappe.get_traceback())
def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued''' if os.path.exists(os.path.join('.', '.restarting')): # Don't add task to queue if webserver is in restart mode return with frappe.init_site(): sites = get_sites() for site in sites: try: enqueue_events_for_site(site=site) except Exception as e: print(e.__class__, 'Failed to enqueue events for site: {}'.format(site))
def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued''' if os.path.exists(os.path.join('.', '.restarting')): # Don't add task to queue if webserver is in restart mode return with frappe.init_site(): jobs_per_site = get_jobs() sites = get_sites() for site in sites: try: enqueue_events_for_site(site=site, queued_jobs=jobs_per_site[site]) except: # it should try to enqueue other sites print(frappe.get_traceback())
def set_last_active_for_user(context, user=None): "Set users last active date to current datetime" from frappe.core.doctype.user.user import get_system_users from frappe.utils.user import set_last_active_to_now site = get_site(context) with frappe.init_site(site): frappe.connect() if not user: user = get_system_users(limit=1) if len(user) > 0: user = user[0] else: return set_last_active_to_now(user) frappe.db.commit()
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. """ 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')