ed_journals/modules/logs/asynchronous/
live_log_dir_reader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use thiserror::Error;

use crate::logs::asynchronous::log_dir_reader::{LogDirReader, LogDirReaderError};
use crate::logs::content::LogEvent;
use crate::modules::shared::asynchronous::async_blocker::AsyncBlocker;

/// The async variant of [super::blocking::LiveLogDirReader]. Watches the whole journal dir and
/// reads all files. Once all historic files have been read the current read will only resolve once
/// the newest log file is changed at which it will read the active log file and return the entry.
///
/// ```rust
/// # use std::env::current_dir;
/// use std::path::PathBuf;
/// use ed_journals::logs::asynchronous::LiveLogDirReader;
///
/// # tokio_test::block_on(async {
/// let path = PathBuf::from("somePath");
/// # let path = current_dir()
/// #    .unwrap()
/// #    .join("test-files")
/// #    .join("journals");
/// let mut live_dir_reader = LiveLogDirReader::open(path)
///     .unwrap();
///
/// // At first this will read all existing lines from the journal logs, after which it will wait
/// // until it detects new entries in the latest log file.
/// while let Some(entry) = live_dir_reader.next().await {
///     // Do something with the entry
///     # break;
/// }
/// # });
/// ```
#[derive(Debug)]
pub struct LiveLogDirReader {
    blocker: AsyncBlocker,
    log_dir_reader: LogDirReader,
    _watcher: RecommendedWatcher,
    active: Arc<AtomicBool>,
}

#[derive(Debug, Error)]
pub enum LiveLogDirReaderError {
    #[error(transparent)]
    LogDirReaderError(#[from] LogDirReaderError),

    #[error(transparent)]
    NotifyError(#[from] notify::Error),
}

impl LiveLogDirReader {
    pub fn open<P: AsRef<Path>>(dir_path: P) -> Result<LiveLogDirReader, LiveLogDirReaderError> {
        let log_dir_reader = LogDirReader::open(&dir_path);

        let blocker = AsyncBlocker::new();
        let local_blocker = blocker.clone();

        let mut watcher = notify::recommended_watcher(move |_| {
            local_blocker.unblock_blocking();
        })?;

        watcher.watch(dir_path.as_ref(), RecursiveMode::NonRecursive)?;

        Ok(LiveLogDirReader {
            blocker,
            active: Arc::new(AtomicBool::new(true)),
            _watcher: watcher,
            log_dir_reader,
        })
    }

    pub async fn next(&mut self) -> Option<Result<LogEvent, LiveLogDirReaderError>> {
        loop {
            if !self.active.load(Ordering::Relaxed) || self.log_dir_reader.is_failing() {
                return None;
            }

            let Some(result) = self.log_dir_reader.next().await else {
                self.blocker.block().await;
                continue;
            };

            return Some(result.map_err(|e| e.into()));
        }
    }
}