Skip to content

Commit

Permalink
Don't wait for the auto-update check at startup #1339
Browse files Browse the repository at this point in the history
- also add a shorter request timeout
  • Loading branch information
Acly committed Oct 29, 2024
1 parent 2467880 commit 15358e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
29 changes: 16 additions & 13 deletions ai_diffusion/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -91,18 +92,20 @@ 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:
request.setRawHeader(b"Authorization", f"Bearer {bearer}".encode("utf-8"))
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":
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions ai_diffusion/ui/diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion ai_diffusion/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 15358e6

Please sign in to comment.