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