From 32c5bab3cea02efa82fdcdfe603cc84089b11126 Mon Sep 17 00:00:00 2001 From: toast Date: Wed, 26 Feb 2025 01:25:21 +1100 Subject: [PATCH] Update uptime command and other misc additions --- Cargo.lock | 101 ++++++++++++++++++------------------ Cargo.toml | 11 ++-- cmds/Cargo.toml | 2 +- cmds/src/dispatch/uptime.rs | 29 ++++++++++- src/errors.rs | 29 +++++++++++ src/main.rs | 35 ++++--------- src/shutdown.rs | 23 ++++++++ 7 files changed, 147 insertions(+), 83 deletions(-) create mode 100644 src/errors.rs create mode 100644 src/shutdown.rs diff --git a/Cargo.lock b/Cargo.lock index cfe6dea..cabbc9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,9 +119,9 @@ dependencies = [ [[package]] name = "bb8-redis" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3428672918816ef7533c206902630ccaceebf69ca8831a30c18eacf5cb619070" +checksum = "8a68215c76c457dccfed027a397ccd72482feabd64c2f93cbb2971a7c4121351" dependencies = [ "bb8", "redis", @@ -215,9 +215,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.14" +version = "1.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c3d1b2e905a3a7b00a6141adb0e4c0bb941d11caf55349d863942a1cc44e3c9" +checksum = "c736e259eea577f443d5c86c304f9f4ae0295c43f3ba05c21f1d66b5f06001af" dependencies = [ "shlex", ] @@ -446,9 +446,9 @@ dependencies = [ [[package]] name = "either" -version = "1.13.0" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +checksum = "b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d" [[package]] name = "encoding_rs" @@ -492,9 +492,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "flate2" -version = "1.0.35" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" +checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc" dependencies = [ "crc32fast", "miniz_oxide", @@ -682,9 +682,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" +checksum = "5017294ff4bb30944501348f6f8e42e6ad28f42c8bbef7a74029aff064a4e3c2" dependencies = [ "atomic-waker", "bytes", @@ -812,7 +812,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.7", + "h2 0.4.8", "http 1.2.0", "http-body 1.0.1", "httparse", @@ -1091,7 +1091,7 @@ dependencies = [ [[package]] name = "kon" -version = "0.6.5" +version = "0.6.6" dependencies = [ "kon_cmds", "kon_libs", @@ -1102,7 +1102,7 @@ dependencies = [ [[package]] name = "kon_cmds" -version = "0.1.3" +version = "0.1.4" dependencies = [ "dashmap 6.1.0", "kon_libs", @@ -1152,9 +1152,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.169" +version = "0.2.170" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "linux-raw-sys" @@ -1180,9 +1180,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.25" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" [[package]] name = "memchr" @@ -1223,9 +1223,9 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" +checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" dependencies = [ "adler2", ] @@ -1243,9 +1243,9 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dab59f8e050d5df8e4dd87d9206fb6f65a483e20ac9fda365ade4fab353196c" +checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" dependencies = [ "libc", "log", @@ -1318,9 +1318,9 @@ checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "openssl" -version = "0.10.70" +version = "0.10.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61cfb4e166a8bb8c9b55c500bc2308550148ece889be90f609377e58140f42c6" +checksum = "5e14130c6a98cd258fdcb0fb6d744152343ff729cbfcb28c656a9d12b999fbcd" dependencies = [ "bitflags 2.8.0", "cfg-if", @@ -1359,9 +1359,9 @@ dependencies = [ [[package]] name = "openssl-sys" -version = "0.9.105" +version = "0.9.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b22d5b84be05a8d6947c7cb71f7c849aa0f112acd4bf51c2a7c1c988ac0a9dc" +checksum = "8bb61ea9811cc39e3c2069f40b8b8e2e70d8569b361f879786cc7ed48b777cdd" dependencies = [ "cc", "libc", @@ -1420,7 +1420,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "poise" version = "0.6.1" -source = "git+https://github.com/serenity-rs/poise?branch=next#3feba0cb3b305aecae3ca47abe048b986813c8e4" +source = "git+https://github.com/serenity-rs/poise?branch=next#2c99507892037da2ec1b51b698e998349f1a9e31" dependencies = [ "async-trait", "derivative", @@ -1438,7 +1438,7 @@ dependencies = [ [[package]] name = "poise_macros" version = "0.6.1" -source = "git+https://github.com/serenity-rs/poise?branch=next#3feba0cb3b305aecae3ca47abe048b986813c8e4" +source = "git+https://github.com/serenity-rs/poise?branch=next#2c99507892037da2ec1b51b698e998349f1a9e31" dependencies = [ "darling", "proc-macro2", @@ -1542,9 +1542,9 @@ dependencies = [ [[package]] name = "redis" -version = "0.28.2" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e37ec3fd44bea2ec947ba6cc7634d7999a6590aca7c35827c250bc0de502bda6" +checksum = "9568894e8bdefd16512bca9e286a9d2abc27773609aa4eb7f428497d64df4373" dependencies = [ "arc-swap", "bytes", @@ -1555,6 +1555,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "ryu", + "socket2", "tokio", "tokio-util", "url", @@ -1562,9 +1563,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.8" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" dependencies = [ "bitflags 2.8.0", ] @@ -1653,7 +1654,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.4.7", + "h2 0.4.8", "http 1.2.0", "http-body 1.0.1", "http-body-util", @@ -1688,9 +1689,9 @@ dependencies = [ [[package]] name = "ring" -version = "0.17.9" +version = "0.17.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e75ec5e92c4d8aede845126adc388046234541629e76029599ed35a003c7ed24" +checksum = "da5349ae27d3887ca812fb375b45a4fbb36d8d12d2df394968cd86e35683fe73" dependencies = [ "cc", "cfg-if", @@ -1893,9 +1894,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" dependencies = [ "serde_derive", ] @@ -1911,9 +1912,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" dependencies = [ "proc-macro2", "quote", @@ -1922,9 +1923,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.138" +version = "1.0.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" +checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" dependencies = [ "itoa", "memchr", @@ -2182,9 +2183,9 @@ checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" [[package]] name = "tempfile" -version = "3.16.0" +version = "3.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" +checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" dependencies = [ "cfg-if", "fastrand", @@ -2506,15 +2507,15 @@ checksum = "74b08b0c1257381af16a5c3605254d529d3e7e109f3c62befc5d168968192998" [[package]] name = "typenum" -version = "1.17.0" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" [[package]] name = "typesize" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20304d891be0766f52123746c721d1190b953e874f9eccf29067a64c1a0ae16c" +checksum = "e29e4cac0f1acdbbe7b4deb46876a04246dc6abf60b6f2587bef8ae327cd134c" dependencies = [ "chrono", "dashmap 5.5.3", @@ -2547,9 +2548,9 @@ checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" [[package]] name = "untrusted" @@ -3026,9 +3027,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" +checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index ea0b893..b62829f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon" -version = "0.6.5" +version = "0.6.6" edition = "2024" [workspace] @@ -8,13 +8,13 @@ members = ["cmds", "libs", "repo", "tokens"] [workspace.dependencies] bb8 = "0.9.0" -bb8-redis = "0.20.0" +bb8-redis = "0.21.0" cargo_toml = "0.21.0" dashmap = "6.1.0" poise = "0.6.1" reqwest = { version = "0.12.12", features = ["json", "native-tls-vendored"] } -serde = "1.0.217" -serde_json = "1.0.134" +serde = "1.0.218" +serde_json = "1.0.139" sysinfo = "0.33.1" lazy_static = "1.5.0" tokio = { version = "1.42.0", features = ["macros", "signal", "rt-multi-thread"] } @@ -42,9 +42,6 @@ path = "src/main.rs" [profile.dev] opt-level = 0 -debug = true [profile.release] opt-level = 2 -debug = false -strip = true diff --git a/cmds/Cargo.toml b/cmds/Cargo.toml index fdb83c9..b24d1e2 100644 --- a/cmds/Cargo.toml +++ b/cmds/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kon_cmds" -version = "0.1.3" +version = "0.1.4" edition = "2024" [dependencies] diff --git a/cmds/src/dispatch/uptime.rs b/cmds/src/dispatch/uptime.rs index 2ae85c8..22aba85 100644 --- a/cmds/src/dispatch/uptime.rs +++ b/cmds/src/dispatch/uptime.rs @@ -45,6 +45,22 @@ fn get_os_info() -> String { format!("{name} {version}") } +fn fmt_mem(bytes: u64) -> String { + let units = ["B", "KB", "MB", "GB"]; + let mut bytes = bytes as f64; + let mut unit = units[0]; + + for &u in &units { + if bytes < 1024.0 { + unit = u; + break; + } + bytes /= 1024.0; + } + + format!("{bytes:.2} {unit}") +} + /// Retrieve host and bot uptimes #[poise::command(slash_command, install_context = "Guild|User", interaction_context = "Guild|BotDm|PrivateChannel")] pub async fn uptime(ctx: super::PoiseCtx<'_>) -> KonResult<()> { @@ -58,6 +74,16 @@ pub async fn uptime(ctx: super::PoiseCtx<'_>) -> KonResult<()> { // Fetch system's processor let cpu = sys.cpus(); + // Fetch system memory usage + let sram = fmt_mem(sys.used_memory()); + let sram_total = fmt_mem(sys.total_memory()); + + // Fetch process memory usage + let pram = match sys.process(sysinfo::get_current_pid().unwrap()) { + Some(proc) => fmt_mem(proc.memory()), + None => String::from("Unavailable") + }; + // Fetch bot's process uptime let curr_pid = sysinfo::get_current_pid().unwrap(); let now = SystemTime::now(); @@ -68,10 +94,11 @@ pub async fn uptime(ctx: super::PoiseCtx<'_>) -> KonResult<()> { } let stat_msg = [ - format!("**{} {}** `{GIT_COMMIT_HASH}:{GIT_COMMIT_BRANCH}`", bot.name, BOT_VERSION.as_str()), + format!("**{} v{}** `{GIT_COMMIT_HASH}:{GIT_COMMIT_BRANCH}`", bot.name, BOT_VERSION.as_str()), format!(">>> System: `{}`", format_duration(sys_uptime)), format!("Process: `{}`", format_duration(proc_uptime)), format!("CPU: `{}`", cpu[0].brand()), + format!("RAM: `{pram}` (`{sram}/{sram_total}`)"), format!("OS: `{}`", get_os_info()) ]; ctx.reply(stat_msg.join("\n")).await?; diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..7a80239 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,29 @@ +use { + kon_libs::{ + KonData, + KonError, + mention_dev + }, + poise::FrameworkError +}; + +pub async fn fw_errors(error: FrameworkError<'_, KonData, KonError>) { + match error { + poise::FrameworkError::Command { error, ctx, .. } => { + println!("PoiseCommandError({}): {error}", ctx.command().qualified_name); + ctx + .reply(format!( + "Encountered an error during command execution, ask {} to check console for more details!", + mention_dev(ctx).unwrap_or_default() + )) + .await + .expect("Error sending message"); + }, + poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {error}", event.snake_case_name()), + poise::FrameworkError::UnknownInteraction { interaction, .. } => println!( + "PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({})", + interaction.user.name, interaction.data.name + ), + other => println!("PoiseOtherError: {other}") + } +} diff --git a/src/main.rs b/src/main.rs index 666e871..0426abc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod errors; +mod shutdown; // https://cdn.toast-server.net/RustFSHiearchy.png // Using the new filesystem hierarchy @@ -9,8 +11,7 @@ use { GIT_COMMIT_BRANCH, GIT_COMMIT_HASH, KonData, - KonResult, - mention_dev + KonResult }, kon_tokens::token_path, poise::serenity_prelude::{ @@ -84,28 +85,7 @@ async fn main() { println!("Discord[{get_guild_name}]: {} ran /{}", ctx.author().name, ctx.command().qualified_name); }) }, - on_error: |error| { - Box::pin(async move { - match error { - poise::FrameworkError::Command { error, ctx, .. } => { - println!("PoiseCommandError({}): {error}", ctx.command().qualified_name); - ctx - .reply(format!( - "Encountered an error during command execution, ask {} to check console for more details!", - mention_dev(ctx).unwrap_or_default() - )) - .await - .expect("Error sending message"); - }, - poise::FrameworkError::EventHandler { error, event, .. } => println!("PoiseEventHandlerError({}): {error}", event.snake_case_name()), - poise::FrameworkError::UnknownInteraction { interaction, .. } => println!( - "PoiseUnknownInteractionError: {} tried to execute an unknown interaction ({})", - interaction.user.name, interaction.data.name - ), - other => println!("PoiseOtherError: {other}") - } - }) - }, + on_error: |error| Box::pin(async move { errors::fw_errors(error).await }), initialize_owners: true, ..Default::default() }) @@ -120,6 +100,13 @@ async fn main() { .await .expect("Error creating client"); + let shard_manager = client.shard_manager.clone(); + + tokio::spawn(async move { + shutdown::gracefully_shutdown().await; + shard_manager.shutdown_all().await; + }); + if let Err(why) = client.start().await { println!("Error starting client: {why:#?}"); } diff --git a/src/shutdown.rs b/src/shutdown.rs new file mode 100644 index 0000000..13832c0 --- /dev/null +++ b/src/shutdown.rs @@ -0,0 +1,23 @@ +use tokio::{ + select, + signal::unix::{ + SignalKind, + signal + } +}; + +pub async fn gracefully_shutdown() { + let [mut s1, mut s2, mut s3] = [ + signal(SignalKind::interrupt()).unwrap(), + signal(SignalKind::hangup()).unwrap(), + signal(SignalKind::terminate()).unwrap() + ]; + + select!( + v = s1.recv() => v.unwrap(), + v = s2.recv() => v.unwrap(), + v = s3.recv() => v.unwrap() + ); + + println!("\nKon says goodbye! 👋"); +}