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))
local cmd = ("docker logs -f --since %q %q 2>&1"):format(SINCE, CONTAINER_NAME)
local fh = assert(io.popen(cmd, "r"))
for line in fh:lines() do
-- Plain substring match (no regex)
if line:find(ERROR_PATTERN, 1, true) ~= nil then
log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(OLLAMA_HOST, OLLAMA_PORT))
if WOL_MAC ~= "" then
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 ok then
log(("Sucessfully sent WOL to %s via %s:%d"):format(WOL_MAC, WOL_BCAST, WOL_PORT))
else
log("WOL failed: " .. tostring(err))
while true do
local fh = assert(io.popen(cmd, "r"))
for line in fh:lines() do
-- Plain substring match (no regex)
if line:find(ERROR_PATTERN, 1, true) ~= nil then
log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(OLLAMA_HOST, OLLAMA_PORT))
if WOL_MAC ~= "" then
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 ok then
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
-- Optional wait (kept commented for minimal parity)
-- if port_is_up(OLLAMA_HOST, OLLAMA_PORT, UP_WAIT_TIMEOUT) then
-- log("Ollama reachable again.")
-- else
-- log("Timeout waiting for Ollama.")
-- end
log("Waiting for SSH to become reachable...")
if port_is_up(OLLAMA_HOST, SSH_PORT, 60) then
log("SSH is reachable. Starting ollama service...")
socket.sleep(2)
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
log("Ollama service is reachable again.")
socket.sleep(30)
else
log("Timeout waiting for Ollama service to come up after SSH command.")
-- Optional wait (kept commented for minimal parity)
-- if port_is_up(OLLAMA_HOST, OLLAMA_PORT, UP_WAIT_TIMEOUT) then
-- log("Ollama reachable again.")
-- else
-- log("Timeout waiting for Ollama.")
-- end
log("Waiting for SSH to become reachable...")
if port_is_up(OLLAMA_HOST, SSH_PORT, 60) then
log("SSH is reachable. Starting ollama service...")
socket.sleep(2)
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
log("Ollama service is reachable again.")
socket.sleep(30)
break
else
log("Timeout waiting for Ollama service to come up after SSH command.")
end
end
end
end
if line:find(FINISH_PATTERN, 1, true) ~= nil then
log(("Detected finish pattern: %q"):format(FINISH_PATTERN))
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("shutdown.exe /s /t 0", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE)
if line:find(FINISH_PATTERN, 1, true) ~= nil then
log(("Detected finish pattern: %q"):format(FINISH_PATTERN))
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("shutdown.exe /s /t 0", "micro", OLLAMA_HOST, SSH_PORT, SSH_IDENTITY_FILE)
socket.sleep(5)
break
end
end
fh:close()
log("Log stream ended.")
end
fh:close()
log("Log stream ended.")
end
main()

Loading…
Cancel
Save