support reloading config via SIGUSR1
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
a567e314e9
commit
2f449ba47d
7 changed files with 75 additions and 10 deletions
55
src/service/config/mod.rs
Normal file
55
src/service/config/mod.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use std::{iter, path::Path, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use conduwuit::{
|
||||
config::{check, Config},
|
||||
error, implement, Result, Server,
|
||||
};
|
||||
|
||||
pub struct Service {
|
||||
server: Arc<Server>,
|
||||
}
|
||||
|
||||
const SIGNAL: &str = "SIGUSR1";
|
||||
|
||||
#[async_trait]
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self { server: args.server.clone() }))
|
||||
}
|
||||
|
||||
async fn worker(self: Arc<Self>) -> Result {
|
||||
while self.server.running() {
|
||||
if self.server.signal.subscribe().recv().await == Ok(SIGNAL) {
|
||||
if let Err(e) = self.handle_reload() {
|
||||
error!("Failed to reload config: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
||||
}
|
||||
|
||||
#[implement(Service)]
|
||||
fn handle_reload(&self) -> Result {
|
||||
if self.server.config.config_reload_signal {
|
||||
self.reload(iter::empty())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[implement(Service)]
|
||||
pub fn reload<'a, I>(&self, paths: I) -> Result<Arc<Config>>
|
||||
where
|
||||
I: Iterator<Item = &'a Path>,
|
||||
{
|
||||
let old = self.server.config.clone();
|
||||
let new = Config::load(paths).and_then(|raw| Config::new(&raw))?;
|
||||
|
||||
check::reload(&old, &new)?;
|
||||
self.server.config.update(new)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue