Fix API rate limit handling in anonymous report and BunkerWeb Pro plugins download jobs

This commit is contained in:
Théophile Diot 2024-03-18 12:35:25 +00:00
parent f9378678c6
commit f98cc357ff
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 16 additions and 4 deletions

View file

@ -103,8 +103,12 @@ try:
data["bw_instances_number"] = str(len(JOB.db.get_instances()))
response = post("https://api.bunkerweb.io/data", json=data, headers={"User-Agent": f"BunkerWeb/{data['version']}"}, allow_redirects=True, timeout=10)
response.raise_for_status()
resp = post("https://api.bunkerweb.io/data", json=data, headers={"User-Agent": f"BunkerWeb/{data['version']}"}, allow_redirects=True, timeout=10)
if resp.status_code == 429:
LOGGER.warning("Anonymous report has been sent too many times, skipping for today")
else:
resp.raise_for_status()
cached, err = JOB.cache_file("last_report.json", dumps(data, indent=4).encode())
if not cached:

View file

@ -140,10 +140,12 @@ try:
resp_data = resp.json()
if db_metadata["is_pro"] and resp_data.get("action") == "clean":
clean_pro_plugins(db)
elif resp.status_code == 429:
LOGGER.warning("Too many requests to the remote server while checking BunkerWeb Pro license, please try again later")
sys_exit(0)
elif resp.status_code == 500:
LOGGER.error("An error occurred with the remote server while checking BunkerWeb Pro license, please try again later")
status = 2
sys_exit(status)
sys_exit(2)
else:
resp.raise_for_status()
@ -176,6 +178,9 @@ try:
metadata = default_metadata.copy()
db.set_pro_metadata(metadata)
clean_pro_plugins(db)
elif resp.status_code == 429:
LOGGER.warning("Too many requests to the remote server while checking BunkerWeb Pro plugins, please try again later")
sys_exit(0)
elif resp.headers.get("Content-Type", "") != "application/octet-stream":
LOGGER.error(f"Got unexpected content type: {resp.headers.get('Content-Type', 'missing')} from {API_ENDPOINT}/pro")
status = 2
@ -201,6 +206,9 @@ try:
LOGGER.error(f"Couldn't find Pro plugins for BunkerWeb version {data['version']} at {PREVIEW_ENDPOINT}/v{data['version']}.zip")
status = 2
sys_exit(status)
elif resp.status_code == 429:
LOGGER.warning("Too many requests to the remote server while checking Preview Pro plugins, please try again later")
sys_exit(0)
elif resp.headers.get("Content-Type", "") != "application/zip":
LOGGER.error(f"Got unexpected content type: {resp.headers.get('Content-Type', 'missing')} from {PREVIEW_ENDPOINT}/v{data['version']}.zip")
status = 2