Monkey patch issue with production (supervisor)

hi i am using monkey batch to overide some standerd methodd its working ok when start the application by “bench start” but when using supervisor its some time i think working and some no

i am using belwo code in my cutom app init


from __future__ import unicode_literals
import os
import importlib
import frappe

__version__ = '0.0.1'
app_name = 'mycustomapp'

patches_loaded = False

def load_monkey_patches():
    
    """
    Loads all modules present in monkey_patches to override some logic
    in Frappe / ERPNext. Returns if patches have already been loaded earlier.
    """
    global patches_loaded

    if patches_loaded:
        return

    print('------------------load--------------monkey')
    patches_loaded = True

    if app_name not in frappe.get_installed_apps():
        return

    for module_name in os.listdir(frappe.get_app_path(app_name, "monkey_patches")):
        if not module_name.endswith(".py") or module_name == "__init__.py":
            continue

        importlib.import_module(app_name + ".monkey_patches." + module_name[:-3])

old_get_hooks = frappe.get_hooks

def get_hooks(*args, **kwargs):
    load_monkey_patches()
    return old_get_hooks(*args, **kwargs)


frappe.get_hooks = get_hooks



old_connect = frappe.connect

def connect(*args, **kwargs):
    """
    Patches frappe.connect to load monkey patches once a connection is
    established with the database.
    """

    old_connect(*args, **kwargs)
    load_monkey_patches()


frappe.connect = connect


def console(*data):
    frappe.publish_realtime("out_to_console", data, user=frappe.session.user)

thanks in advance

this is my version erpnext_oob/__init__.py · 余则霖/ERPNext开箱即用 - Gitee.com

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import importlib

import frappe

patches_loaded = False

__version__ = '13.0.0'


def console(*data):
    frappe.publish_realtime("out_to_console", data, user=frappe.session.user)


def load_monkey_patches():
    global patches_loaded

    if (
        patches_loaded
        or not getattr(frappe, "conf", None)
        or not "erpnext_oob" in frappe.get_installed_apps()
    ):
        return

    for app in frappe.get_installed_apps():
        if app in ['frappe', 'erpnext']: continue

        folder = frappe.get_app_path(app, "monkey_patches")
        if not os.path.exists(folder): continue

        for module_name in os.listdir(folder):
            if not module_name.endswith(".py") or module_name == "__init__.py":
                continue

            importlib.import_module(f"{app}.monkey_patches.{module_name[:-3]}")

    patches_loaded = True


connect = frappe.connect


def custom_connect(*args, **kwargs):
    out = connect(*args, **kwargs)

    if frappe.conf.auto_commit_on_many_writes:
        frappe.db.auto_commit_on_many_writes = 1

    load_monkey_patches()
    return out


frappe.connect = custom_connect
1 Like

thanks szufisher
i used your code version but i still facing the same issue with the supervisor

i overided some standered methods in payslip