[#1525] Add the possibility to defined a custom timeout to api calls and set a linear one depending on the number of services upon reload

This commit is contained in:
Théophile Diot 2024-10-23 13:49:17 +02:00
parent caaff13678
commit 4abb20b6e7
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
4 changed files with 9 additions and 7 deletions

View file

@ -35,6 +35,7 @@ try:
db = Database(LOGGER, sqlalchemy_string=getenv("DATABASE_URI", None))
instances = db.get_instances()
services = db.get_non_default_settings(global_only=True, methods=False, with_drafts=True, filtered_settings=("SERVER_NAME"))["SERVER_NAME"].split(" ")
for instance in instances:
endpoint = f"http://{instance['hostname']}:{instance['port']}"
@ -52,7 +53,7 @@ try:
LOGGER.info(
f"Successfully sent API request to {api.endpoint}/lets-encrypt/certificates",
)
sent, err, status, resp = api.request("POST", "/reload")
sent, err, status, resp = api.request("POST", "/reload", timeout=max(5, 2 * len(services)))
if not sent:
status = 1
LOGGER.error(f"Can't send API request to {api.endpoint}/reload : {err}")

View file

@ -29,10 +29,11 @@ class ApiCaller:
url: str,
files: Optional[Dict[str, BytesIO]] = None,
data: Optional[Dict[str, Any]] = None,
timeout=(5, 10),
response: bool = False,
) -> Tuple[bool, Optional[Dict[str, Any]]]:
def send_request(api, files):
sent, err, status, resp = api.request(method, url, files=files, data=data)
sent, err, status, resp = api.request(method, url, files=files, data=data, timeout=timeout)
return api, sent, err, status, resp
ret = True
@ -67,10 +68,10 @@ class ApiCaller:
return ret, responses
def send_files(self, path: str, url: str) -> bool:
def send_files(self, path: str, url: str, timeout=(5, 10)) -> bool:
with BytesIO() as tgz:
with tar_open(mode="w:gz", fileobj=tgz, dereference=True, compresslevel=3) as tf:
tf.add(path, arcname=".")
tgz.seek(0, 0)
files = {"archive.tar.gz": tgz}
return self.send_to_apis("POST", url, files=files)[0]
return self.send_to_apis("POST", url, files=files, timeout=timeout)[0]

View file

@ -128,7 +128,7 @@ class JobScheduler(ApiCaller):
def __reload(self) -> bool:
self.__logger.info("Reloading nginx...")
reload_success = self.send_to_apis("POST", "/reload")[0]
reload_success = self.send_to_apis("POST", "/reload", timeout=max(5, 2 * len(self.__env["SERVER_NAME"].split(" "))))[0]
if reload_success:
self.__logger.info("Successfully reloaded nginx")
return True

View file

@ -829,7 +829,7 @@ if __name__ == "__main__":
for thread in threads:
thread.join()
success, responses = SCHEDULER.send_to_apis("POST", "/reload", response=True)
success, responses = SCHEDULER.send_to_apis("POST", "/reload", timeout=max(5, 2 * len(env["SERVER_NAME"].split(" "))), response=True)
if not success:
reachable = False
LOGGER.debug(f"Error while reloading all bunkerweb instances: {responses}")
@ -893,7 +893,7 @@ if __name__ == "__main__":
for thread in tmp_threads:
thread.join()
if not SCHEDULER.send_to_apis("POST", "/reload")[0]:
if not SCHEDULER.send_to_apis("POST", "/reload", timeout=max(5, 2 * len(env["SERVER_NAME"].split(" "))))[0]:
LOGGER.error("Error while reloading bunkerweb with failover configuration, skipping ...")
elif not reachable:
LOGGER.warning("No BunkerWeb instance is reachable, skipping failover ...")