actually implement config reloading using notify

This commit is contained in:
Adrian Malacoda 2018-02-25 02:52:40 -06:00
parent 6fe20b8b86
commit c0ee8b4d6d
3 changed files with 21 additions and 4 deletions

View File

@ -15,6 +15,7 @@ transformable_channels = "0.1.1"
time = "0.1"
regex = "0.2"
multimap = "0.4.0"
notify = "4.0.0"
pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" }
echobox = { git = "http://gitlab.monarch-pass.net/malacoda/echobox.git" }
stc = { git = "http://gitlab.monarch-pass.net/malacoda/stc.git" }

View File

@ -2,6 +2,9 @@ use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::sync::mpsc;
use std::time::Duration;
extern crate tenquestionmarks;
use tenquestionmarks::Tenquestionmarks;
@ -14,6 +17,8 @@ extern crate env_logger;
extern crate time;
extern crate crossbeam;
extern crate notify;
use notify::{RecommendedWatcher, Watcher, RecursiveMode, DebouncedEvent};
fn init_logger () {
let mut builder = env_logger::Builder::new();
@ -37,9 +42,20 @@ fn main () {
Ok(tqm) => {
info!("tenquestionmarks initialized successfully");
crossbeam::scope(|scope| {
//scope.spawn(|| {
// tqm.reconfigure(&read_config_from_file(&config_file_name));
//});
scope.spawn(|| {
let (notify_sender, notify_reciever) = mpsc::channel();
let mut watcher: RecommendedWatcher = Watcher::new(notify_sender, Duration::from_secs(2)).expect("Failed to create watcher");
watcher.watch(&config_file_name, RecursiveMode::NonRecursive).expect("Failed to watch config file");
loop {
if let Ok(event) = notify_reciever.recv() {
if let DebouncedEvent::Write(_) = event {
info!("Detected modified config file, reconfiguring");
tqm.reconfigure(&read_config_from_file(&config_file_name));
}
}
}
});
tqm.run();
})

View File

@ -107,4 +107,4 @@ responses = ["Did some carbon-based lifeform just say **I C E**?"]
type = "random"
parents = ["stdin", "discord"]
pattern = "^?slap (.*)"
responses = ["/me slaps $1 around a bit with a large trout"]
responses = ["/me slaps $1 around a bit with a large trout", "/me slaps $1 around a bit with a large brick"]