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.
This commit is contained in:
@@ -13,7 +13,7 @@ local ollama_manager = require("ollama_manager")
|
||||
local session_check = require("session_check")
|
||||
|
||||
-- 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))
|
||||
|
||||
-- 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
|
||||
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, service_started -- Return current states without changes
|
||||
end
|
||||
|
||||
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)
|
||||
powered_on = true
|
||||
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
|
||||
utils.log("SSH timeout - host may not be powered on")
|
||||
end
|
||||
return powered_on
|
||||
return powered_on, service_started
|
||||
end
|
||||
|
||||
-- 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))
|
||||
|
||||
-- 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
|
||||
utils.log(("User '%s' is currently logged into a desktop session. Skipping shutdown to avoid interruption."):format(
|
||||
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
|
||||
|
||||
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
|
||||
return powered_on
|
||||
service_started = false
|
||||
return powered_on, service_started
|
||||
end
|
||||
|
||||
-- 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 powered_on = false
|
||||
local service_started = false
|
||||
|
||||
while true do
|
||||
local fh = assert(io.popen(cmd, "r"))
|
||||
@@ -87,12 +102,12 @@ local function main()
|
||||
for line in fh:lines() do
|
||||
-- Handle error pattern detection
|
||||
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
|
||||
|
||||
-- Handle finish pattern detection
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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)
|
||||
|
||||
-- Wait for service to become available
|
||||
if network.port_is_up(config.OLLAMA_HOST, config.OLLAMA_PORT, 90) then
|
||||
utils.log("Ollama service is reachable again.")
|
||||
if network.port_is_up(config.OLLAMA_HOST, config.OLLAMA_PORT, 60) then
|
||||
utils.log("Ollama service is reachable again")
|
||||
utils.sleep(30)
|
||||
return true
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
-- Stop Ollama service and shutdown host
|
||||
function ollama_manager.stop_service_and_shutdown(config)
|
||||
utils.log("Shutting down Ollama host to save power...")
|
||||
-- Stop Ollama service
|
||||
function ollama_manager.stop_service(config)
|
||||
utils.log("Stopping Ollama service...")
|
||||
|
||||
-- Stop ollama service using nssm
|
||||
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)
|
||||
-- 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)
|
||||
end
|
||||
|
||||
-- Shutdown the host
|
||||
function ollama_manager.shutdown_host(config)
|
||||
utils.log("Shutting down Ollama host to save power...")
|
||||
|
||||
-- Shutdown the host
|
||||
ssh.execute("shutdown.exe /s /t 0", config.SSH_USER, config.OLLAMA_HOST, config.SSH_PORT, config.SSH_IDENTITY_FILE)
|
||||
|
||||
Reference in New Issue
Block a user