graceful exit

This commit is contained in:
2026-08-07 05:30:15 +02:00
parent d65e1aec0e
commit 02cd4c6347
6 changed files with 191 additions and 21 deletions
+22 -2
View File
@@ -2,18 +2,38 @@ pub mod account;
pub mod bot;
pub mod executor;
pub mod strategy;
pub mod types;
pub mod tradelog;
pub mod types;
use tokio::io::{self, AsyncBufReadExt, BufReader};
use tokio::sync::watch;
use crate::bot::Bot;
async fn shutdown_listener(tx: watch::Sender<bool>) {
let stdin = BufReader::new(io::stdin());
let mut lines = stdin.lines();
while let Ok(Some(line)) = lines.next_line().await {
if line.trim().eq_ignore_ascii_case("exit") {
println!("Shutdown requested.");
let _ = tx.send(true);
break;
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut builder = env_logger::Builder::from_default_env();
builder.filter_level(log::LevelFilter::Info);
builder.init();
let (shutdown_tx, shutdown_rx) = watch::channel(false);
tokio::spawn(shutdown_listener(shutdown_tx));
let bot = Bot::new().await?;
bot.start().await
bot.start(shutdown_rx).await
}