add limitTextFileLines before log

This commit is contained in:
2026-04-18 19:17:07 +07:00
parent 638e05a13a
commit 77c06befda

View File

@@ -127,6 +127,7 @@ function load_state(StateFilePath)
return State(cf, DateTime(lr))
end
catch e
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Warning loading state: $e")
end
return State(0, nothing)
@@ -206,6 +207,7 @@ end
function do_reboot()
cmd = reboot_command()
if cmd === nothing
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Reboot not supported on this OS")
return false
end
@@ -214,10 +216,11 @@ function do_reboot()
cmd_str = join(map(x -> replace(x, '"' => "\\\""), cmd), " ")
if DRY_RUN
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("DRY RUN: would run reboot command: $cmd_str")
return true
end
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Executing reboot command: $cmd_str")
try
# Construct a Cmd from an array so arguments are passed directly (no shell)
@@ -225,6 +228,7 @@ function do_reboot()
run(Cmd(cmd_array))
return true
catch e
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Failed to execute reboot command: $e")
return false
end
@@ -266,6 +270,7 @@ function perform_check!(st::State)
# During cooldown, track failures but don't trigger reboot yet
if success
broadcast_msg("Broadcasting from file: $thisFilePath")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("$ROUTER_IP is reachable during cooldown. Router is back online! Resetting state.")
st.consecutive_fails = 0
save_state(st, StateFilePath)
@@ -273,6 +278,7 @@ function perform_check!(st::State)
# println("2-2")
st.consecutive_fails += 1
broadcast_msg("Broadcasting from file: $thisFilePath")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("$ROUTER_IP is unreachable during cooldown. Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
end
@@ -284,6 +290,7 @@ function perform_check!(st::State)
# println("3-1")
if st.consecutive_fails > 0
# println("3-2")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("$ROUTER_IP is reachable; resetting consecutive failure counter.")
else
# logmsg("$ROUTER_IP is reachable.")
@@ -294,6 +301,7 @@ function perform_check!(st::State)
else
st.consecutive_fails += 1
broadcast_msg("Broadcasting from file: $thisFilePath")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("$ROUTER_IP is unreachable (last result: $routerresult). Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
save_state(st, StateFilePath)
end
@@ -305,6 +313,7 @@ function perform_check!(st::State)
if ok
# println("4-2")
broadcast_msg("Broadcasting from file: $thisFilePath")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Reboot executed (or simulated). Resetting failure counter.")
st.consecutive_fails = 0
st.last_reboot_datetime = Dates.now()
@@ -312,6 +321,7 @@ function perform_check!(st::State)
@show StateFilePath
save_state(st, StateFilePath)
else
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Reboot attempt failed; will retry after next interval.")
end
end
@@ -340,15 +350,16 @@ function main_loop()
end
end
# limit log file to latest 100 events
limitTextFileLines(LogFilePath; maxlines=100)
thisFilePath = @__FILE__
broadcast_msg("Broadcasting from file: $thisFilePath")
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Starting check loop. Checking router $ROUTER_IP every $(CHECK_INTERVAL_SECS) seconds.")
st = load_state(StateFilePath)
while true
try
perform_check!(st)
catch e
limitTextFileLines(LogFilePath; maxlines=100)
logmsg("Error during check: $e")
end
sleep(CHECK_INTERVAL_SECS)