fix(static-sites): retry Bunny zone credential propagation
This commit is contained in:
parent
86964aa187
commit
d9ea6cf9b7
2 changed files with 55 additions and 18 deletions
|
|
@ -11,6 +11,7 @@ import os
|
|||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
|
@ -21,6 +22,8 @@ from typing import Any
|
|||
|
||||
MANIFEST_NAME = ".static-site-manifest.json"
|
||||
MANIFEST_VERSION = 1
|
||||
BUNNY_RETRY_DELAYS = (1, 2, 4, 8, 16)
|
||||
BUNNY_TRANSIENT_STATUS_CODES = {401, 408, 429, 500, 502, 503, 504}
|
||||
|
||||
|
||||
class DeployError(RuntimeError):
|
||||
|
|
@ -113,24 +116,34 @@ class BunnyStorage:
|
|||
) -> bytes | None:
|
||||
url_path = urllib.parse.quote(path, safe="/")
|
||||
request_headers = {"AccessKey": self.password, **(headers or {})}
|
||||
request = urllib.request.Request(
|
||||
f"{self.base_url}/{url_path}",
|
||||
data=body,
|
||||
headers=request_headers,
|
||||
method=method,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=60) as response:
|
||||
return response.read()
|
||||
except urllib.error.HTTPError as exc:
|
||||
if missing_ok and exc.code == 404:
|
||||
return None
|
||||
detail = exc.read().decode("utf-8", errors="replace")
|
||||
raise DeployError(
|
||||
f"Bunny Storage {method} {path} failed: HTTP {exc.code}: {detail}"
|
||||
) from exc
|
||||
except urllib.error.URLError as exc:
|
||||
raise DeployError(f"Bunny Storage {method} {path} failed: {exc.reason}") from exc
|
||||
for attempt in range(len(BUNNY_RETRY_DELAYS) + 1):
|
||||
request = urllib.request.Request(
|
||||
f"{self.base_url}/{url_path}",
|
||||
data=body,
|
||||
headers=request_headers,
|
||||
method=method,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=60) as response:
|
||||
return response.read()
|
||||
except urllib.error.HTTPError as exc:
|
||||
if missing_ok and exc.code == 404:
|
||||
return None
|
||||
detail = exc.read().decode("utf-8", errors="replace")
|
||||
if (
|
||||
exc.code in BUNNY_TRANSIENT_STATUS_CODES
|
||||
and attempt < len(BUNNY_RETRY_DELAYS)
|
||||
):
|
||||
time.sleep(BUNNY_RETRY_DELAYS[attempt])
|
||||
continue
|
||||
raise DeployError(
|
||||
f"Bunny Storage {method} {path} failed: HTTP {exc.code}: {detail}"
|
||||
) from exc
|
||||
except urllib.error.URLError as exc:
|
||||
raise DeployError(
|
||||
f"Bunny Storage {method} {path} failed: {exc.reason}"
|
||||
) from exc
|
||||
raise AssertionError("unreachable Bunny Storage retry state")
|
||||
|
||||
def get_manifest(self) -> bytes | None:
|
||||
return self._request("GET", MANIFEST_NAME, missing_ok=True)
|
||||
|
|
|
|||
Loading…
Reference in a new issue