Refactor error and finish pattern handling in auto-boot-ollama-host script

Update the handle_error_pattern and handle_finish_pattern functions to return the powered_on state. This change improves the clarity of the powered_on logic and ensures consistent state management during Ollama startup and shutdown processes.
This commit is contained in:
Bastian (BaM)
2025-09-14 20:40:50 +02:00
parent 76ddedbf14
commit 93bccebdff

View File

@@ -22,12 +22,13 @@ local function handle_error_pattern(config, powered_on)
-- Check if user is currently logged into a desktop session -- Check if user is currently logged into a desktop session
utils.log("Checking if user is currently logged into desktop session...") utils.log("Checking if user is currently logged into desktop session...")
local user_logged_in = session_check.is_user_logged_in(config) local user_logged_in = session_check.is_user_logged_in(config)
if user_logged_in then if user_logged_in then
utils.log(("User '%s' is currently logged into a desktop session. Skipping Ollama startup to avoid interruption."):format(config.SSH_USER)) utils.log(("User '%s' is currently logged into a desktop session. Skipping Ollama startup to avoid interruption.")
:format(config.SSH_USER))
return powered_on -- Return current powered_on state without changes return powered_on -- Return current powered_on state without changes
end end
utils.log(("User '%s' is not logged into a desktop session. Proceeding with Ollama startup."):format(config.SSH_USER)) utils.log(("User '%s' is not logged into a desktop session. Proceeding with Ollama startup."):format(config.SSH_USER))
-- Send Wake-on-LAN if configured -- Send Wake-on-LAN if configured
@@ -45,25 +46,34 @@ local function handle_error_pattern(config, powered_on)
-- Wait for SSH and start service -- Wait for SSH and start service
utils.log("Waiting for SSH to become reachable...") utils.log("Waiting for SSH to become reachable...")
if network.port_is_up(config.OLLAMA_HOST, config.SSH_PORT, 60) then if network.port_is_up(config.OLLAMA_HOST, config.SSH_PORT, 60) then
-- If SSH is reachable, the host is powered on (regardless of WOL status)
powered_on = true
utils.log("SSH is reachable - host is powered on")
ollama_manager.start_service(config) ollama_manager.start_service(config)
else
utils.log("SSH timeout - host may not be powered on")
end end
return powered_on
end end
-- Handle finish pattern detection and shutdown -- Handle finish pattern detection and shutdown
local function handle_finish_pattern(config) local function handle_finish_pattern(config, powered_on)
utils.log(("Detected finish pattern: %q"):format(config.FINISH_PATTERN)) utils.log(("Detected finish pattern: %q"):format(config.FINISH_PATTERN))
-- Check if user is currently logged into a desktop session -- Check if user is currently logged into a desktop session
utils.log("Checking if user is currently logged into desktop session before shutdown...") utils.log("Checking if user is currently logged into desktop session before shutdown...")
local user_logged_in = session_check.is_user_logged_in(config) local user_logged_in = session_check.is_user_logged_in(config)
if user_logged_in then if user_logged_in then
utils.log(("User '%s' is currently logged into a desktop session. Skipping shutdown to avoid interruption."):format(config.SSH_USER)) utils.log(("User '%s' is currently logged into a desktop session. Skipping shutdown to avoid interruption."):format(
return -- Exit without shutting down config.SSH_USER))
return powered_on -- Exit without shutting down
end end
utils.log(("User '%s' is not logged into a desktop session. Proceeding with shutdown."):format(config.SSH_USER)) utils.log(("User '%s' is not logged into a desktop session. Proceeding with shutdown."):format(config.SSH_USER))
ollama_manager.stop_service_and_shutdown(config) ollama_manager.stop_service_and_shutdown(config)
powered_on = false
return powered_on
end end
-- Main application logic -- Main application logic
@@ -76,18 +86,16 @@ local function main()
while true do while true do
local fh = assert(io.popen(cmd, "r")) local fh = assert(io.popen(cmd, "r"))
for line in fh:lines() do for line in fh:lines() do
-- Handle error pattern detection -- Handle error pattern detection
if line:find(config.ERROR_PATTERN, 1, true) ~= nil then if line:find(config.ERROR_PATTERN, 1, true) ~= nil then
handle_error_pattern(config, powered_on) powered_on = handle_error_pattern(config, powered_on)
powered_on = true
end end
-- Handle finish pattern detection -- Handle finish pattern detection
if line:find(config.FINISH_PATTERN, 1, true) ~= nil and powered_on == true then if line:find(config.FINISH_PATTERN, 1, true) ~= nil and powered_on == true then
handle_finish_pattern(config) powered_on = handle_finish_pattern(config, powered_on)
powered_on = false
break break
end end
end end