should work
This commit is contained in:
@@ -128,7 +128,6 @@ end
|
||||
function save_state(st::State, StateFilePath)
|
||||
obj = Dict("consecutive_fails" => st.consecutive_fails,
|
||||
"last_reboot_datetime" => st.last_reboot_datetime)
|
||||
@show StateFilePath
|
||||
JSON.json(StateFilePath, obj)
|
||||
end
|
||||
|
||||
@@ -227,16 +226,18 @@ end
|
||||
|
||||
# Single check iteration
|
||||
function perform_check!(st::State)
|
||||
# Check if we're in cooldown period
|
||||
in_cooldown = false
|
||||
if st.last_reboot_datetime !== nothing
|
||||
timepass = ((Dates.now() - st.last_reboot_datetime).value / 1000) |> floor |> Int
|
||||
|
||||
if timepass < COOLDOWN_AFTER_REBOOT_SECS
|
||||
logmsg("In cooldown after recent reboot; skipping check. $timepass/$COOLDOWN_AFTER_REBOOT_SECS seconds")
|
||||
return
|
||||
in_cooldown = true
|
||||
end
|
||||
end
|
||||
|
||||
success = false
|
||||
last_result = nothing
|
||||
last_res7ult = nothing
|
||||
for i in 1:ATTEMPTS_PER_CHECK
|
||||
ok, result = check_router_once(ROUTER_IP)
|
||||
last_result = result
|
||||
@@ -246,6 +247,39 @@ function perform_check!(st::State)
|
||||
end
|
||||
sleep(BACKOFF_BETWEEN_ATTEMPTS)
|
||||
end
|
||||
|
||||
if in_cooldown
|
||||
# During cooldown, track failures but don't trigger reboot yet
|
||||
if success
|
||||
logmsg("$ROUTER_IP is reachable during cooldown. Router is back online! Resetting state.")
|
||||
st.consecutive_fails = 0
|
||||
save_state(st, StateFilePath)
|
||||
else
|
||||
st.consecutive_fails += 1
|
||||
routerresult = isnothing(last_result) ? "no response" : last_result
|
||||
logmsg("$ROUTER_IP is unreachable during cooldown. Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
|
||||
save_state(st, StateFilePath)
|
||||
|
||||
# Check if we've reached threshold by now
|
||||
if st.consecutive_fails >= FAILS_TO_REBOOT
|
||||
logmsg("Cooldown has expired and router is still unreachable. Triggering reboot.")
|
||||
ok = do_reboot()
|
||||
if ok
|
||||
thisFilePath = @__FILE__
|
||||
broadcast_msg("Broadcasting from file: $thisFilePath")
|
||||
logmsg("Reboot executed (or simulated). Resetting failure counter.")
|
||||
st.consecutive_fails = 0
|
||||
st.last_reboot_datetime = Dates.now()
|
||||
save_state(st, StateFilePath)
|
||||
else
|
||||
logmsg("Reboot attempt failed; will retry after next interval.")
|
||||
end
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
# Outside cooldown - full check with potential reboot
|
||||
if success
|
||||
if st.consecutive_fails > 0
|
||||
logmsg("$ROUTER_IP is reachable; resetting consecutive failure counter.")
|
||||
@@ -257,7 +291,7 @@ function perform_check!(st::State)
|
||||
return
|
||||
else
|
||||
st.consecutive_fails += 1
|
||||
routerresult = isnothing(last_result) ? "no response" : last_result
|
||||
routerresult = isnothing(last_result) ? "no response" : last_result
|
||||
logmsg("$ROUTER_IP is unreachable (last result: $routerresult). Consecutive fails: $(st.consecutive_fails)/$FAILS_TO_REBOOT.")
|
||||
save_state(st, StateFilePath)
|
||||
end
|
||||
@@ -265,7 +299,7 @@ function perform_check!(st::State)
|
||||
save_state(st, StateFilePath)
|
||||
ok = do_reboot()
|
||||
if ok
|
||||
thisFilePath = @__FILE__
|
||||
thisFilePath = @__FILE__
|
||||
broadcast_msg("Broadcasting from file: $thisFilePath")
|
||||
logmsg("Reboot executed (or simulated). Resetting failure counter.")
|
||||
st.consecutive_fails = 0
|
||||
|
||||
Reference in New Issue
Block a user