feat: correctly handle interrupt
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,6 @@
|
||||
/target
|
||||
|
||||
/*.txt
|
||||
|
||||
/players.redb
|
||||
/players.json
|
||||
|
||||
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -500,6 +500,16 @@ dependencies = [
|
||||
"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]]
|
||||
name = "curl"
|
||||
version = "0.4.48"
|
||||
@@ -925,6 +935,18 @@ dependencies = [
|
||||
"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]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
@@ -1428,6 +1450,7 @@ name = "sdgb-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"compio",
|
||||
"ctrlc",
|
||||
"futures-util",
|
||||
"nyquest-preset",
|
||||
"palc",
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user