feat: reworked CLI interface with subcommands

This commit is contained in:
əlemi 2022-11-05 03:28:35 +01:00
parent 4345a9e9b9
commit a6bc0da6fa
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -9,8 +9,8 @@ use tracing::{info, error};
use tracing_subscriber::filter::filter_fn; use tracing_subscriber::filter::filter_fn;
use eframe::egui::Context; use eframe::egui::Context;
use clap::Parser; use clap::{Parser, Subcommand};
use tokio::sync::watch; use tokio::sync::{watch, mpsc};
use sea_orm::Database; use sea_orm::Database;
use worker::visualizer::AppState; use worker::visualizer::AppState;
@ -25,16 +25,8 @@ use gui::{
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about)] #[command(author, version, about)]
struct CliArgs { struct CliArgs {
/// Connection string for database to use #[clap(subcommand)]
db: String, mode: Mode,
/// Run background worker
#[arg(long, default_value_t = false)]
worker: bool,
/// Run user interface
#[arg(long, default_value_t = false)]
gui: bool,
/// Check interval for background worker /// Check interval for background worker
#[arg(short, long, default_value_t = 10)] #[arg(short, long, default_value_t = 10)]
@ -49,6 +41,19 @@ struct CliArgs {
log_size: u64, log_size: u64,
} }
#[derive(Subcommand, Clone, Debug)]
enum Mode {
/// Run as background service fetching sources from db
Worker {
/// Connection string for database to use
db_uri: String,
},
/// Run as foreground user interface displaying collected data
GUI {
},
}
// When compiling for web: // When compiling for web:
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
fn setup_tracing(_layer: InternalLoggerLayer) { fn setup_tracing(_layer: InternalLoggerLayer) {
@ -83,22 +88,9 @@ fn main() {
setup_tracing(logger.layer()); setup_tracing(logger.layer());
let state = match AppState::new( match args.mode {
width_rx, Mode::Worker { db_uri } => {
args.interval as i64,
args.cache_time as i64,
) {
Ok(s) => s,
Err(e) => {
error!(target: "launcher", "Could not create application state: {:?}", e);
return;
}
};
let view = state.view();
let run_rx_clone = run_rx.clone(); let run_rx_clone = run_rx.clone();
let db_uri = args.db.clone();
let worker = std::thread::spawn(move || { let worker = std::thread::spawn(move || {
tokio::runtime::Builder::new_current_thread() tokio::runtime::Builder::new_current_thread()
.enable_all() .enable_all()
@ -116,6 +108,58 @@ fn main() {
let mut jobs = vec![]; let mut jobs = vec![];
jobs.push(
tokio::spawn(logger.worker(run_rx_clone.clone()))
);
jobs.push(
tokio::spawn(
surveyor_loop(
db.clone(),
args.interval as i64,
args.cache_time as i64,
run_rx_clone.clone(),
)
)
);
for (i, job) in jobs.into_iter().enumerate() {
if let Err(e) = job.await {
error!(target: "launcher", "Could not join task #{}: {:?}", i, e);
}
}
info!(target: "launcher", "Stopping background worker");
})
});
worker.join().unwrap();
},
Mode::GUI { } => {
let (uri_tx, uri_rx) = mpsc::channel(10);
let state = match AppState::new(
width_rx,
uri_rx,
args.interval as i64,
args.cache_time as i64,
) {
Ok(s) => s,
Err(e) => {
error!(target: "launcher", "Could not create application state: {:?}", e);
return;
}
};
let view = state.view();
let run_rx_clone = run_rx.clone();
let worker = std::thread::spawn(move || {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let mut jobs = vec![];
let run_rx_clone_clone = run_rx_clone.clone(); let run_rx_clone_clone = run_rx_clone.clone();
jobs.push( jobs.push(
@ -133,26 +177,11 @@ fn main() {
tokio::spawn(logger.worker(run_rx_clone.clone())) tokio::spawn(logger.worker(run_rx_clone.clone()))
); );
if args.worker {
jobs.push( jobs.push(
tokio::spawn( tokio::spawn(
surveyor_loop( state.worker(run_rx_clone.clone())
db.clone(),
args.interval as i64,
args.cache_time as i64,
run_rx_clone.clone(),
)
) )
); );
}
if args.gui {
jobs.push(
tokio::spawn(
state.worker(db, run_rx_clone.clone())
)
);
}
for (i, job) in jobs.into_iter().enumerate() { for (i, job) in jobs.into_iter().enumerate() {
if let Err(e) = job.await { if let Err(e) = job.await {
@ -164,13 +193,10 @@ fn main() {
}) })
}); });
if args.gui {
let native_options = eframe::NativeOptions::default(); let native_options = eframe::NativeOptions::default();
info!(target: "launcher", "Starting native GUI"); info!(target: "launcher", "Starting native GUI");
let db_name = args.db.clone().split('/').last().unwrap_or("").to_string();
eframe::run_native( eframe::run_native(
// TODO replace this with a loop that ends so we can cleanly exit the background worker // TODO replace this with a loop that ends so we can cleanly exit the background worker
"dashboard", "dashboard",
@ -183,7 +209,7 @@ fn main() {
Box::new( Box::new(
App::new( App::new(
cc, cc,
db_name, uri_tx,
args.interval as i64, args.interval as i64,
view, view,
width_tx, width_tx,
@ -199,9 +225,11 @@ fn main() {
if let Err(e) = run_tx.send(false) { if let Err(e) = run_tx.send(false) {
error!(target: "launcher", "Error signaling end to workers: {:?}", e); error!(target: "launcher", "Error signaling end to workers: {:?}", e);
} }
}
if let Err(e) = worker.join() { if let Err(e) = worker.join() {
error!(target: "launcher", "Error joining background thread : {:?}", e); error!(target: "launcher", "Error joining background thread : {:?}", e);
} }
}
}
} }