From b10a6ea207ee4bcdee51be2052359b999eeb7bcf Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 14 Nov 2023 13:31:19 +0100 Subject: config hot reload --- src/config.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index a7abf3b..be41420 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,7 @@ +use crate::State; use anyhow::Context; +use inotify::{EventMask, Inotify, WatchMask}; +use log::{error, info}; use serde::{ de::{value, Error, SeqAccess, Visitor}, Deserialize, Deserializer, Serialize, @@ -10,6 +13,7 @@ use std::{ marker::PhantomData, net::SocketAddr, path::PathBuf, + sync::Arc, }; #[derive(Debug, Serialize, Deserialize)] @@ -168,3 +172,35 @@ impl Default for Limits { } } } + +pub fn setup_file_watch(config_path: String, state: Arc) { + std::thread::spawn(move || { + let mut inotify = Inotify::init().unwrap(); + inotify + .watches() + .add( + ".", + WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, + ) + .unwrap(); + let mut buffer = [0u8; 4096]; + loop { + let events = inotify + .read_events_blocking(&mut buffer) + .expect("Failed to read inotify events"); + + for event in events { + if event.mask.contains(EventMask::MODIFY) { + info!("reloading config"); + match Config::load(&config_path) { + Ok(conf) => { + let mut r = state.config.blocking_write(); + *r = Arc::new(conf) + } + Err(e) => error!("config has errors: {e}"), + } + } + } + } + }); +} -- cgit v1.2.3-70-g09d2