mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
Refactor: Move all initialization logic out of api_server and into init (#91)
* Zeroconf logging improvements * Ignore RuntimeErrors in background threads - Prevents issues during shutdown * Migrate start up code from api_server.py to init.py * Add error handlers to the API server to handle detached instances * Integrate RenderQueue eval loop into RenderQueue object * Silently catch RuntimeErrors on evaluate_queue * Stop background queue updates in prepare_for_shutdown
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from PyQt6.QtGui import QFont
|
||||
@@ -16,7 +15,10 @@ class QSignalHandler(logging.Handler, QObject):
|
||||
|
||||
def emit(self, record):
|
||||
msg = self.format(record)
|
||||
self.new_record.emit(msg) # Emit signal
|
||||
try:
|
||||
self.new_record.emit(msg) # Emit signal
|
||||
except RuntimeError:
|
||||
pass
|
||||
|
||||
|
||||
class ConsoleWindow(QMainWindow):
|
||||
|
||||
@@ -183,8 +183,13 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def __background_update(self):
|
||||
while True:
|
||||
self.update_servers()
|
||||
self.fetch_jobs()
|
||||
try:
|
||||
self.update_servers()
|
||||
self.fetch_jobs()
|
||||
except RuntimeError:
|
||||
pass
|
||||
except Exception as e:
|
||||
logger.error(f"Uncaught exception in background update: {e}")
|
||||
time.sleep(0.5)
|
||||
|
||||
def closeEvent(self, event):
|
||||
@@ -372,30 +377,36 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def load_image_path(self, image_path):
|
||||
# Load and set the image using QPixmap
|
||||
pixmap = QPixmap(image_path)
|
||||
if not pixmap:
|
||||
logger.error("Error loading image")
|
||||
return
|
||||
self.image_label.setPixmap(pixmap)
|
||||
try:
|
||||
pixmap = QPixmap(image_path)
|
||||
if not pixmap:
|
||||
logger.error("Error loading image")
|
||||
return
|
||||
self.image_label.setPixmap(pixmap)
|
||||
except Exception as e:
|
||||
logger.error(f"Error loading image path: {e}")
|
||||
|
||||
def load_image_data(self, pillow_image):
|
||||
# Convert the Pillow Image to a QByteArray (byte buffer)
|
||||
byte_array = QByteArray()
|
||||
buffer = QBuffer(byte_array)
|
||||
buffer.open(QIODevice.OpenModeFlag.WriteOnly)
|
||||
pillow_image.save(buffer, "PNG")
|
||||
buffer.close()
|
||||
try:
|
||||
# Convert the Pillow Image to a QByteArray (byte buffer)
|
||||
byte_array = QByteArray()
|
||||
buffer = QBuffer(byte_array)
|
||||
buffer.open(QIODevice.OpenModeFlag.WriteOnly)
|
||||
pillow_image.save(buffer, "PNG")
|
||||
buffer.close()
|
||||
|
||||
# Create a QImage from the QByteArray
|
||||
image = QImage.fromData(byte_array)
|
||||
# Create a QImage from the QByteArray
|
||||
image = QImage.fromData(byte_array)
|
||||
|
||||
# Create a QPixmap from the QImage
|
||||
pixmap = QPixmap.fromImage(image)
|
||||
# Create a QPixmap from the QImage
|
||||
pixmap = QPixmap.fromImage(image)
|
||||
|
||||
if not pixmap:
|
||||
logger.error("Error loading image")
|
||||
return
|
||||
self.image_label.setPixmap(pixmap)
|
||||
if not pixmap:
|
||||
logger.error("Error loading image")
|
||||
return
|
||||
self.image_label.setPixmap(pixmap)
|
||||
except Exception as e:
|
||||
logger.error(f"Error loading image data: {e}")
|
||||
|
||||
def update_servers(self):
|
||||
found_servers = list(set(ZeroconfServer.found_hostnames() + self.added_hostnames))
|
||||
|
||||
@@ -32,20 +32,23 @@ class StatusBar(QStatusBar):
|
||||
|
||||
# Check for status change every 1s on background thread
|
||||
while True:
|
||||
new_status = proxy.status()
|
||||
new_image_name = image_names.get(new_status, 'Synchronize.png')
|
||||
image_path = os.path.join(resources_dir(), new_image_name)
|
||||
self.label.setPixmap((QPixmap(image_path).scaled(16, 16, Qt.AspectRatioMode.KeepAspectRatio)))
|
||||
try:
|
||||
# update status label - get download status
|
||||
new_status = proxy.status()
|
||||
if EngineManager.download_tasks:
|
||||
if len(EngineManager.download_tasks) == 1:
|
||||
task = EngineManager.download_tasks[0]
|
||||
new_status = f"{new_status} | Downloading {task.engine.capitalize()} {task.version}..."
|
||||
else:
|
||||
new_status = f"{new_status} | Downloading {len(EngineManager.download_tasks)} engines"
|
||||
self.messageLabel.setText(new_status)
|
||||
|
||||
# add download status
|
||||
if EngineManager.download_tasks:
|
||||
if len(EngineManager.download_tasks) == 1:
|
||||
task = EngineManager.download_tasks[0]
|
||||
new_status = f"{new_status} | Downloading {task.engine.capitalize()} {task.version}..."
|
||||
else:
|
||||
new_status = f"{new_status} | Downloading {len(EngineManager.download_tasks)} engines"
|
||||
|
||||
self.messageLabel.setText(new_status)
|
||||
# update status image
|
||||
new_image_name = image_names.get(new_status, 'Synchronize.png')
|
||||
new_image_path = os.path.join(resources_dir(), new_image_name)
|
||||
self.label.setPixmap((QPixmap(new_image_path).scaled(16, 16, Qt.AspectRatioMode.KeepAspectRatio)))
|
||||
except RuntimeError: # ignore runtime errors during shutdown
|
||||
pass
|
||||
time.sleep(1)
|
||||
|
||||
background_thread = threading.Thread(target=background_update,)
|
||||
|
||||
Reference in New Issue
Block a user