From 8ee6b78f6dc03209cf1c5ec26bc0d3d799dc3c76 Mon Sep 17 00:00:00 2001 From: "Bastian (BaM)" Date: Mon, 15 Sep 2025 09:19:35 +0200 Subject: [PATCH] Enhance network.port_is_up function by adding a check_interval_sec parameter for customizable retry intervals. Update logging to reflect the new interval, improving feedback during port availability checks. --- scripts/network.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/network.lua b/scripts/network.lua index 42dea97..4d7ac4e 100644 --- a/scripts/network.lua +++ b/scripts/network.lua @@ -39,11 +39,12 @@ function network.get_socket() end -- Check if a TCP port is accepting connections within a timeout (seconds) -function network.port_is_up(host, port, timeout_sec) +function network.port_is_up(host, port, timeout_sec, check_interval_sec) -- Set defaults with clear intent host = host or "127.0.0.1" port = port or 0 timeout_sec = timeout_sec or 30 + check_interval_sec = check_interval_sec or 2 -- Convert to proper types host = tostring(host) @@ -58,20 +59,19 @@ function network.port_is_up(host, port, timeout_sec) -- Implement timeout loop with short intervals local start_time = os.time() - local check_interval = 1 -- Check every 1 second while (os.time() - start_time) < timeout do local cmd = string.format("nc -z -w1 %s %d 2>/dev/null", host, port) - local result = os.execute(cmd) - if result == 0 then + local success, reason, code = os.execute(cmd) + if success and reason == "exit" and code == 0 then utils.log("Port " .. port .. " is now available on " .. host) return true end -- Wait before next check if (os.time() - start_time) < timeout then - utils.log("Port " .. port .. " not yet available on " .. host .. ", retrying in " .. check_interval .. "s...") - os.execute("sleep " .. check_interval) + utils.log("Port " .. port .. " not yet available on " .. host .. ", retrying in " .. check_interval_sec .. "s...") + os.execute("sleep " .. check_interval_sec) end end