83 lines
2.2 KiB
Python
83 lines
2.2 KiB
Python
from pathlib import Path
|
|
import threading
|
|
import atexit
|
|
|
|
WEB_DIRECTORY = str(Path(__file__).parent / "web")
|
|
|
|
from .nodes import WorkflowManagerNode
|
|
from .server import setup_web_routes
|
|
from .logger import is_backend_logging_enabled, log
|
|
|
|
NODE_CLASS_MAPPINGS = {"WorkflowManager": WorkflowManagerNode}
|
|
NODE_DISPLAY_NAME_MAPPINGS = {"WorkflowManager": "Workflow Manager"}
|
|
|
|
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
|
|
|
|
# 延迟初始化路由
|
|
_route_initialized = False
|
|
_route_lock = threading.Lock()
|
|
|
|
|
|
def _try_setup_routes():
|
|
"""尝试设置 Web 路由(延迟初始化)"""
|
|
global _route_initialized
|
|
|
|
if _route_initialized:
|
|
return True
|
|
|
|
with _route_lock:
|
|
# double-check lock pattern
|
|
if _route_initialized:
|
|
return True
|
|
|
|
try:
|
|
from server import PromptServer
|
|
|
|
if PromptServer.instance is None:
|
|
log("[yeban-WorkflowManager] PromptServer.instance \u4e3a\u7a7a\uff0c\u7a0d\u540e\u91cd\u8bd5")
|
|
return False
|
|
|
|
if not hasattr(PromptServer.instance, 'app'):
|
|
log("[yeban-WorkflowManager] PromptServer.instance.app \u4e0d\u53ef\u7528")
|
|
return False
|
|
|
|
setup_web_routes(PromptServer.instance.app)
|
|
_route_initialized = True
|
|
log("[yeban-WorkflowManager] \u8def\u7531\u6ce8\u518c\u6210\u529f")
|
|
return True
|
|
|
|
except ImportError as e:
|
|
log(f"[yeban-WorkflowManager] PromptServer \u4e0d\u53ef\u7528: {e}")
|
|
return False
|
|
except Exception as e:
|
|
log(f"[yeban-WorkflowManager] \u6ce8\u518c\u8def\u7531\u5931\u8d25: {e}")
|
|
import traceback
|
|
if is_backend_logging_enabled():
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
|
|
# 尝试立即初始化
|
|
_try_setup_routes()
|
|
|
|
|
|
# 注册一个延迟初始化的回调
|
|
def _delayed_setup():
|
|
"""延迟初始化回调"""
|
|
if not _route_initialized:
|
|
_try_setup_routes()
|
|
|
|
|
|
# 在模块加载完成后尝试一次
|
|
import threading
|
|
timer = threading.Timer(0.5, _delayed_setup)
|
|
timer.daemon = True
|
|
timer.start()
|
|
|
|
|
|
# 清理
|
|
def _cleanup():
|
|
timer.cancel()
|
|
|
|
atexit.register(_cleanup)
|