feat: correctly handle interrupt

This commit is contained in:
mokurin000
2025-07-30 19:15:48 +08:00
parent 5ad0135deb
commit d35372b20a
4 changed files with 44 additions and 3 deletions

View File

@@ -31,3 +31,4 @@ spdlog-rs = { version = "0.4.3", default-features = false, features = [
] }
futures-util = "0.3.31"
redb = { version = "2.6.1", optional = true }
ctrlc = { version = "3.4.7", features = ["termination"] }

View File

@@ -1,6 +1,7 @@
use std::{
fs::OpenOptions,
io::{BufRead, stdin},
io::{self, BufRead},
sync::atomic::{AtomicBool, Ordering},
};
use futures_util::StreamExt;
@@ -28,10 +29,17 @@ use crate::commands::Cli;
mod cache;
mod commands;
static EARLY_QUIT: AtomicBool = AtomicBool::new(false);
#[cfg_attr(feature = "compio", compio::main)]
#[cfg_attr(feature = "tokio", tokio::main)]
async fn main() -> Result<(), Box<dyn snafu::Error>> {
nyquest_preset::register();
ctrlc::set_handler(|| {
EARLY_QUIT.store(true, Ordering::Relaxed);
})?;
let cmd = <Cli as Parser>::parse();
let client = ClientBuilder::default().build_async().await?;
@@ -91,7 +99,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
}
commands::Commands::ListAllUser => {
let mut stdin = stdin().lock();
let mut stdin = io::stdin().lock();
let mut buf = String::new();
let mut user_ids = Vec::new();
@@ -117,6 +125,10 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
let players = futures_util::stream::iter(user_ids)
.map(async |user_id| {
if EARLY_QUIT.load(Ordering::Relaxed) {
return Err("early skip due to ctrl-c")?;
}
#[cfg(feature = "cache")]
{
use redb::ReadableTable;
@@ -170,6 +182,9 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
.collect::<Vec<_>>()
.await;
#[cfg(feature = "cache")]
let _ = write.commit();
let output = OpenOptions::new()
.write(true)
.truncate(true)