Serverproxy manager (#61)

* Create serverproxy_manager.py

* Replace use of direct RenderServerProxy with ServerProxyManager method
This commit is contained in:
2023-11-05 01:00:36 -05:00
committed by GitHub
parent c3b446be8e
commit 0271abf705
3 changed files with 43 additions and 20 deletions

View File

@@ -18,8 +18,8 @@ import psutil
import yaml import yaml
from flask import Flask, request, render_template, send_file, after_this_request, Response, redirect, url_for, abort 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.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.distributed_job_manager import DistributedJobManager
from src.engines.core.base_worker import string_to_status, RenderStatus from src.engines.core.base_worker import string_to_status, RenderStatus
from src.engines.engine_manager import EngineManager 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(): for child_key in found_job.children.keys():
child_id = child_key.split('@')[0] child_id = child_key.split('@')[0]
hostname = child_key.split('@')[-1] 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 found_job.status = RenderStatus.NOT_STARTED
RenderQueue.save_state() RenderQueue.save_state()
return found_job.json(), 200 return found_job.json(), 200

View File

@@ -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

View File

@@ -27,6 +27,7 @@ from .widgets.menubar import MenuBar
from .widgets.proportional_image_label import ProportionalImageLabel from .widgets.proportional_image_label import ProportionalImageLabel
from .widgets.statusbar import StatusBar from .widgets.statusbar import StatusBar
from .widgets.toolbar import ToolBar from .widgets.toolbar import ToolBar
from src.api.serverproxy_manager import ServerProxyManager
logger = logging.getLogger() logger = logging.getLogger()
@@ -48,7 +49,6 @@ class MainWindow(QMainWindow):
# Load the queue # Load the queue
self.engine_browser_window = None self.engine_browser_window = None
self.server_info_group = None self.server_info_group = None
self.server_proxies = {}
self.current_hostname = None self.current_hostname = None
self.subprocess_runner = None self.subprocess_runner = None
@@ -177,8 +177,7 @@ class MainWindow(QMainWindow):
def __background_update(self): def __background_update(self):
while True: while True:
self.update_servers() self.update_servers()
# self.fetch_jobs() self.fetch_jobs()
# todo: fix job updates - issues with threading
time.sleep(0.5) time.sleep(0.5)
def closeEvent(self, event): def closeEvent(self, event):
@@ -198,33 +197,44 @@ class MainWindow(QMainWindow):
@property @property
def current_server_proxy(self): 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): 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: try:
# Retrieve the new hostname selected by the user
new_hostname = self.server_list_view.currentItem().text() new_hostname = self.server_list_view.currentItem().text()
# Check if the hostname has changed to avoid unnecessary updates
if new_hostname != self.current_hostname: if new_hostname != self.current_hostname:
# Update the current hostname and clear the job list
self.current_hostname = new_hostname self.current_hostname = new_hostname
self.job_list_view.setRowCount(0) self.job_list_view.setRowCount(0)
self.fetch_jobs(clear_table=True) self.fetch_jobs(clear_table=True)
# Select the first row if there are jobs listed
if self.job_list_view.rowCount(): if self.job_list_view.rowCount():
self.job_list_view.selectRow(0) self.job_list_view.selectRow(0)
# Update the Server Info box when a server is changed # Update server information display
self.server_info_hostname.setText(self.current_hostname or "unknown") self.update_server_info_display(new_hostname)
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")
except AttributeError: except AttributeError:
# Handle cases where the server list view might not be properly initialized
pass 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): def fetch_jobs(self, clear_table=False):
if not self.current_server_proxy: if not self.current_server_proxy:
@@ -380,10 +390,7 @@ class MainWindow(QMainWindow):
# Update proxys # Update proxys
for hostname in found_servers: for hostname in found_servers:
if not self.server_proxies.get(hostname, None): ServerProxyManager.get_proxy_for_hostname(hostname) # setup background updates
new_proxy = RenderServerProxy(hostname=hostname)
new_proxy.start_background_update()
self.server_proxies[hostname] = new_proxy
# Add in all the missing servers # Add in all the missing servers
current_server_list = [] current_server_list = []