Skip to content

Commit

Permalink
Fix timer not firing (removed thread for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnsch committed Jul 16, 2020
1 parent 076a86b commit 1ed3f93
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/reports_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
time::Instant,
};
use tcn::{SignedReport, TemporaryContactNumber};
use timer::Timer;
use timer::{Guard, Timer};

pub trait TcnMatcher {
fn match_reports(
Expand Down Expand Up @@ -217,35 +217,41 @@ where
T: 'static + TcnDao,
{
tcn_batches_manager: Arc<Mutex<TcnBatchesManager<T>>>,
timer: Arc<Mutex<Timer>>,
_timer_data: TimerData
}

struct TimerData {
_timer: Arc<Mutex<Timer>>,
_guard: Guard
}

impl<T> ObservedTcnProcessorImpl<T>
where
T: 'static + TcnDao,
{
pub fn new(tcn_batches_manager: TcnBatchesManager<T>) -> ObservedTcnProcessorImpl<T> {
let tcn_batches_manager = Arc::new(Mutex::new(tcn_batches_manager));
let instance = ObservedTcnProcessorImpl {
tcn_batches_manager: Arc::new(Mutex::new(tcn_batches_manager)),
timer: Arc::new(Mutex::new(Timer::new())),
tcn_batches_manager: tcn_batches_manager.clone(),
_timer_data: Self::schedule_process_batches(tcn_batches_manager)

};
instance.schedule_process_batches();
instance
}

fn schedule_process_batches(&self) {
let timer_mutex = self.timer.clone();
let tcn_batches_manager = self.tcn_batches_manager.clone();
thread::spawn(move || {
let timer = timer_mutex.lock().unwrap();
timer.schedule_repeating(chrono::Duration::seconds(10), move || {
fn schedule_process_batches(tcn_batches_manager: Arc<Mutex<TcnBatchesManager<T>>>) -> TimerData {
let timer = Arc::new(Mutex::new(Timer::new()));
TimerData {
_timer: timer.clone(),
_guard: timer.clone().lock().unwrap().schedule_repeating(chrono::Duration::seconds(1), move || {
debug!("Flushing TCN batches into database");
let tcn_batches_manager_res = tcn_batches_manager.lock();
let tcn_batches_manager = expect_log!(tcn_batches_manager_res, "error");
let flush_res = tcn_batches_manager.flush();
expect_log!(flush_res, "Couldn't flush TCNs");
})
});

}
}
}

Expand Down Expand Up @@ -372,6 +378,8 @@ impl TcnDao for TcnDaoImpl {

// Overwrites if already exists
fn save_batch(&self, observed_tcns: Vec<ObservedTcn>) -> Result<(), ServicesError> {
debug!("Saving TCN batch: {:?}", observed_tcns);

self.db.transaction(|t| {
for tcn in observed_tcns {
let tcn_str = hex::encode(tcn.tcn.0);
Expand Down

0 comments on commit 1ed3f93

Please sign in to comment.