use core::input::InputScope;
use std::time::Duration;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
#[derive(Debug, Clone)]
pub struct CancelHandle(Arc<AtomicBool>);
impl CancelHandle {
fn new() -> CancelHandle {
CancelHandle(Arc::new(AtomicBool::new(false)))
}
pub fn cancel(&self) {
self.0.store(true, SeqCst);
}
fn is_cancelled(&self) -> bool {
self.0.load(SeqCst)
}
}
fn set_schedule<F>(every: Duration, operation: F) -> CancelHandle
where
F: Fn() -> () + Send + 'static,
{
let handle = CancelHandle::new();
let inner_handle = handle.clone();
thread::Builder::new()
.name("dipstick-scheduler".to_string())
.spawn(move || loop {
thread::sleep(every);
if inner_handle.is_cancelled() {
break;
}
operation();
})
.unwrap(); handle
}
pub trait ScheduleFlush {
fn flush_every(&self, period: Duration) -> CancelHandle;
}
impl<T: InputScope + Send + Sync + Clone + 'static> ScheduleFlush for T {
fn flush_every(&self, period: Duration) -> CancelHandle {
let scope = self.clone();
set_schedule(period, move || {
if let Err(err) = scope.flush() {
error!("Could not flush metrics: {}", err);
}
})
}
}