chore: Refactor coordinator configuration and status reporting

- Update default coordinator name to 'coordinator'
- Improve status reporting by using dedicated variables
- Adjust `zinit.get` call to use `create: true`
- Set `zinit_default` based on `args.name` when 'default' is provided
- Update `coordinatorServer.name` default to 'coordinator'
- Make 'coordinator' the default for `ArgsGet.name`
- Use `coordinator_default` for `ArgsGet.name` if set
- Adjust `CoordinatorServer.binary_path` default
- Update `zinit.get` to use `create: true`
- Log socket closure for debugging
- Remove unused import `incubaid.herolib.core.texttools`
This commit is contained in:
Mahmoud-Emad
2025-11-18 11:50:52 +02:00
parent 9dc33e3ce9
commit f9a2ebf24b
7 changed files with 71 additions and 53 deletions

View File

@@ -2,7 +2,6 @@ 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
@@ -13,7 +12,7 @@ import os
fn startupcmd() ![]startupmanager.ZProcessNewArgs {
mut cfg := get()!
mut res := []startupmanager.ZProcessNewArgs{}
res << 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}'
@@ -30,7 +29,11 @@ fn startupcmd() ![]startupmanager.ZProcessNewArgs {
fn running() !bool {
mut cfg := get()!
// Check if the process is running by checking the HTTP port
res := osal.exec(cmd: 'curl -fsSL http://127.0.0.1:${cfg.http_port} || exit 1', stdout: false, raise_error: false)!
res := osal.exec(
cmd: 'curl -fsSL http://127.0.0.1:${cfg.http_port} || exit 1'
stdout: false
raise_error: false
)!
return res.exit_code == 0
}
@@ -51,13 +54,13 @@ fn stop_post() ! {
// checks if a certain version or above is installed
fn installed() !bool {
mut cfg := get()!
// Check if the binary exists
mut binary := pathlib.get(cfg.binary_path)
if !binary.exists() {
return false
}
return true
}
@@ -85,7 +88,7 @@ fn install() ! {
pub fn build_coordinator() ! {
console.print_header('build coordinator')
println('📦 Starting coordinator build process...\n')
// Use default config instead of getting from factory
println(' Initializing configuration...')
mut cfg := CoordinatorServer{}
@@ -94,7 +97,7 @@ pub fn build_coordinator() ! {
println(' - Redis address: ${cfg.redis_addr}')
println(' - HTTP port: ${cfg.http_port}')
println(' - WS port: ${cfg.ws_port}\n')
// Ensure rust is installed
println('Step 1/3: Checking Rust dependency...')
if !osal.cmd_exists('rustc') {
@@ -106,7 +109,7 @@ pub fn build_coordinator() ! {
res := osal.exec(cmd: 'rustc --version', stdout: false, raise_error: false)!
println('Rust is already installed: ${res.output.trim_space()}\n')
}
// Clone or get the repository
println('Step 2/3: Cloning/updating horus repository...')
// Use the configured repo_path or default coderoot
@@ -116,42 +119,42 @@ pub fn build_coordinator() ! {
pull: true
reset: false
)!
// Update the path to the actual cloned repo
cfg.repo_path = repo.path()
println(' Repository ready at: ${cfg.repo_path}\n')
// Build the coordinator binary from the horus workspace
println('Step 3/3: Building coordinator binary...')
println('WARNING: This may take several minutes (compiling Rust code)...')
println('Running: cargo build -p hero-coordinator --release\n')
cmd := 'cd ${cfg.repo_path} && . ~/.cargo/env && RUSTFLAGS="-A warnings" cargo build -p hero-coordinator --release'
osal.execute_stdout(cmd)!
println('\n Build completed successfully')
// Ensure binary directory exists and copy the binary
println('📁 Preparing binary directory: ${cfg.binary_path}')
mut binary_path_obj := pathlib.get(cfg.binary_path)
osal.dir_ensure(binary_path_obj.path_dir())!
// Copy the built binary to the configured location
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)!
source_file.copy(dest: cfg.binary_path, rsync: false)!
println('\n🎉 Coordinator built successfully!')
println('📍 Binary location: ${cfg.binary_path}')
}
fn build() ! {
console.print_header('build coordinator')
mut cfg := get()!
// Ensure Redis is installed and running (required for coordinator)
console.print_debug('Checking if Redis is installed and running...')
redis_check := osal.exec(cmd: 'redis-cli -c -p 6379 ping', stdout: false, raise_error: false)!
@@ -167,7 +170,7 @@ fn build() ! {
} else {
console.print_debug('Redis is already running')
}
// Ensure rust is installed
console.print_debug('Checking if Rust is installed...')
mut rust_installer := rust.get()!
@@ -178,7 +181,7 @@ fn build() ! {
} else {
console.print_debug('Rust is already installed: ${res.output.trim_space()}')
}
// Clone or get the repository
console.print_debug('Cloning/updating horus repository...')
mut gs := gittools.new()!
@@ -187,43 +190,43 @@ fn build() ! {
pull: true
reset: false
)!
// Update the path to the actual cloned repo
cfg.repo_path = repo.path()
set(cfg)!
console.print_debug('Repository path: ${cfg.repo_path}')
// Build the coordinator binary from the horus workspace
console.print_header('Building coordinator binary (this may take several minutes ${cfg.repo_path})...')
console.print_debug('Running: cargo build -p hero-coordinator --release')
console.print_debug('Build output:')
cmd := 'cd ${cfg.repo_path} && . ~/.cargo/env && RUSTFLAGS="-A warnings" cargo build -p hero-coordinator --release'
osal.execute_stdout(cmd)!
console.print_debug('Build completed successfully')
// Ensure binary directory exists and copy the binary
console.print_header('Preparing binary directory: ${cfg.binary_path}')
mut binary_path_obj := pathlib.get(cfg.binary_path)
osal.dir_ensure(binary_path_obj.path_dir())!
// Copy the built binary to the configured location
source_binary := '${cfg.repo_path}/target/release/coordinator'
console.print_debug('Copying binary from: ${source_binary}')
console.print_debug('Copying binary to: ${cfg.binary_path}')
mut source_file := pathlib.get_file(path: source_binary)!
source_file.copy(dest: cfg.binary_path, rsync: false)!
console.print_header('coordinator built successfully at ${cfg.binary_path}')
}
fn destroy() ! {
mut server := get()!
server.stop()!
osal.process_kill_recursive(name: 'coordinator')!
// Remove the built binary
osal.rm(server.binary_path)!
}

View File

@@ -17,7 +17,7 @@ __global (
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
name string = 'coordinator'
binary_path string
redis_addr string
http_port int
@@ -38,19 +38,22 @@ pub fn new(args ArgsGet) !&CoordinatorServer {
log_level: args.log_level
repo_path: args.repo_path
}
// Try to set in Redis, if it fails (Redis not available), use in-memory config
set(obj) or {
console.print_debug('Redis not available, using in-memory configuration')
set_in_mem(obj)!
}
return get(name: args.name)!
}
pub fn get(args ArgsGet) !&CoordinatorServer {
pub fn get(args_ ArgsGet) !&CoordinatorServer {
mut args := args_
mut context := base.context()!
coordinator_default = args.name
if args.name == 'coordinator' && coordinator_default != '' {
args.name = coordinator_default
}
if args.fromdb || args.name !in coordinator_global {
mut r := context.redis()!
if r.hexists('context:coordinator', args.name)! {

View File

@@ -14,13 +14,13 @@ const default = true
@[heap]
pub struct CoordinatorServer {
pub mut:
name string = 'default'
binary_path string = os.join_path(os.home_dir(), 'hero/bin/coordinator')
redis_addr string = '127.0.0.1:6379'
http_port int = 8081
ws_port int = 9653
log_level string = 'info'
repo_path string = '/root/code/git.ourworld.tf/herocode/horus'
name string = 'coordinator'
binary_path string = os.join_path(os.home_dir(), 'hero/bin/coordinator')
redis_addr string = '127.0.0.1:6379'
http_port int = 8081
ws_port int = 9653
log_level string = 'info'
repo_path string = '/root/code/git.ourworld.tf/herocode/horus'
}
// your checking & initialization code if needed
@@ -30,7 +30,7 @@ fn obj_init(mycfg_ CoordinatorServer) !CoordinatorServer {
mycfg.name = 'default'
}
if mycfg.binary_path == '' {
mycfg.binary_path = os.join_path(os.home_dir(),'hero/bin/coordinator')
mycfg.binary_path = os.join_path(os.home_dir(), 'hero/bin/coordinator')
}
if mycfg.redis_addr == '' {
mycfg.redis_addr = '127.0.0.1:6379'