Browse Source

Refactor error handling and service management in auto-boot-ollama-host.lua and ollama_manager.lua. Update functions to return service state alongside power state, improving control over service startup and shutdown based on user session status.

main
Bastian (BaM) 3 months ago
parent
commit
991f99885f
  1. 35
      scripts/auto-boot-ollama-host.lua
  2. 17
      scripts/ollama_manager.lua

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

@ -13,7 +13,7 @@ local ollama_manager = require("ollama_manager")
local session_check = require("session_check") local session_check = require("session_check")
-- Handle error pattern detection and recovery -- Handle error pattern detection and recovery
local function handle_error_pattern(config, powered_on) local function handle_error_pattern(config, powered_on, service_started)
utils.log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(config.OLLAMA_HOST, config.OLLAMA_PORT)) utils.log(("Detected EHOSTUNREACH for Ollama (%s:%d)."):format(config.OLLAMA_HOST, config.OLLAMA_PORT))
-- Check if user is currently logged into a desktop session -- Check if user is currently logged into a desktop session
@ -23,7 +23,7 @@ local function handle_error_pattern(config, powered_on)
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.") utils.log(("User '%s' is currently logged into a desktop session. Skipping Ollama startup to avoid interruption.")
:format(config.SSH_USER)) :format(config.SSH_USER))
return powered_on -- Return current powered_on state without changes return powered_on, service_started -- Return current states 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))
@ -46,15 +46,17 @@ local function handle_error_pattern(config, powered_on)
-- If SSH is reachable, the host is powered on (regardless of WOL status) -- If SSH is reachable, the host is powered on (regardless of WOL status)
powered_on = true powered_on = true
utils.log("SSH is reachable - host is powered on") utils.log("SSH is reachable - host is powered on")
ollama_manager.start_service(config) if ollama_manager.start_service(config) then
service_started = true
end
else else
utils.log("SSH timeout - host may not be powered on") utils.log("SSH timeout - host may not be powered on")
end end
return powered_on return powered_on, service_started
end end
-- Handle finish pattern detection and shutdown -- Handle finish pattern detection and shutdown
local function handle_finish_pattern(config, powered_on) local function handle_finish_pattern(config, powered_on, service_started)
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
@ -64,13 +66,25 @@ local function handle_finish_pattern(config, powered_on)
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( utils.log(("User '%s' is currently logged into a desktop session. Skipping shutdown to avoid interruption."):format(
config.SSH_USER)) config.SSH_USER))
return powered_on -- Exit without shutting down
-- If service was started, stop it since user is now active
if service_started then
utils.log("Stopping Ollama service since user is now active on desktop")
ollama_manager.stop_service(config)
service_started = false
end
return powered_on, service_started -- Exit without shutting down host
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) if service_started then
ollama_manager.stop_service(config)
end
ollama_manager.shutdown_host(config)
powered_on = false powered_on = false
return powered_on service_started = false
return powered_on, service_started
end end
-- Main application logic -- Main application logic
@ -80,6 +94,7 @@ local function main()
local cmd = ("docker logs -f --since %q %q 2>&1"):format(config.SINCE, config.CONTAINER_NAME) local cmd = ("docker logs -f --since %q %q 2>&1"):format(config.SINCE, config.CONTAINER_NAME)
local powered_on = false local powered_on = false
local service_started = false
while true do while true do
local fh = assert(io.popen(cmd, "r")) local fh = assert(io.popen(cmd, "r"))
@ -87,12 +102,12 @@ local function main()
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
powered_on = handle_error_pattern(config, powered_on) powered_on, service_started = handle_error_pattern(config, powered_on, service_started)
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
powered_on = handle_finish_pattern(config, powered_on) powered_on, service_started = handle_finish_pattern(config, powered_on, service_started)
break break
end end
end end

17
scripts/ollama_manager.lua

@ -20,19 +20,19 @@ function ollama_manager.start_service(config)
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl start ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) -- ssh.execute('wsl.exe -d Debian -- sudo systemctl start ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
-- Wait for service to become available -- Wait for service to become available
if network.port_is_up(config.OLLAMA_HOST, config.OLLAMA_PORT, 90) then if network.port_is_up(config.OLLAMA_HOST, config.OLLAMA_PORT, 60) then
utils.log("Ollama service is reachable again.") utils.log("Ollama service is reachable again")
utils.sleep(30) utils.sleep(30)
return true return true
else else
utils.log("Timeout waiting for Ollama service to come up after SSH command.") utils.log("Timeout waiting for Ollama service to come up after SSH command")
return false return false
end end
end end
-- Stop Ollama service and shutdown host -- Stop Ollama service
function ollama_manager.stop_service_and_shutdown(config) function ollama_manager.stop_service(config)
utils.log("Shutting down Ollama host to save power...") utils.log("Stopping Ollama service...")
-- Stop ollama service using nssm -- Stop ollama service using nssm
ssh.execute("nssm stop ollama", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) ssh.execute("nssm stop ollama", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
@ -40,6 +40,11 @@ function ollama_manager.stop_service_and_shutdown(config)
-- Alternative systemctl commands (commented out) -- Alternative systemctl commands (commented out)
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl disable ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) -- ssh.execute('wsl.exe -d Debian -- sudo systemctl disable ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
-- ssh.execute('wsl.exe -d Debian -- sudo systemctl stop ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) -- ssh.execute('wsl.exe -d Debian -- sudo systemctl stop ollama', config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
end
-- Shutdown the host
function ollama_manager.shutdown_host(config)
utils.log("Shutting down Ollama host to save power...")
-- Shutdown the host -- Shutdown the host
ssh.execute("shutdown.exe /s /t 0", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE) ssh.execute("shutdown.exe /s /t 0", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)

Loading…
Cancel
Save