Files
continuwuity/src/admin/server/commands.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
3.8 KiB
Rust
Raw Normal View History

2025-01-24 07:04:29 +00:00
use std::{fmt::Write, path::PathBuf, sync::Arc};
2024-07-24 23:53:48 +00:00
2025-04-10 20:55:41 +00:00
use conduwuit::{
Err, Result, info,
utils::{stream::IterStream, time},
warn,
};
use futures::TryStreamExt;
2024-07-27 00:11:41 +00:00
use crate::admin_command;
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn uptime(&self) -> Result {
2024-07-27 00:11:41 +00:00
let elapsed = self
.services
2024-05-09 15:59:08 -07:00
.server
2024-04-26 18:55:45 -07:00
.started
.elapsed()
2024-07-06 06:47:42 +00:00
.expect("standard duration");
let result = time::pretty(elapsed);
self.write_str(&format!("{result}.")).await
2024-04-26 18:55:45 -07:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn show_config(&self) -> Result {
self.write_str(&format!("{}", *self.services.server.config))
.await
}
2025-01-24 07:04:29 +00:00
#[admin_command]
pub(super) async fn reload_config(&self, path: Option<PathBuf>) -> Result {
2025-01-24 07:04:29 +00:00
let path = path.as_deref().into_iter();
2025-01-28 20:55:28 +00:00
self.services.config.reload(path)?;
2025-01-24 07:04:29 +00:00
self.write_str("Successfully reconfigured.").await
2025-01-24 07:04:29 +00:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn list_features(&self, available: bool, enabled: bool, comma: bool) -> Result {
let delim = if comma { "," } else { " " };
2024-07-24 23:53:48 +00:00
if enabled && !available {
let features = info::rustc::features().join(delim);
let out = format!("`\n{features}\n`");
return self.write_str(&out).await;
2024-07-24 23:53:48 +00:00
}
if available && !enabled {
let features = info::cargo::features().join(delim);
let out = format!("`\n{features}\n`");
return self.write_str(&out).await;
2024-07-24 23:53:48 +00:00
}
let mut features = String::new();
let enabled = info::rustc::features();
let available = info::cargo::features();
for feature in available {
let active = enabled.contains(&feature.as_str());
let emoji = if active { "" } else { "" };
let remark = if active { "[enabled]" } else { "" };
2024-07-24 23:53:48 +00:00
writeln!(features, "{emoji} {feature} {remark}")?;
}
self.write_str(&features).await
2024-07-24 23:53:48 +00:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn memory_usage(&self) -> Result {
2024-07-27 00:11:41 +00:00
let services_usage = self.services.memory_usage().await?;
let database_usage = self.services.db.db.memory_usage()?;
let allocator_usage =
conduwuit::alloc::memory_usage().map_or(String::new(), |s| format!("\nAllocator:\n{s}"));
self.write_str(&format!(
"Services:\n{services_usage}\nDatabase:\n{database_usage}{allocator_usage}",
))
.await
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn clear_caches(&self) -> Result {
2024-07-27 00:11:41 +00:00
self.services.clear_cache().await;
self.write_str("Done.").await
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn list_backups(&self) -> Result {
2025-04-10 20:55:41 +00:00
self.services
.db
.db
.backup_list()?
.try_stream()
.try_for_each(|result| write!(self, "{result}"))
.await
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn backup_database(&self) -> Result {
let db = Arc::clone(&self.services.db);
2025-04-10 20:55:41 +00:00
let result = self
2024-07-27 00:11:41 +00:00
.services
2024-05-09 15:59:08 -07:00
.server
.runtime()
.spawn_blocking(move || match db.db.backup() {
2025-04-10 20:55:41 +00:00
| Ok(()) => "Done".to_owned(),
| Err(e) => format!("Failed: {e}"),
2024-05-09 15:59:08 -07:00
})
2024-07-27 00:11:41 +00:00
.await?;
2025-04-10 20:55:41 +00:00
let count = self.services.db.db.backup_count()?;
self.write_str(&format!("{result}. Currently have {count} backups."))
.await
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn admin_notice(&self, message: Vec<String>) -> Result {
2024-06-16 02:10:47 +00:00
let message = message.join(" ");
2024-07-27 00:11:41 +00:00
self.services.admin.send_text(&message).await;
2024-06-16 02:10:47 +00:00
self.write_str("Notice was sent to #admins").await
2024-06-16 02:10:47 +00:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn reload_mods(&self) -> Result {
2024-07-27 00:11:41 +00:00
self.services.server.reload()?;
2024-06-16 01:44:41 +00:00
self.write_str("Reloading server...").await
2024-06-16 01:44:41 +00:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
2024-06-16 19:46:32 +00:00
#[cfg(unix)]
pub(super) async fn restart(&self, force: bool) -> Result {
2024-12-14 21:58:01 -05:00
use conduwuit::utils::sys::current_exe_deleted;
2024-07-03 04:46:50 +00:00
if !force && current_exe_deleted() {
return Err!(
"The server cannot be restarted because the executable changed. If this is expected \
use --force to override."
);
2024-07-03 04:46:50 +00:00
}
2024-07-27 00:11:41 +00:00
self.services.server.restart()?;
2024-06-16 19:46:32 +00:00
self.write_str("Restarting server...").await
2024-06-16 19:46:32 +00:00
}
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn shutdown(&self) -> Result {
2024-06-16 01:44:41 +00:00
warn!("shutdown command");
2024-07-27 00:11:41 +00:00
self.services.server.shutdown()?;
2024-06-16 01:44:41 +00:00
self.write_str("Shutting down server...").await
2024-06-16 01:44:41 +00:00
}