implement actor terminal ui
This commit is contained in:
146
core/actor/examples/terminal_ui_main.rs
Normal file
146
core/actor/examples/terminal_ui_main.rs
Normal file
@@ -0,0 +1,146 @@
|
||||
//! Simplified main function for Baobab Actor TUI
|
||||
//!
|
||||
//! This binary provides a clean entry point for the actor monitoring and job dispatch interface.
|
||||
|
||||
use anyhow::{Result, Context};
|
||||
use baobab_actor::terminal_ui::{App, setup_and_run_tui};
|
||||
use clap::Parser;
|
||||
use log::{info, warn, error};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Child, Command};
|
||||
use tokio::signal;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "baobab-actor-tui")]
|
||||
#[command(about = "Terminal UI for Baobab Actor - Monitor and dispatch jobs to a single actor")]
|
||||
struct Args {
|
||||
/// Actor ID to monitor
|
||||
#[arg(short, long)]
|
||||
id: String,
|
||||
|
||||
/// Path to actor binary
|
||||
#[arg(short, long)]
|
||||
path: PathBuf,
|
||||
|
||||
/// Directory containing example .rhai scripts
|
||||
#[arg(short, long)]
|
||||
example_dir: Option<PathBuf>,
|
||||
|
||||
/// Redis URL for job queue
|
||||
#[arg(short, long, default_value = "redis://localhost:6379")]
|
||||
redis_url: String,
|
||||
|
||||
/// Enable verbose logging
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
/// Initialize logging based on verbosity level
|
||||
fn init_logging(verbose: bool) {
|
||||
if verbose {
|
||||
env_logger::Builder::from_default_env()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.init();
|
||||
} else {
|
||||
env_logger::Builder::from_default_env()
|
||||
.filter_level(log::LevelFilter::Info)
|
||||
.init();
|
||||
}
|
||||
}
|
||||
|
||||
/// Create and configure the TUI application
|
||||
fn create_app(args: &Args) -> Result<App> {
|
||||
App::new(
|
||||
args.id.clone(),
|
||||
args.path.clone(),
|
||||
args.redis_url.clone(),
|
||||
args.example_dir.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Spawn the actor binary as a background process
|
||||
fn spawn_actor_process(args: &Args) -> Result<Child> {
|
||||
info!("🎬 Spawning actor process: {}", args.path.display());
|
||||
|
||||
let mut cmd = Command::new(&args.path);
|
||||
|
||||
// Redirect stdout and stderr to null to prevent logs from interfering with TUI
|
||||
cmd.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null());
|
||||
|
||||
// Spawn the process
|
||||
let child = cmd
|
||||
.spawn()
|
||||
.with_context(|| format!("Failed to spawn actor process: {}", args.path.display()))?;
|
||||
|
||||
info!("✅ Actor process spawned with PID: {}", child.id());
|
||||
Ok(child)
|
||||
}
|
||||
|
||||
/// Cleanup function to terminate actor process
|
||||
fn cleanup_actor_process(mut actor_process: Child) {
|
||||
info!("🧹 Cleaning up actor process...");
|
||||
|
||||
match actor_process.try_wait() {
|
||||
Ok(Some(status)) => {
|
||||
info!("Actor process already exited with status: {}", status);
|
||||
}
|
||||
Ok(None) => {
|
||||
info!("Terminating actor process...");
|
||||
if let Err(e) = actor_process.kill() {
|
||||
error!("Failed to kill actor process: {}", e);
|
||||
} else {
|
||||
match actor_process.wait() {
|
||||
Ok(status) => info!("Actor process terminated with status: {}", status),
|
||||
Err(e) => error!("Failed to wait for actor process: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to check actor process status: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
// Initialize logging
|
||||
init_logging(args.verbose);
|
||||
|
||||
info!("🚀 Starting Baobab Actor TUI...");
|
||||
info!("Actor ID: {}", args.id);
|
||||
info!("Actor Path: {}", args.path.display());
|
||||
info!("Redis URL: {}", args.redis_url);
|
||||
|
||||
if let Some(ref example_dir) = args.example_dir {
|
||||
info!("Example Directory: {}", example_dir.display());
|
||||
}
|
||||
|
||||
// Spawn the actor process first
|
||||
let actor_process = spawn_actor_process(&args)?;
|
||||
|
||||
// Give the actor a moment to start up
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||
|
||||
// Create app and run TUI
|
||||
let app = create_app(&args)?;
|
||||
|
||||
// Set up signal handling for graceful shutdown
|
||||
let result = tokio::select! {
|
||||
tui_result = setup_and_run_tui(app) => {
|
||||
info!("TUI exited");
|
||||
tui_result
|
||||
}
|
||||
_ = signal::ctrl_c() => {
|
||||
info!("Received Ctrl+C, shutting down...");
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
// Clean up the actor process
|
||||
cleanup_actor_process(actor_process);
|
||||
|
||||
result
|
||||
}
|
Reference in New Issue
Block a user