From 4b6938e0f63ddb88590432438012b6ba71c86924 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 26 Apr 2024 18:55:45 -0700 Subject: [PATCH] add admin server uptime command Signed-off-by: Jason Volk --- src/service/admin/server/mod.rs | 6 +++++- src/service/admin/server/server_commands.rs | 18 ++++++++++++++++++ src/service/globals/mod.rs | 5 +++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/service/admin/server/mod.rs b/src/service/admin/server/mod.rs index b18b2c7a..e591f7c5 100644 --- a/src/service/admin/server/mod.rs +++ b/src/service/admin/server/mod.rs @@ -5,13 +5,16 @@ use ruma::events::room::message::RoomMessageEventContent; use self::server_commands::{ backup_database, clear_database_caches, clear_service_caches, list_backups, list_database_files, memory_usage, - show_config, + show_config, uptime, }; use crate::Result; #[cfg_attr(test, derive(Debug))] #[derive(Subcommand)] pub(crate) enum ServerCommand { + /// - Time elapsed since startup + Uptime, + /// - Show configuration values ShowConfig, @@ -43,6 +46,7 @@ pub(crate) enum ServerCommand { pub(crate) async fn process(command: ServerCommand, body: Vec<&str>) -> Result { Ok(match command { + ServerCommand::Uptime => uptime(body).await?, ServerCommand::ShowConfig => show_config(body).await?, ServerCommand::MemoryUsage => memory_usage(body).await?, ServerCommand::ClearDatabaseCaches { diff --git a/src/service/admin/server/server_commands.rs b/src/service/admin/server/server_commands.rs index 363f104b..b5e20651 100644 --- a/src/service/admin/server/server_commands.rs +++ b/src/service/admin/server/server_commands.rs @@ -2,6 +2,24 @@ use ruma::events::room::message::RoomMessageEventContent; use crate::{services, Result}; +pub(crate) async fn uptime(_body: Vec<&str>) -> Result { + let seconds = services() + .globals + .started + .elapsed() + .expect("standard duration") + .as_secs(); + let result = format!( + "up {} days, {} hours, {} minutes, {} seconds.", + seconds / 86400, + (seconds % 86400) / 60 / 60, + (seconds % 3600) / 60, + seconds % 60, + ); + + Ok(RoomMessageEventContent::notice_html(String::new(), result)) +} + pub(crate) async fn show_config(_body: Vec<&str>) -> Result { // Construct and send the response Ok(RoomMessageEventContent::text_plain(format!("{}", services().globals.config))) diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index b58ebbee..402934ef 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -7,7 +7,7 @@ use std::{ atomic::{self, AtomicBool}, Arc, }, - time::Instant, + time::{Instant, SystemTime}, }; use argon2::Argon2; @@ -63,7 +63,7 @@ pub(crate) struct Service<'a> { pub(crate) roomid_federationhandletime: RwLock>, pub(crate) stateres_mutex: Arc>, pub(crate) rotate: RotationHandler, - + pub(crate) started: SystemTime, pub(crate) shutdown: AtomicBool, pub(crate) argon: Argon2<'a>, } @@ -165,6 +165,7 @@ impl Service<'_> { stateres_mutex: Arc::new(Mutex::new(())), sync_receivers: RwLock::new(HashMap::new()), rotate: RotationHandler::new(), + started: SystemTime::now(), shutdown: AtomicBool::new(false), argon, };