Skip to content

Commit

Permalink
fix(check-update): handle cdn request failure
Browse files Browse the repository at this point in the history
fix #226
  • Loading branch information
NateScarlet committed Dec 20, 2021
1 parent b852938 commit 20c850e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
39 changes: 36 additions & 3 deletions auto_derby/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,53 @@

from . import window
from .__version__ import VERSION
from concurrent import futures
import logging

_VERSION_URL = "https://cdn.jsdelivr.net/gh/NateScarlet/auto-derby@master/version"
_LOGGER = logging.getLogger(__name__)


_VERSION_URLS = (
"https://cdn.jsdelivr.net/gh/NateScarlet/auto-derby@master/version",
"https://github.com/NateScarlet/auto-derby/raw/master/version",
"https://natescarlet.coding.net/p/github/d/auto-derby/git/raw/master/version",
)
_CHANGELOG_URL = "https://github.com/NateScarlet/auto-derby/blob/master/CHANGELOG.md"


def latest() -> Text:
def _http_get(url: Text) -> Text:
# Use `requests` if we have more http related feature
resp = cast.instance(
urllib.request.urlopen(_VERSION_URL),
urllib.request.urlopen(url),
http.client.HTTPResponse,
)
if resp.status != 200:
raise RuntimeError("response status %d: %s", resp.status, url)
return cast.text(resp.read())


def latest() -> Text:
pool = futures.ThreadPoolExecutor()

def _do(url: Text):
return url, _http_get(url)

jobs = [pool.submit(_do, url) for url in _VERSION_URLS]
try:
while jobs:
try:
done, jobs = futures.wait(jobs, return_when=futures.FIRST_COMPLETED)
url, ret = done.pop().result()
_LOGGER.info("lastest: %s from %s", ret, url)
return ret
except:
pass
finally:
pool.shutdown(False)
_LOGGER.warning("latest: all request failed, use current version")
return VERSION


def parse(v: Text) -> Tuple[int, int, int, Text]:
main, *extras = v.split("-")
if main.count(".") != 2:
Expand Down
4 changes: 4 additions & 0 deletions auto_derby/version_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ def test_parse():
assert version.parse("v0.1.2") == (0, 1, 2, "")
assert version.parse("0.1.2-rc.1") == (0, 1, 2, "rc.1")
assert version.parse("0.1.2-rc.1-build1") == (0, 1, 2, "rc.1-build1")


def test_latest():
assert version.latest()

0 comments on commit 20c850e

Please sign in to comment.