Browse Source

Refactor log monitoring loop to continuously check for error and finish patterns, enhancing Ollama service management

main
Bastian (BaM) 3 months ago
parent
commit
e743f61aef
  1. 89
      scripts/auto-boot-ollama-host.lua

89
scripts/auto-boot-ollama-host.lua

@ -147,55 +147,62 @@ local function main()
log(("Looking for pattern: %q"):format(ERROR_PATTERN)) log(("Looking for pattern: %q"):format(ERROR_PATTERN))
local cmd = ("docker logs -f --since %q %q 2>&1"):format(SINCE, CONTAINER_NAME) local cmd = ("docker logs -f --since %q %q 2>&1"):format(SINCE, CONTAINER_NAME)
local fh = assert(io.popen(cmd, "r"))
while true do
for line in fh:lines() do
-- Plain substring match (no regex) local fh = assert(io.popen(cmd, "r"))
if line:find(ERROR_PATTERN, 1, true) ~= nil then for line in fh:lines() do
log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(OLLAMA_HOST, OLLAMA_PORT)) -- Plain substring match (no regex)
if line:find(ERROR_PATTERN, 1, true) ~= nil then
if WOL_MAC ~= "" then log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(OLLAMA_HOST, OLLAMA_PORT))
log(("Sending WOL to %s via %s:%d"):format(WOL_MAC, WOL_BCAST, WOL_PORT))
local ok, err = send_wol(WOL_MAC, WOL_BCAST, WOL_PORT) if WOL_MAC ~= "" then
if ok then log(("Sending WOL to %s via %s:%d"):format(WOL_MAC, WOL_BCAST, WOL_PORT))
log(("Sucessfully sent WOL to %s via %s:%d"):format(WOL_MAC, WOL_BCAST, WOL_PORT)) local ok, err = send_wol(WOL_MAC, WOL_BCAST, WOL_PORT)
else if ok then
log("WOL failed: " .. tostring(err)) log(("Sucessfully sent WOL to %s via %s:%d"):format(WOL_MAC, WOL_BCAST, WOL_PORT))
else
log("WOL failed: " .. tostring(err))
end
end end
end
-- Optional wait (kept commented for minimal parity) -- Optional wait (kept commented for minimal parity)
-- if port_is_up(OLLAMA_HOST, OLLAMA_PORT, UP_WAIT_TIMEOUT) then -- if port_is_up(OLLAMA_HOST, OLLAMA_PORT, UP_WAIT_TIMEOUT) then
-- log("Ollama reachable again.") -- log("Ollama reachable again.")
-- else -- else
-- log("Timeout waiting for Ollama.") -- log("Timeout waiting for Ollama.")
-- end -- end
log("Waiting for SSH to become reachable...") log("Waiting for SSH to become reachable...")
if port_is_up(OLLAMA_HOST, SSH_PORT, 60) then if port_is_up(OLLAMA_HOST, SSH_PORT, 60) then
log("SSH is reachable. Starting ollama service...") log("SSH is reachable. Starting ollama service...")
socket.sleep(2) socket.sleep(2)
ssh("wsl.exe -d Debian -- 'sudo systemctl enable ollama && sudo systemctl start ollama'", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE) ssh("wsl.exe -d Debian -- 'sudo systemctl enable ollama && sudo systemctl start ollama'", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE)
if (port_is_up(OLLAMA_HOST, OLLAMA_PORT, 90)) then if (port_is_up(OLLAMA_HOST, OLLAMA_PORT, 90)) then
log("Ollama service is reachable again.") log("Ollama service is reachable again.")
socket.sleep(30) socket.sleep(30)
else break
log("Timeout waiting for Ollama service to come up after SSH command.") else
log("Timeout waiting for Ollama service to come up after SSH command.")
end
end end
end end
end
if line:find(FINISH_PATTERN, 1, true) ~= nil then if line:find(FINISH_PATTERN, 1, true) ~= nil then
log(("Detected finish pattern: %q"):format(FINISH_PATTERN)) log(("Detected finish pattern: %q"):format(FINISH_PATTERN))
log("Shutting down Ollama host to save power...") log("Shutting down Ollama host to save power...")
ssh("wsl.exe -d Debian -- 'sudo systemctl disable ollama && sudo systemctl stop ollama'", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE) ssh("wsl.exe -d Debian -- 'sudo systemctl disable ollama && sudo systemctl stop ollama'", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE)
ssh("shutdown.exe /s /t 0", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE) ssh("shutdown.exe /s /t 0", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE)
socket.sleep(5)
break
end
end end
fh:close()
log("Log stream ended.")
end end
fh:close()
log("Log stream ended.")
end end
main() main()

Loading…
Cancel
Save