ed_journals/modules/journal/blocking/
live_journal_dir_reader.rsuse std::path::Path;
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use thiserror::Error;
use crate::journal::models::journal_event::JournalEvent;
use crate::journal::shared::journal_buffer::LiveJournalBuffer;
use crate::journal::LiveJournalBufferError;
use crate::logs::blocking::{LogDirReader, LogDirReaderError};
use crate::modules::journal::models::journal_event_kind::JournalEventKind;
use crate::modules::shared::blocking::sync_blocker::SyncBlocker;
#[derive(Debug)]
pub struct LiveJournalDirReader {
blocker: SyncBlocker,
_watcher: RecommendedWatcher,
log_dir_reader: LogDirReader,
journal_buffer: LiveJournalBuffer,
}
#[derive(Debug, Error)]
pub enum JournalDirWatcherError {
#[error(transparent)]
LogDirReader(#[from] LogDirReaderError),
#[error(transparent)]
LiveJournalBuffer(#[from] LiveJournalBufferError),
#[error(transparent)]
Notify(#[from] notify::Error),
}
impl LiveJournalDirReader {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, JournalDirWatcherError> {
let blocker = SyncBlocker::new();
let local_blocker = blocker.clone();
let dir_path = path.as_ref().to_path_buf();
let journal_buffer = LiveJournalBuffer::new(dir_path);
let local_journal_buffer = journal_buffer.clone();
let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| {
if let Ok(event) = res {
local_journal_buffer.handle_notify_event(event);
local_blocker.unblock();
}
})?;
watcher.watch(path.as_ref(), RecursiveMode::NonRecursive)?;
let log_dir_reader = LogDirReader::open(path);
Ok(LiveJournalDirReader {
blocker,
_watcher: watcher,
log_dir_reader,
journal_buffer,
})
}
}
impl Iterator for LiveJournalDirReader {
type Item = Result<JournalEvent, JournalDirWatcherError>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(log_event) = self.log_dir_reader.next() {
return Some(match log_event {
Ok(event) => Ok(JournalEvent {
is_live: self.log_dir_reader.is_reading_latest(),
kind: JournalEventKind::LogEvent(event),
}),
Err(error) => Err(error.into()),
});
}
match self.journal_buffer.next() {
Some(Ok(entry)) => return Some(Ok(entry)),
Some(Err(error)) => return Some(Err(error.into())),
None => {
self.blocker.block();
continue;
}
}
}
}
}