Handle CTRL-C shutdown

This commit is contained in:
Snowiiii
2024-08-24 11:17:36 +02:00
parent 43cbd6c2bb
commit fa1ca0782e
4 changed files with 61 additions and 2 deletions

38
Cargo.lock generated
View File

@@ -154,6 +154,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "cipher"
version = "0.4.4"
@@ -267,6 +273,16 @@ dependencies = [
"hybrid-array",
]
[[package]]
name = "ctrlc"
version = "3.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
dependencies = [
"nix",
"windows-sys 0.59.0",
]
[[package]]
name = "der"
version = "0.7.9"
@@ -823,6 +839,18 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.6.0",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "nom"
version = "7.1.3"
@@ -1066,6 +1094,7 @@ dependencies = [
"base64",
"bytes",
"crossbeam-channel",
"ctrlc",
"dhat",
"digest 0.11.0-pre.9",
"hmac",
@@ -2065,6 +2094,15 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"

View File

@@ -33,6 +33,8 @@ num-traits = "0.2"
num-derive = "0.4"
num-bigint = "0.4.6"
ctrlc = "3.4"
# encryption
rsa = "0.9.6"
rsa-der = "0.3.0"

View File

@@ -1,3 +1,6 @@
use pumpkin_core::text::color::NamedColor;
use pumpkin_core::text::TextComponent;
use crate::commands::tree::CommandTree;
use crate::commands::tree_builder::require;
@@ -7,7 +10,10 @@ const DESCRIPTION: &str = "Stop the server.";
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(&|sender| sender.permission_lvl() >= 4)
.execute(&|_sender, _args| std::process::exit(0)),
require(&|sender| sender.permission_lvl() >= 4).execute(&|sender, _args| {
sender
.send_message(TextComponent::text("Stopping Server").color_named(NamedColor::Red));
std::process::exit(0)
}),
)
}

View File

@@ -31,6 +31,8 @@ static ALLOC: dhat::Alloc = dhat::Alloc;
#[cfg(not(target_os = "wasi"))]
fn main() -> io::Result<()> {
use pumpkin_core::text::{color::NamedColor, TextComponent};
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
#[cfg(feature = "dhat-heap")]
@@ -39,6 +41,17 @@ fn main() -> io::Result<()> {
.enable_all()
.build()
.unwrap();
ctrlc::set_handler(|| {
log::warn!(
"{}",
TextComponent::text("Stopping Server")
.color_named(NamedColor::Red)
.to_pretty_console()
);
std::process::exit(0);
})
.unwrap();
// ensure rayon is built outside of tokio scope
rayon::ThreadPoolBuilder::new().build_global().unwrap();
rt.block_on(async {