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.
This commit is contained in:
@@ -39,11 +39,12 @@ function network.get_socket()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Check if a TCP port is accepting connections within a timeout (seconds)
|
-- 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
|
-- Set defaults with clear intent
|
||||||
host = host or "127.0.0.1"
|
host = host or "127.0.0.1"
|
||||||
port = port or 0
|
port = port or 0
|
||||||
timeout_sec = timeout_sec or 30
|
timeout_sec = timeout_sec or 30
|
||||||
|
check_interval_sec = check_interval_sec or 2
|
||||||
|
|
||||||
-- Convert to proper types
|
-- Convert to proper types
|
||||||
host = tostring(host)
|
host = tostring(host)
|
||||||
@@ -58,20 +59,19 @@ function network.port_is_up(host, port, timeout_sec)
|
|||||||
|
|
||||||
-- Implement timeout loop with short intervals
|
-- Implement timeout loop with short intervals
|
||||||
local start_time = os.time()
|
local start_time = os.time()
|
||||||
local check_interval = 1 -- Check every 1 second
|
|
||||||
|
|
||||||
while (os.time() - start_time) < timeout do
|
while (os.time() - start_time) < timeout do
|
||||||
local cmd = string.format("nc -z -w1 %s %d 2>/dev/null", host, port)
|
local cmd = string.format("nc -z -w1 %s %d 2>/dev/null", host, port)
|
||||||
local result = os.execute(cmd)
|
local success, reason, code = os.execute(cmd)
|
||||||
if result == 0 then
|
if success and reason == "exit" and code == 0 then
|
||||||
utils.log("Port " .. port .. " is now available on " .. host)
|
utils.log("Port " .. port .. " is now available on " .. host)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Wait before next check
|
-- Wait before next check
|
||||||
if (os.time() - start_time) < timeout then
|
if (os.time() - start_time) < timeout then
|
||||||
utils.log("Port " .. port .. " not yet available on " .. host .. ", retrying in " .. check_interval .. "s...")
|
utils.log("Port " .. port .. " not yet available on " .. host .. ", retrying in " .. check_interval_sec .. "s...")
|
||||||
os.execute("sleep " .. check_interval)
|
os.execute("sleep " .. check_interval_sec)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user