Skip to content

Commit 3a8c744

Browse files
core: commonwealth: DHCPServerManager: Try to start DHCP server 3 times
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 2bd40b9 commit 3a8c744

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

core/libs/commonwealth/commonwealth/utils/DHCPServerManager.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,23 @@ def command_list(self) -> List[Union[str, pathlib.Path]]:
101101
]
102102

103103
def start(self) -> None:
104-
try:
105-
# pylint: disable=consider-using-with
106-
self._subprocess = subprocess.Popen(self.command_list(), shell=False, encoding="utf-8", errors="ignore")
107-
time.sleep(3)
108-
if not self.is_running():
109-
exit_code = self._subprocess.returncode
110-
raise RuntimeError(f"Failed to initialize Dnsmasq ({exit_code}).")
111-
logger.info("DHCP Server started.")
112-
except Exception as error:
113-
raise RuntimeError("Unable to start DHCP Server.") from error
104+
attempts = 3
105+
for attempt in range(attempts):
106+
try:
107+
# pylint: disable=consider-using-with
108+
self._subprocess = subprocess.Popen(self.command_list(), shell=False, encoding="utf-8", errors="ignore")
109+
time.sleep(3)
110+
if self.is_running():
111+
logger.info("DHCP Server started.")
112+
return
113+
else:
114+
exit_code = self._subprocess.returncode
115+
logger.error(f"Attempt {attempt + 1} failed to initialize Dnsmasq ({exit_code}).")
116+
except Exception as error:
117+
logger.error(f"Attempt {attempt + 1} encountered an error: {error}")
118+
time.sleep(1) # Wait a bit before retrying
119+
120+
raise RuntimeError("Unable to start DHCP Server")
114121

115122
def stop(self) -> None:
116123
if self.is_running():

0 commit comments

Comments
 (0)