Skip to content

Commit 09cf464

Browse files
authored
Optimize gateway startup and service update time (#2153)
Avoid running certbot if the certificate exists. This greatly reduces the gateway startup time when many services or entrypoints are being re-registered. Running certbot can take 3-4 seconds, even if the certificate already exists and there is nothing to do. Certificate renewal is not an issue as it is done by certbot's built-in systemd timer.
1 parent 361891c commit 09cf464

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • src/dstack/_internal/proxy/gateway/services

src/dstack/_internal/proxy/gateway/services/nginx.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ def write_conf(self, conf: str, conf_name: str) -> None:
105105
sudo_rm(conf_path)
106106
raise
107107

108-
@staticmethod
109-
def run_certbot(domain: str, acme: ACMESettings) -> None:
108+
@classmethod
109+
def run_certbot(cls, domain: str, acme: ACMESettings) -> None:
110+
if cls.certificate_exists(domain):
111+
return
112+
110113
logger.info("Running certbot for %s", domain)
111114

112115
cmd = ["sudo", "timeout", "--kill-after", str(CERTBOT_2ND_TIMEOUT), str(CERTBOT_TIMEOUT)]
@@ -134,6 +137,11 @@ def run_certbot(domain: str, acme: ACMESettings) -> None:
134137
if r.returncode != 0:
135138
raise ProxyError(f"Error obtaining {domain} TLS certificate:\n{r.stderr.decode()}")
136139

140+
@staticmethod
141+
def certificate_exists(domain: str) -> bool:
142+
cmd = ["sudo", "test", "-e", f"/etc/letsencrypt/live/{domain}/fullchain.pem"]
143+
return subprocess.run(cmd, timeout=2).returncode == 0
144+
137145
@staticmethod
138146
def get_config_name(domain: str) -> str:
139147
return f"443-{domain}.conf"

0 commit comments

Comments
 (0)