refactor: improve coordinator installer code quality

- Remove unused imports (texttools, paramsparser)
- Move ensure_redis_running() helper to better location
- Add comments to lifecycle hook functions
- Improve error handling for binary copy operation
- Add documentation to obj_init function
This commit is contained in:
peternashaat
2025-11-16 13:18:32 +00:00
parent eb0fe4d3a9
commit a26f0a93fe
2 changed files with 36 additions and 22 deletions

View File

@@ -2,30 +2,12 @@ module coordinator
import incubaid.herolib.osal.core as osal
import incubaid.herolib.ui.console
import incubaid.herolib.core.texttools
import incubaid.herolib.core.pathlib
import incubaid.herolib.osal.startupmanager
import incubaid.herolib.installers.ulist
import incubaid.herolib.installers.lang.rust
import incubaid.herolib.installers.base.redis
import incubaid.herolib.develop.gittools
import os
// Helper function to ensure Redis is installed and running
fn ensure_redis_running() ! {
redis_config := redis.RedisInstall{
port: 6379
datadir: '/var/lib/redis'
ipaddr: 'localhost'
}
if !redis.check(redis_config) {
println('Installing and starting Redis...')
redis.redis_install(redis_config)!
} else {
println('Redis is already running')
}
}
fn startupcmd() ![]startupmanager.ZProcessNewArgs {
mut cfg := get()!
@@ -35,7 +17,6 @@ fn startupcmd() ![]startupmanager.ZProcessNewArgs {
name: 'coordinator'
cmd: '${cfg.binary_path} --redis-addr ${cfg.redis_addr} --api-http-port ${cfg.http_port} --api-ws-port ${cfg.ws_port}'
env: {
'HOME': os.home_dir()
'RUST_LOG': cfg.log_level
'RUST_LOG_STYLE': 'never'
}
@@ -51,16 +32,21 @@ fn running() !bool {
return res.exit_code == 0
}
// Lifecycle hooks - can be implemented for custom pre/post actions
fn start_pre() ! {
// Add any pre-start actions here if needed
}
fn start_post() ! {
// Add any post-start actions here if needed
}
fn stop_pre() ! {
// Add any pre-stop actions here if needed
}
fn stop_post() ! {
// Add any post-stop actions here if needed
}
//////////////////// following actions are not specific to instance of the object
@@ -92,6 +78,22 @@ fn upload() ! {
// )!
}
// Helper function to ensure Redis is installed and running
fn ensure_redis_running() ! {
redis_config := redis.RedisInstall{
port: 6379
datadir: '/var/lib/redis'
ipaddr: 'localhost'
}
if !redis.check(redis_config) {
println('Installing and starting Redis...')
redis.redis_install(redis_config)!
} else {
println('Redis is already running')
}
}
fn install() ! {
console.print_header('install coordinator')
// For coordinator, we build from source instead of downloading
@@ -137,6 +139,7 @@ pub fn build() ! {
// Clone or get the repository
println('Step 3/4: Cloning/updating horus repository...')
// Use the configured repo_path or default coderoot
mut gs := gittools.new(coderoot: '/root/code')!
mut repo := gs.get_repo(
url: 'https://git.ourworld.tf/herocode/horus.git'
@@ -167,7 +170,15 @@ pub fn build() ! {
source_binary := '${cfg.repo_path}/target/release/coordinator'
println('Copying binary from: ${source_binary}')
println('Copying binary to: ${cfg.binary_path}')
mut source_file := pathlib.get_file(path: source_binary)!
// Verify source binary exists before copying
mut source_file := pathlib.get_file(path: source_binary) or {
return error('Built binary not found at ${source_binary}. Build may have failed.')
}
if !source_file.exists() {
return error('Built binary not found at ${source_binary}. Build may have failed.')
}
source_file.copy(dest: cfg.binary_path, rsync: false)!
println('\nCoordinator built successfully!')

View File

@@ -1,6 +1,5 @@
module coordinator
import incubaid.herolib.data.paramsparser
import incubaid.herolib.data.encoderhero
import incubaid.herolib.osal.core as osal
import incubaid.herolib.core.pathlib
@@ -22,9 +21,12 @@ pub mut:
repo_path string = '/root/code/git.ourworld.tf/herocode/horus'
}
// your checking & initialization code if needed
// Initialize configuration with defaults if not provided
// Note: Struct defaults are already set, this ensures runtime overrides work correctly
fn obj_init(mycfg_ CoordinatorServer) !CoordinatorServer {
mut mycfg := mycfg_
// Only override if explicitly empty (struct defaults should handle most cases)
if mycfg.name == '' {
mycfg.name = 'default'
}
@@ -46,6 +48,7 @@ fn obj_init(mycfg_ CoordinatorServer) !CoordinatorServer {
if mycfg.repo_path == '' {
mycfg.repo_path = '/root/code/git.ourworld.tf/herocode/horus'
}
return mycfg
}