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

2
.gitignore vendored
View File

@@ -1,4 +1,6 @@
/target /target
/*.txt /*.txt
/players.redb /players.redb
/players.json

23
Cargo.lock generated
View File

@@ -500,6 +500,16 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "ctrlc"
version = "3.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46f93780a459b7d656ef7f071fe699c4d3d2cb201c4b24d085b6ddc505276e73"
dependencies = [
"nix",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "curl" name = "curl"
version = "0.4.48" version = "0.4.48"
@@ -925,6 +935,18 @@ dependencies = [
"getrandom 0.2.16", "getrandom 0.2.16",
] ]
[[package]]
name = "nix"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@@ -1428,6 +1450,7 @@ name = "sdgb-cli"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"compio", "compio",
"ctrlc",
"futures-util", "futures-util",
"nyquest-preset", "nyquest-preset",
"palc", "palc",

View File

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

View File

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