Files
continuwuity/src/core/server.rs
T

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

121 lines
3.1 KiB
Rust
Raw Normal View History

2024-05-09 15:59:08 -07:00
use std::{
2024-06-05 21:57:10 +00:00
sync::atomic::{AtomicBool, AtomicU32, Ordering},
2024-05-09 15:59:08 -07:00
time::SystemTime,
};
2024-06-01 08:03:20 +00:00
use tokio::{runtime, sync::broadcast};
2024-05-09 15:59:08 -07:00
use crate::{config::Config, log, Err, Result};
2024-05-09 15:59:08 -07:00
/// Server runtime state; public portion
pub struct Server {
/// Server-wide configuration instance
pub config: Config,
/// Timestamp server was started; used for uptime.
pub started: SystemTime,
/// Reload/shutdown pending indicator; server is shutting down. This is an
/// observable used on shutdown and should not be modified.
pub stopping: AtomicBool,
2024-06-01 08:03:20 +00:00
2024-05-09 15:59:08 -07:00
/// Reload/shutdown desired indicator; when false, shutdown is desired. This
/// is an observable used on shutdown and modifying is not recommended.
pub reloading: AtomicBool,
2024-05-09 15:59:08 -07:00
2024-06-16 19:46:32 +00:00
/// Restart desired; when true, restart it desired after shutdown.
pub restarting: AtomicBool,
2024-05-09 15:59:08 -07:00
/// Handle to the runtime
pub runtime: Option<runtime::Handle>,
/// Reload/shutdown signal
pub signal: broadcast::Sender<&'static str>,
2024-06-11 01:26:31 +00:00
/// Logging subsystem state
2024-06-26 00:19:35 +00:00
pub log: log::Log,
2024-05-09 15:59:08 -07:00
/// TODO: move stats
pub requests_spawn_active: AtomicU32,
pub requests_spawn_finished: AtomicU32,
pub requests_handle_active: AtomicU32,
pub requests_handle_finished: AtomicU32,
pub requests_panic: AtomicU32,
}
impl Server {
#[must_use]
2024-06-26 00:19:35 +00:00
pub fn new(config: Config, runtime: Option<runtime::Handle>, log: log::Log) -> Self {
2024-05-09 15:59:08 -07:00
Self {
config,
started: SystemTime::now(),
stopping: AtomicBool::new(false),
reloading: AtomicBool::new(false),
2024-06-16 19:46:32 +00:00
restarting: AtomicBool::new(false),
2024-05-09 15:59:08 -07:00
runtime,
signal: broadcast::channel::<&'static str>(1).0,
2024-06-11 01:26:31 +00:00
log,
2024-05-09 15:59:08 -07:00
requests_spawn_active: AtomicU32::new(0),
requests_spawn_finished: AtomicU32::new(0),
requests_handle_active: AtomicU32::new(0),
requests_handle_finished: AtomicU32::new(0),
requests_panic: AtomicU32::new(0),
}
}
2024-06-16 01:39:14 +00:00
pub fn reload(&self) -> Result<()> {
if cfg!(not(conduit_mods)) {
return Err!("Reloading not enabled");
2024-06-16 01:39:14 +00:00
}
if self.reloading.swap(true, Ordering::AcqRel) {
return Err!("Reloading already in progress");
2024-06-16 01:39:14 +00:00
}
if self.stopping.swap(true, Ordering::AcqRel) {
return Err!("Shutdown already in progress");
2024-06-16 01:39:14 +00:00
}
2024-07-06 13:53:21 +00:00
self.signal("SIGINT").inspect_err(|_| {
self.stopping.store(false, Ordering::Release);
self.reloading.store(false, Ordering::Release);
})
2024-06-16 01:39:14 +00:00
}
2024-06-16 19:46:32 +00:00
pub fn restart(&self) -> Result<()> {
if self.restarting.swap(true, Ordering::AcqRel) {
return Err!("Restart already in progress");
2024-06-16 19:46:32 +00:00
}
self.shutdown()
2024-07-06 13:53:21 +00:00
.inspect_err(|_| self.restarting.store(false, Ordering::Release))
2024-06-16 19:46:32 +00:00
}
2024-06-16 01:39:14 +00:00
pub fn shutdown(&self) -> Result<()> {
if self.stopping.swap(true, Ordering::AcqRel) {
return Err!("Shutdown already in progress");
2024-06-16 01:39:14 +00:00
}
self.signal("SIGTERM")
2024-07-06 13:53:21 +00:00
.inspect_err(|_| self.stopping.store(false, Ordering::Release))
2024-06-16 01:39:14 +00:00
}
pub fn signal(&self, sig: &'static str) -> Result<()> {
if let Err(e) = self.signal.send(sig) {
return Err!("Failed to send signal: {e}");
2024-06-16 01:39:14 +00:00
}
Ok(())
}
2024-05-09 15:59:08 -07:00
#[inline]
pub fn runtime(&self) -> &runtime::Handle {
self.runtime
.as_ref()
.expect("runtime handle available in Server")
}
2024-06-05 21:57:10 +00:00
#[inline]
pub fn running(&self) -> bool { !self.stopping.load(Ordering::Acquire) }
2024-05-09 15:59:08 -07:00
}