From 15358e61c9979c5e7e5a64b2c1006ed3b7985cf4 Mon Sep 17 00:00:00 2001 From: Acly Date: Tue, 29 Oct 2024 11:31:54 +0100 Subject: [PATCH] Don't wait for the auto-update check at startup #1339 - also add a shorter request timeout --- ai_diffusion/network.py | 29 ++++++++++++++++------------- ai_diffusion/ui/diffusion.py | 6 ++++++ ai_diffusion/updates.py | 4 +++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/ai_diffusion/network.py b/ai_diffusion/network.py index 03d99b9b1..a7a9dbabf 100644 --- a/ai_diffusion/network.py +++ b/ai_diffusion/network.py @@ -38,17 +38,18 @@ def from_reply(reply: QNetworkReply): code = reply.error() # type: ignore (bug in PyQt5-stubs) url = reply.url().toString() status = reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute) - try: # extract detailed information from the payload - data = json.loads(reply.readAll().data()) - error = data.get("error", "Network error") - return NetworkError(code, f"{error} ({reply.errorString()})", url, status, data) - except: - try: - text = reply.readAll().data().decode("utf-8") - if text: - return NetworkError(code, f"{text} ({reply.errorString()})", url, status) + if reply.isReadable(): + try: # extract detailed information from the payload + data = json.loads(reply.readAll().data()) + error = data.get("error", "Network error") + return NetworkError(code, f"{error} ({reply.errorString()})", url, status, data) except: - pass + try: + text = reply.readAll().data().decode("utf-8") + if text: + return NetworkError(code, f"{text} ({reply.errorString()})", url, status) + except: + pass return NetworkError(code, reply.errorString(), url, status) @@ -91,11 +92,11 @@ def http( data: dict | QByteArray | None = None, bearer="", headers: Headers | None = None, + timeout: int | None = None, ): self._cleanup() request = QNetworkRequest(QUrl(url)) - # request.setTransferTimeout({"GET": 30000, "POST": 0}[method]) # requires Qt 5.15 (Krita 5.2) request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True) request.setRawHeader(b"ngrok-skip-browser-warning", b"69420") if bearer: @@ -103,6 +104,8 @@ def http( if headers: for key, value in headers: request.setRawHeader(key.encode("utf-8"), value.encode("utf-8")) + if timeout is not None: + request.setTransferTimeout(timeout) assert method in ["GET", "POST", "PUT"] if method == "POST": @@ -128,8 +131,8 @@ def http( self._requests[reply] = Request(url, future) return future - def get(self, url: str, bearer=""): - return self.http("GET", url, bearer=bearer) + def get(self, url: str, bearer="", timeout: int | None = None): + return self.http("GET", url, bearer=bearer, timeout=timeout) def post(self, url: str, data: dict, bearer=""): return self.http("POST", url, data, bearer=bearer) diff --git a/ai_diffusion/ui/diffusion.py b/ai_diffusion/ui/diffusion.py index 3a20386fc..f14ff89f0 100644 --- a/ai_diffusion/ui/diffusion.py +++ b/ai_diffusion/ui/diffusion.py @@ -56,8 +56,12 @@ def update_content(self): au = root.auto_update match au.state: + case UpdateState.checking: + self._update_status.setText(_("Checking for updates...")) + self._update_button.setEnabled(False) case UpdateState.available: self._update_status.setText(_("Latest version") + f": {au.latest_version}") + self._update_button.setEnabled(True) case UpdateState.downloading: self._update_status.setText(_("Downloading package...")) self._update_button.setEnabled(False) @@ -79,6 +83,7 @@ def is_visible(self): return settings.auto_update and root.auto_update.state not in [ UpdateState.latest, UpdateState.failed_check, + UpdateState.checking, ] def _toggle_auto_update(self): @@ -191,6 +196,7 @@ def __init__(self, server: Server): layout.addWidget(info, 0, Qt.AlignmentFlag.AlignRight) layout.addStretch() + self.update_content() root.auto_update.state_changed.connect(self.update_content) def update_content(self): diff --git a/ai_diffusion/updates.py b/ai_diffusion/updates.py index 25741fa5c..0dba13d0b 100644 --- a/ai_diffusion/updates.py +++ b/ai_diffusion/updates.py @@ -71,7 +71,9 @@ async def _check(self): self.state = UpdateState.checking log.info(f"Checking for latest plugin version at {self.api_url}") - result = await self._net.get(f"{self.api_url}/plugin/latest?version={self.current_version}") + result = await self._net.get( + f"{self.api_url}/plugin/latest?version={self.current_version}", timeout=10000 + ) self.latest_version = result.get("version") if not self.latest_version: log.error(f"Invalid plugin update information: {result}")