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 eframe::egui::Context;
use clap::Parser;
use tokio::sync::watch;
use clap::{Parser, Subcommand};
use tokio::sync::{watch, mpsc};
use sea_orm::Database;
use worker::visualizer::AppState;
@ -25,16 +25,8 @@ use gui::{
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct CliArgs {
/// Connection string for database to use
db: String,
/// Run background worker
#[arg(long, default_value_t = false)]
worker: bool,
/// Run user interface
#[arg(long, default_value_t = false)]
gui: bool,
#[clap(subcommand)]
mode: Mode,
/// Check interval for background worker
#[arg(short, long, default_value_t = 10)]
@ -49,6 +41,19 @@ struct CliArgs {
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:
#[cfg(target_arch = "wasm32")]
fn setup_tracing(_layer: InternalLoggerLayer) {
@ -83,22 +88,9 @@ fn main() {
setup_tracing(logger.layer());
let state = match AppState::new(
width_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();
match args.mode {
Mode::Worker { db_uri } => {
let run_rx_clone = run_rx.clone();
let db_uri = args.db.clone();
let worker = std::thread::spawn(move || {
tokio::runtime::Builder::new_current_thread()
.enable_all()
@ -116,6 +108,58 @@ fn main() {
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();
jobs.push(
@ -133,26 +177,11 @@ fn main() {
tokio::spawn(logger.worker(run_rx_clone.clone()))
);
if args.worker {
jobs.push(
tokio::spawn(
surveyor_loop(
db.clone(),
args.interval as i64,
args.cache_time as i64,
run_rx_clone.clone(),
)
state.worker(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() {
if let Err(e) = job.await {
@ -164,13 +193,10 @@ fn main() {
})
});
if args.gui {
let native_options = eframe::NativeOptions::default();
info!(target: "launcher", "Starting native GUI");
let db_name = args.db.clone().split('/').last().unwrap_or("").to_string();
eframe::run_native(
// TODO replace this with a loop that ends so we can cleanly exit the background worker
"dashboard",
@ -183,7 +209,7 @@ fn main() {
Box::new(
App::new(
cc,
db_name,
uri_tx,
args.interval as i64,
view,
width_tx,
@ -199,9 +225,11 @@ fn main() {
if let Err(e) = run_tx.send(false) {
error!(target: "launcher", "Error signaling end to workers: {:?}", e);
}
}
if let Err(e) = worker.join() {
error!(target: "launcher", "Error joining background thread : {:?}", e);
}
}
}
}