From 0271abf705ecdcafdec391be5bdeb609b795c741 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 5 Nov 2023 01:00:36 -0500 Subject: [PATCH] Serverproxy manager (#61) * Create serverproxy_manager.py * Replace use of direct RenderServerProxy with ServerProxyManager method --- src/api/api_server.py | 4 ++-- src/api/serverproxy_manager.py | 16 +++++++++++++ src/ui/main_window.py | 43 ++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 src/api/serverproxy_manager.py diff --git a/src/api/api_server.py b/src/api/api_server.py index 29b8f32..b3e4e98 100755 --- a/src/api/api_server.py +++ b/src/api/api_server.py @@ -18,8 +18,8 @@ import psutil import yaml from flask import Flask, request, render_template, send_file, after_this_request, Response, redirect, url_for, abort -from src.api.server_proxy import RenderServerProxy from src.api.add_job_helpers import handle_uploaded_project_files, process_zipped_project, create_render_jobs +from src.api.serverproxy_manager import ServerProxyManager from src.distributed_job_manager import DistributedJobManager from src.engines.core.base_worker import string_to_status, RenderStatus from src.engines.engine_manager import EngineManager @@ -211,7 +211,7 @@ def make_job_ready(job_id): for child_key in found_job.children.keys(): child_id = child_key.split('@')[0] hostname = child_key.split('@')[-1] - RenderServerProxy(hostname).request_data(f'job/{child_id}/make_ready') + ServerProxyManager.get_proxy_for_hostname(hostname).request_data(f'job/{child_id}/make_ready') found_job.status = RenderStatus.NOT_STARTED RenderQueue.save_state() return found_job.json(), 200 diff --git a/src/api/serverproxy_manager.py b/src/api/serverproxy_manager.py new file mode 100644 index 0000000..a6b45ba --- /dev/null +++ b/src/api/serverproxy_manager.py @@ -0,0 +1,16 @@ +from src.api.server_proxy import RenderServerProxy + + +class ServerProxyManager: + + server_proxys = {} + + @classmethod + def get_proxy_for_hostname(cls, hostname): + found_proxy = cls.server_proxys.get(hostname) + if not found_proxy: + new_proxy = RenderServerProxy(hostname) + new_proxy.start_background_update() + cls.server_proxys[hostname] = new_proxy + found_proxy = new_proxy + return found_proxy diff --git a/src/ui/main_window.py b/src/ui/main_window.py index 70e37a1..f26d5c2 100644 --- a/src/ui/main_window.py +++ b/src/ui/main_window.py @@ -27,6 +27,7 @@ from .widgets.menubar import MenuBar from .widgets.proportional_image_label import ProportionalImageLabel from .widgets.statusbar import StatusBar from .widgets.toolbar import ToolBar +from src.api.serverproxy_manager import ServerProxyManager logger = logging.getLogger() @@ -48,7 +49,6 @@ class MainWindow(QMainWindow): # Load the queue self.engine_browser_window = None self.server_info_group = None - self.server_proxies = {} self.current_hostname = None self.subprocess_runner = None @@ -177,8 +177,7 @@ class MainWindow(QMainWindow): def __background_update(self): while True: self.update_servers() - # self.fetch_jobs() - # todo: fix job updates - issues with threading + self.fetch_jobs() time.sleep(0.5) def closeEvent(self, event): @@ -198,33 +197,44 @@ class MainWindow(QMainWindow): @property def current_server_proxy(self): - return self.server_proxies.get(self.current_hostname, None) + return ServerProxyManager.get_proxy_for_hostname(self.current_hostname) def server_picked(self): - """Update the table and Server Info box when a server is changed""" + """Update the UI elements relevant to the server selection.""" try: + # Retrieve the new hostname selected by the user new_hostname = self.server_list_view.currentItem().text() + + # Check if the hostname has changed to avoid unnecessary updates if new_hostname != self.current_hostname: + # Update the current hostname and clear the job list self.current_hostname = new_hostname self.job_list_view.setRowCount(0) self.fetch_jobs(clear_table=True) + # Select the first row if there are jobs listed if self.job_list_view.rowCount(): self.job_list_view.selectRow(0) - # Update the Server Info box when a server is changed - self.server_info_hostname.setText(self.current_hostname or "unknown") - server_info = ZeroconfServer.get_hostname_properties(self.current_hostname) - if server_info: - self.server_info_os.setText(f"OS: {server_info['system_os']} {server_info['system_os_version']}") - self.server_info_cpu.setText(f"CPU: {server_info['system_cpu']} - {server_info.get('system_cpu_cores')} cores") - else: - self.server_info_os.setText(f"OS: Unknown") - self.server_info_cpu.setText(f"CPU: Unknown") + # Update server information display + self.update_server_info_display(new_hostname) except AttributeError: + # Handle cases where the server list view might not be properly initialized pass + def update_server_info_display(self, hostname): + """Updates the server information section of the UI.""" + self.server_info_hostname.setText(hostname or "unknown") + server_info = ZeroconfServer.get_hostname_properties(hostname) + + # Use the get method with defaults to avoid KeyError + os_info = f"OS: {server_info.get('system_os', 'Unknown')} {server_info.get('system_os_version', '')}" + cpu_info = f"CPU: {server_info.get('system_cpu', 'Unknown')} - {server_info.get('system_cpu_cores', 'Unknown')} cores" + + self.server_info_os.setText(os_info.strip()) + self.server_info_cpu.setText(cpu_info) + def fetch_jobs(self, clear_table=False): if not self.current_server_proxy: @@ -380,10 +390,7 @@ class MainWindow(QMainWindow): # Update proxys for hostname in found_servers: - if not self.server_proxies.get(hostname, None): - new_proxy = RenderServerProxy(hostname=hostname) - new_proxy.start_background_update() - self.server_proxies[hostname] = new_proxy + ServerProxyManager.get_proxy_for_hostname(hostname) # setup background updates # Add in all the missing servers current_server_list = []