Skip to content

Commit

Permalink
[#1525] Add the possibility to defined a custom timeout to api calls …
Browse files Browse the repository at this point in the history
…and set a linear one depending on the number of services upon reload
  • Loading branch information
TheophileDiot committed Oct 23, 2024
1 parent caaff13 commit 4abb20b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/common/core/letsencrypt/jobs/certbot-deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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']}"
Expand All @@ -52,7 +53,7 @@
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}")
Expand Down
7 changes: 4 additions & 3 deletions src/common/utils/ApiCaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ def send_to_apis(
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
Expand Down Expand Up @@ -67,10 +68,10 @@ def send_request(api, files):

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]
2 changes: 1 addition & 1 deletion src/scheduler/JobScheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __str_to_schedule(self, every: str) -> Job:

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
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ def check_plugin_changes(_type: Literal["external", "pro"] = "external"):
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}")
Expand Down Expand Up @@ -893,7 +893,7 @@ def check_plugin_changes(_type: Literal["external", "pro"] = "external"):
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 ...")
Expand Down

0 comments on commit 4abb20b

Please sign in to comment.