use std::thread;
use std::time::{Duration, Instant};
use sonic::store::fst::StoreFSTPool;
use sonic::store::kv::StoreKVPool;
#[derive(Clone)]
pub struct TaskerBuilder {
pub kv_pool: StoreKVPool,
pub fst_pool: StoreFSTPool,
}
pub struct Tasker {
kv_pool: StoreKVPool,
fst_pool: StoreFSTPool,
}
const TASKER_TICK_INTERVAL: Duration = Duration::from_secs(10);
impl TaskerBuilder {
pub fn build(&self) -> Tasker {
Tasker {
kv_pool: self.kv_pool.clone(),
fst_pool: self.fst_pool.clone(),
}
}
}
impl Tasker {
pub fn run(&self) {
tracing::info!("tasker is now active");
loop {
thread::sleep(TASKER_TICK_INTERVAL);
tracing::debug!("running a tasker tick...");
let tick_start = Instant::now();
self.tick();
let tick_took = tick_start.elapsed();
tracing::info!(
"ran tasker tick (took {}s + {}ms)",
tick_took.as_secs(),
tick_took.subsec_millis()
);
}
}
fn tick(&self) {
self.kv_pool.janitor();
self.fst_pool.janitor();
self.kv_pool.flush(false);
self.fst_pool.consolidate(false);
}
}