From 5d52f423e07b26bff2262b9b91282e083d8dee69 Mon Sep 17 00:00:00 2001 From: neo Date: Mon, 2 Dec 2024 17:00:32 +0100 Subject: [PATCH] db fix --- src/components/archive_synchronizer.py | 14 ++++++++++++-- src/main.py | 1 - src/ui/helpers/__init__.py | 1 + src/ui/helpers/get_main_window.py | 8 ++++++++ src/ui/main_window/main_window.py | 6 ++++++ src/ui/test/test.py | 4 +++- 6 files changed, 30 insertions(+), 4 deletions(-) create mode 100755 src/ui/helpers/get_main_window.py diff --git a/src/components/archive_synchronizer.py b/src/components/archive_synchronizer.py index 03c5fde..dc3284f 100644 --- a/src/components/archive_synchronizer.py +++ b/src/components/archive_synchronizer.py @@ -20,6 +20,7 @@ from requests.adapters import HTTPAdapter, Retry from urllib3.exceptions import InsecureRequestWarning from .component import Component +from ui.helpers import get_main_window # Suppress insecure request warning requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) @@ -27,6 +28,7 @@ requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) class ArchiveSynchronizer(Component): def __init__(self, config=None, name=None, period=1, lazy=True, paused=False, threaded=True): super().__init__(config=config, name=name, period=period, lazy=lazy, paused=paused, threaded=threaded) + self.main_window = None self.simulate = "--sim-archiver" in sys.argv self.machine_status = "logged-in" self.machine_id = None @@ -52,6 +54,10 @@ class ArchiveSynchronizer(Component): @db.connection_context() def _get(self): + if self.main_window is None: + self.main_window = get_main_window() + + for record in list(Archive.select().where((Archive.archived != True) | (Archive.uploaded != True))): # using "is not True" breaks the query.. # list() forces the complete execution of the query unlocking the db unlike __enter__() if not self.simulate: if record.archived is not True: @@ -60,7 +66,9 @@ class ArchiveSynchronizer(Component): record.uploaded = self.remote_store(record) is True else: self.log.info("simulated archive synchronizer cycle") - record.save() + #record.save() + self.main_window.run_request.emit(record.save, [], {}) + if self.hold_time > 0: QThread.msleep(self.hold_time) self.gcs_bucket = None @@ -151,6 +159,8 @@ class ArchiveSynchronizer(Component): except (requests.ConnectionError, requests.Timeout) as e: self.log.warning(f"id: {record.id}: failed to archive remotely, archive_endpoint might be unreachable: {str(e)}") return False + self.log.info(f"Archived successfully: {record.id}") + return True def remote_store(self, record): @@ -183,7 +193,7 @@ class ArchiveSynchronizer(Component): return False except Exception: self.log.error(f"id: {record.id}: failed to store remotely:\n{traceback.format_exc()}") - self.log.info(f"id: {record.id}: stored remotely") + self.log.info(f"Stored successfully: {record.id}") return True def remote_fetch(self, remote_path=None, local_path=None): diff --git a/src/main.py b/src/main.py index 9a17125..a8ef38b 100644 --- a/src/main.py +++ b/src/main.py @@ -174,7 +174,6 @@ try: # GUI INIT if "--no-gui" not in sys.argv: - # self.main_window = Main_Window(self.bench) self.main_window = Main_Window() # CONNECT MAIN WINDOW ACTIONS self.main_window.logout_a.triggered.connect(lambda checked, selfie=weakref.ref(self): selfie().logout()) diff --git a/src/ui/helpers/__init__.py b/src/ui/helpers/__init__.py index 9ef44de..7cb7dbd 100644 --- a/src/ui/helpers/__init__.py +++ b/src/ui/helpers/__init__.py @@ -1,2 +1,3 @@ from .calc_foreground_color import calc_foreground_color +from .get_main_window import get_main_window from .replace_widget import replace_widget diff --git a/src/ui/helpers/get_main_window.py b/src/ui/helpers/get_main_window.py new file mode 100755 index 0000000..fec5fc5 --- /dev/null +++ b/src/ui/helpers/get_main_window.py @@ -0,0 +1,8 @@ +from PyQt5.QtWidgets import QApplication, QMainWindow + + +def get_main_window(): + tws = QApplication.topLevelWidgets() + for w in tws: + if isinstance(w, QMainWindow): + return w diff --git a/src/ui/main_window/main_window.py b/src/ui/main_window/main_window.py index 214edaf..b02ec80 100644 --- a/src/ui/main_window/main_window.py +++ b/src/ui/main_window/main_window.py @@ -5,10 +5,16 @@ from ui.window import Window class Main_Window(Window): do = pyqtSignal(dict) + run_request = pyqtSignal(object, list, dict) + + @staticmethod + def run_request_handler(fn, a, ka): + fn(*a, **ka) def __init__(self): super().__init__() self.do.connect(self._do) + self.run_request.connect(self.run_request_handler) # print("MAIN_WINDOW ", str(int(QThread.currentThreadId())), flush=True) @staticmethod diff --git a/src/ui/test/test.py b/src/ui/test/test.py index 1107316..3ffa7c7 100755 --- a/src/ui/test/test.py +++ b/src/ui/test/test.py @@ -368,7 +368,9 @@ class Test(Widget): elif self.step.step_type == "print": compiled_label = self.print(self.archived, self.step.spec.get("template", "EtichettaR5")) self.archived.label = compiled_label - self.archived.save() + self.log.info(f"Label printed. Saving...") + #self.archived.save() + self.main_window.run_request.emit(self.archived.save, [], {}) self.next_timer.start(500) elif self.step.step_type == "wait": self.next_timer.start(500)