feat: correctly handle interrupt
This commit is contained in:
@@ -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