refactor: file-watcher doesn't debug log on each loop

This commit is contained in:
Paul Campbell 2024-06-30 20:10:56 +01:00
parent 40c61fa9ff
commit c85eee85e9

View file

@ -3,7 +3,6 @@ use actix::prelude::*;
use actix::Recipient; use actix::Recipient;
use inotify::{EventMask, Inotify, WatchMask}; use inotify::{EventMask, Inotify, WatchMask};
use std::{path::PathBuf, time::Duration}; use std::{path::PathBuf, time::Duration};
use tracing::{debug, info};
const CHECK_INTERVAL: Duration = Duration::from_secs(1); const CHECK_INTERVAL: Duration = Duration::from_secs(1);
@ -22,7 +21,6 @@ pub enum Error {
} }
pub struct FileWatcher { pub struct FileWatcher {
path: PathBuf,
inotify: Inotify, inotify: Inotify,
recipient: Recipient<FileUpdated>, recipient: Recipient<FileUpdated>,
run_interval: Option<SpawnHandle>, run_interval: Option<SpawnHandle>,
@ -31,11 +29,10 @@ impl FileWatcher {
pub fn new(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Self, Error> { pub fn new(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Self, Error> {
let inotify = Inotify::init()?; let inotify = Inotify::init()?;
inotify.watches().add( inotify.watches().add(
path.clone(), path,
WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE | WatchMask::ATTRIB, WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE | WatchMask::ATTRIB,
)?; )?;
Ok(Self { Ok(Self {
path,
inotify, inotify,
recipient, recipient,
run_interval: None, run_interval: None,
@ -46,7 +43,7 @@ impl Actor for FileWatcher {
type Context = Context<Self>; type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) { fn started(&mut self, ctx: &mut Self::Context) {
info!("Starting file watcher actor"); tracing::info!("Starting file watcher actor");
self.run_interval self.run_interval
.replace(ctx.run_interval(CHECK_INTERVAL, |_act, ctx| { .replace(ctx.run_interval(CHECK_INTERVAL, |_act, ctx| {
ctx.notify(WatchFile); ctx.notify(WatchFile);
@ -58,11 +55,10 @@ impl Handler<WatchFile> for FileWatcher {
type Result = (); type Result = ();
fn handle(&mut self, _msg: WatchFile, _ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, _msg: WatchFile, _ctx: &mut Self::Context) -> Self::Result {
debug!("Watching {} for activity...", self.path.display());
let mut buffer = [0u8; 4096]; let mut buffer = [0u8; 4096];
if let Ok(mut events) = self.inotify.read_events(&mut buffer) { if let Ok(mut events) = self.inotify.read_events(&mut buffer) {
if events.any(|event| event.mask.contains(EventMask::MODIFY)) { if events.any(|event| event.mask.contains(EventMask::MODIFY)) {
info!("File modified"); tracing::info!("File modified");
self.recipient.do_send(FileUpdated); self.recipient.do_send(FileUpdated);
}; };
} }