Add FINISH_PATTERN detection and adjust shutdown logic for Ollama host

This commit is contained in:
Bastian (BaM)
2025-09-14 14:15:49 +02:00
parent 05b799ff94
commit 1424458bcb

View File

@@ -43,6 +43,7 @@ local ERROR_PATTERN = getenv(
"ERROR_PATTERN", "ERROR_PATTERN",
("connect EHOSTUNREACH %s:%d"):format(OLLAMA_HOST, OLLAMA_PORT) ("connect EHOSTUNREACH %s:%d"):format(OLLAMA_HOST, OLLAMA_PORT)
) )
local FINISH_PATTERN = getenv("FINISH_PATTERN", "[DEBUG] Finished fetching. Found 0 documents.") -- e.g. "Server started"
-- Optional Wake-on-LAN -- Optional Wake-on-LAN
local WOL_MAC = getenv("WOL_MAC", "") -- e.g. "AA:BB:CC:DD:EE:FF" local WOL_MAC = getenv("WOL_MAC", "") -- e.g. "AA:BB:CC:DD:EE:FF"
@@ -178,15 +179,19 @@ local function main()
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(180) socket.sleep(2)
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)
else else
log("Timeout waiting for Ollama service to come up after SSH command.") 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
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)
end
end end
fh:close() fh:close()