mirror of
https://github.com/blw1138/Zordon.git
synced 2026-06-09 13:39:24 -05:00
Refactor: ApplicationContext DI wiring (#131)
* refactor: wire all services through ApplicationContext - Created src/application_context.py as DI container with TYPE_CHECKING imports - server.py now instantiates all services in dependency order via ApplicationContext - Fixed infinite recursion bug: 48 instance methods renamed with underscore prefix to avoid shadowing by same-named @classmethod forwarders - ZeroconfServer: instantiate Zeroconf() in __init__, add _sync_class() to configure forwarder, direct _configure/_start calls during wiring - Config, EngineManager, PreviewManager: all forwarders and _sync_class() intact - RenderQueue: load_state and subscribe moved to __init__, threading.Lock retained - DistributedJobManager: subscribe_to_listener moved to __init__ * fix: greedy regex in get_render_devices swallows BlenderKit log output Changed regex from greedy [\s\S]* to non-greedy .*? with re.DOTALL so it stops at the first ] (the end of the GPU data JSON array) instead of matching through timestamped log lines like [19:36:22.109, __init__.py:2881] that contain trailing brackets. * fix: AttributeError on .enabled in update_job_count prevents options from rendering * refactor: log silent AttributeError catches, add _sync_class to remaining services, drop dead ctx slot
This commit is contained in:
@@ -11,6 +11,7 @@ from src.api.api_server import API_VERSION
|
||||
from src.api.api_server import start_api_server
|
||||
from src.api.preview_manager import PreviewManager
|
||||
from src.api.serverproxy_manager import ServerProxyManager
|
||||
from src.application_context import ApplicationContext
|
||||
from src.distributed_job_manager import DistributedJobManager
|
||||
from src.engines.engine_manager import EngineManager
|
||||
from src.render_queue import RenderQueue
|
||||
@@ -26,21 +27,46 @@ logger = logging.getLogger()
|
||||
class ZordonServer:
|
||||
|
||||
def __init__(self):
|
||||
self.ctx = ApplicationContext()
|
||||
|
||||
# setup logging
|
||||
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(module)s: %(message)s', datefmt='%d-%b-%y %H:%M:%S',
|
||||
level=Config.server_log_level.upper())
|
||||
logging.getLogger("requests").setLevel(logging.WARNING) # suppress noisy requests/urllib3 logging
|
||||
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
||||
|
||||
# Load Config YAML
|
||||
# ---- Bootstrap Config ----
|
||||
Config.setup_config_dir()
|
||||
config_path = Path(Config.config_dir()) / "config.yaml"
|
||||
Config.load_config(config_path)
|
||||
self.ctx.config = Config()
|
||||
self.ctx.config.load(config_path)
|
||||
Config._default_instance = self.ctx.config
|
||||
Config._sync_class()
|
||||
|
||||
# configure default paths
|
||||
EngineManager.engines_path = str(Path(Config.upload_folder).expanduser()/ "engines")
|
||||
os.makedirs(EngineManager.engines_path, exist_ok=True)
|
||||
PreviewManager.storage_path = Path(Config.upload_folder).expanduser() / "previews"
|
||||
# ---- Engine Manager ----
|
||||
self.ctx.engine_manager = EngineManager()
|
||||
self.ctx.engine_manager.engines_path = Path(Config.upload_folder).expanduser() / "engines"
|
||||
os.makedirs(self.ctx.engine_manager.engines_path, exist_ok=True)
|
||||
EngineManager._default_instance = self.ctx.engine_manager
|
||||
EngineManager._sync_class()
|
||||
|
||||
# ---- Preview Manager ----
|
||||
self.ctx.preview_manager = PreviewManager()
|
||||
self.ctx.preview_manager.storage_path = Path(Config.upload_folder).expanduser() / "previews"
|
||||
PreviewManager._default_instance = self.ctx.preview_manager
|
||||
PreviewManager._sync_class()
|
||||
|
||||
# ---- Render Queue ----
|
||||
self.ctx.render_queue = RenderQueue()
|
||||
self.ctx.render_queue.load_state(database_directory=Path(Config.upload_folder).expanduser())
|
||||
RenderQueue._default_instance = self.ctx.render_queue
|
||||
RenderQueue._sync_class()
|
||||
|
||||
# ---- Distributed Job Manager ----
|
||||
self.ctx.distributed_job_manager = DistributedJobManager()
|
||||
self.ctx.distributed_job_manager.subscribe_to_listener()
|
||||
DistributedJobManager._default_instance = self.ctx.distributed_job_manager
|
||||
DistributedJobManager._sync_class()
|
||||
|
||||
self.api_server = None
|
||||
self.server_hostname: str = socket.gethostname()
|
||||
@@ -73,10 +99,8 @@ class ZordonServer:
|
||||
logger.debug(f"Upload directory: {Path(Config.upload_folder).expanduser()}")
|
||||
logger.debug(f"Thumbs directory: {PreviewManager.storage_path}")
|
||||
logger.debug(f"Engines directory: {EngineManager.engines_path}")
|
||||
# Set up the RenderQueue object
|
||||
RenderQueue.load_state(database_directory=Path(Config.upload_folder).expanduser())
|
||||
|
||||
ServerProxyManager.subscribe_to_listener()
|
||||
DistributedJobManager.subscribe_to_listener()
|
||||
|
||||
# update hostname
|
||||
self.server_hostname = socket.gethostname()
|
||||
@@ -87,16 +111,21 @@ class ZordonServer:
|
||||
self.api_server.start()
|
||||
|
||||
# start zeroconf server
|
||||
ZeroconfServer.configure(f"_{APP_NAME.lower()}._tcp.local.", self.server_hostname, Config.port_number)
|
||||
ZeroconfServer.properties = {'system_cpu': current_system_cpu(),
|
||||
'system_cpu_brand': current_system_cpu_brand(),
|
||||
'system_cpu_cores': multiprocessing.cpu_count(),
|
||||
'system_os': current_system_os(),
|
||||
'system_os_version': current_system_os_version(),
|
||||
'system_memory': round(psutil.virtual_memory().total / (1024**3)), # in GB
|
||||
'gpu_info': get_gpu_info(),
|
||||
'api_version': API_VERSION}
|
||||
ZeroconfServer.start()
|
||||
ctx = self.ctx
|
||||
ctx.zeroconf_server = ZeroconfServer()
|
||||
ctx.zeroconf_server._configure(f"_{APP_NAME.lower()}._tcp.local.", self.server_hostname, Config.port_number)
|
||||
ctx.zeroconf_server.properties = {'system_cpu': current_system_cpu(),
|
||||
'system_cpu_brand': current_system_cpu_brand(),
|
||||
'system_cpu_cores': multiprocessing.cpu_count(),
|
||||
'system_os': current_system_os(),
|
||||
'system_os_version': current_system_os_version(),
|
||||
'system_memory': round(psutil.virtual_memory().total / (1024**3)),
|
||||
'gpu_info': get_gpu_info(),
|
||||
'api_version': API_VERSION}
|
||||
ZeroconfServer._default_instance = ctx.zeroconf_server
|
||||
ZeroconfServer._sync_class()
|
||||
ctx.zeroconf_server._start()
|
||||
|
||||
logger.info(f"{APP_NAME} Render Server started - Hostname: {self.server_hostname}")
|
||||
RenderQueue.start() # Start evaluating the render queue
|
||||
|
||||
@@ -112,6 +141,7 @@ class ZordonServer:
|
||||
logger.exception(f"Exception during prepare for shutdown: {e}")
|
||||
logger.info(f"{APP_NAME} Render Server has shut down")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
server = ZordonServer()
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user