From 01738b8411feae96d8f0ce367649dd4762df6cb8 Mon Sep 17 00:00:00 2001 From: "dries.k" Date: Thu, 17 Feb 2022 14:00:31 +0100 Subject: [PATCH] improve manual reconnecting --- gotify_tray/gotify/api.py | 20 ++++++++++++++------ gotify_tray/gotify/listener.py | 3 --- gotify_tray/gui/MainApplication.py | 10 +++++++--- gotify_tray/tasks.py | 9 +++++++++ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/gotify_tray/gotify/api.py b/gotify_tray/gotify/api.py index b832507..19d2d11 100644 --- a/gotify_tray/gotify/api.py +++ b/gotify_tray/gotify/api.py @@ -183,23 +183,31 @@ class GotifyClient(GotifySession): self.listener.start() def opened_callback(self, user_callback: Callable[[], None] = None): - self.listener.reset_wait_time() if user_callback: user_callback() - def reconnect(self, increase_wait_time: bool = True): - if increase_wait_time: - self.listener.increase_wait_time() - self.listener.start() + def reconnect(self): + if not self.is_listening(): + self.listener.start() + self.reset_wait_time() def stop(self, reset_wait: bool = False): if reset_wait: - self.listener.reset_wait_time() + self.reset_wait_time() self.listener.stop() def is_listening(self) -> bool: return self.listener.running + def increase_wait_time(self): + self.listener.increase_wait_time() + + def get_wait_time(self) -> int: + return self.listener.wait_time + + def reset_wait_time(self): + self.listener.reset_wait_time() + """ Health """ diff --git a/gotify_tray/gotify/listener.py b/gotify_tray/gotify/listener.py index dc19656..0e53db9 100644 --- a/gotify_tray/gotify/listener.py +++ b/gotify_tray/gotify/listener.py @@ -68,10 +68,7 @@ class Listener(QtCore.QThread): def run(self): self.running = True - logger.debug(f"Listener: waiting {self.wait_time} seconds before connecting.") - try: - time.sleep(self.wait_time) self.ws.run_forever() finally: logger.debug("Listener: stopped.") diff --git a/gotify_tray/gui/MainApplication.py b/gotify_tray/gui/MainApplication.py index d2637df..7a3f83b 100644 --- a/gotify_tray/gui/MainApplication.py +++ b/gotify_tray/gui/MainApplication.py @@ -16,6 +16,7 @@ from gotify_tray.tasks import ( GetApplicationMessagesTask, GetMessagesTask, ServerConnectionWatchdogTask, + SleepTask, ) from gotify_tray.utils import get_abs_path, verify_server from PyQt6 import QtCore, QtGui, QtWidgets @@ -53,7 +54,7 @@ def init_logger(logger: logging.Logger): os.mkdir(logdir) logging.basicConfig( filename=os.path.join(logdir, f"{title}.log"), - format="%(levelname)s > %(name)s > %(asctime)s > %(message)s", + format="%(levelname)s > %(name)s > %(asctime)s > %(filename)20s:%(lineno)3s - %(funcName)20s() > %(message)s", ) @@ -131,14 +132,17 @@ class MainApplication(QtWidgets.QApplication): self.main_window.set_connecting() self.tray.set_icon_error() if not self.shutting_down: - self.gotify_client.reconnect() + self.gotify_client.increase_wait_time() + self.sleep_task = SleepTask(self.gotify_client.get_wait_time()) + self.sleep_task.finished.connect(self.gotify_client.reconnect) + self.sleep_task.start() def reconnect_callback(self): if not self.gotify_client.is_listening(): self.gotify_client.listener.reset_wait_time() + self.gotify_client.reconnect() else: self.gotify_client.stop(reset_wait=True) - self.gotify_client.reconnect(increase_wait_time=False) def insert_message( self, diff --git a/gotify_tray/tasks.py b/gotify_tray/tasks.py index b74c81b..636810e 100644 --- a/gotify_tray/tasks.py +++ b/gotify_tray/tasks.py @@ -175,3 +175,12 @@ class ServerConnectionWatchdogTask(BaseTask): logger.debug( "ServerConnectionWatchdogTask: gotify_client is not listening" ) + + +class SleepTask(BaseTask): + def __init__(self, secs: int): + super(SleepTask, self).__init__() + self.secs = secs + + def task(self): + time.sleep(self.secs)