mirror of
https://github.com/icewind1991/notify-redis.git
synced 2026-06-03 18:24:12 +02:00
update dependencies and better error reporting
This commit is contained in:
parent
95dcfedc38
commit
49652b7288
3 changed files with 398 additions and 555 deletions
902
Cargo.lock
generated
902
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -6,7 +6,8 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
notify = "4.0"
|
notify = "4.0"
|
||||||
redis = "0.10"
|
redis = "0.20"
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
color-eyre = "0.5"
|
||||||
70
src/main.rs
70
src/main.rs
|
|
@ -1,52 +1,13 @@
|
||||||
use chrono::{DateTime, Timelike, Utc};
|
use chrono::{DateTime, Timelike, Utc};
|
||||||
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
use redis::{Client, Commands, Connection, RedisResult};
|
use redis::{Client, Commands, Connection};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt;
|
|
||||||
use std::fmt::Display;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::result::Result;
|
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum WatchError {
|
|
||||||
Notify(notify::Error),
|
|
||||||
Redis(redis::RedisError),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<notify::Error> for WatchError {
|
|
||||||
fn from(err: notify::Error) -> WatchError {
|
|
||||||
WatchError::Notify(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<redis::RedisError> for WatchError {
|
|
||||||
fn from(err: redis::RedisError) -> WatchError {
|
|
||||||
WatchError::Redis(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for WatchError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
WatchError::Redis(err) => err.fmt(f),
|
|
||||||
WatchError::Notify(err) => err.fmt(f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for WatchError {
|
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
|
||||||
match self {
|
|
||||||
WatchError::Redis(err) => Some(err),
|
|
||||||
WatchError::Notify(err) => Some(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
#[serde(tag = "event")]
|
#[serde(tag = "event")]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
|
|
@ -82,28 +43,28 @@ impl From<DebouncedEvent> for Event {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watch(path: &str, redis_connect: &str, redis_list: &str) -> Result<(), WatchError> {
|
fn watch(path: &str, redis_connect: &str, redis_list: &str) -> Result<()> {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
|
|
||||||
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
|
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
|
||||||
let client = Client::open(redis_connect)?;
|
let client = Client::open(redis_connect).wrap_err("Invalid redis connection")?;
|
||||||
let con = client.get_connection()?;
|
let mut con = client
|
||||||
|
.get_connection()
|
||||||
|
.wrap_err("Failed to open redis connection")?;
|
||||||
|
|
||||||
watcher.watch(path, RecursiveMode::Recursive)?;
|
watcher.watch(path, RecursiveMode::Recursive)?;
|
||||||
|
|
||||||
loop {
|
while let Ok(event) = rx.recv() {
|
||||||
match rx.recv() {
|
push_event(event, &mut con, redis_list).wrap_err("Failed to send event to redis")?;
|
||||||
Ok(event) => push_event(event, &con, redis_list)?,
|
|
||||||
Err(e) => println!("watch error: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_event(event: DebouncedEvent, con: &Connection, list: &str) -> RedisResult<()> {
|
fn push_event(event: DebouncedEvent, con: &mut Connection, list: &str) -> Result<()> {
|
||||||
match format_event(event) {
|
match format_event(event) {
|
||||||
Some(formatted_event) => {
|
Some(formatted_event) => {
|
||||||
println!("{}", formatted_event);
|
println!("{}", formatted_event);
|
||||||
con.lpush(list, formatted_event)
|
Ok(con.lpush(list, formatted_event)?)
|
||||||
}
|
}
|
||||||
None => Ok(()),
|
None => Ok(()),
|
||||||
}
|
}
|
||||||
|
|
@ -117,13 +78,12 @@ fn format_event(event: DebouncedEvent) -> Option<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<()> {
|
||||||
let args: Vec<_> = env::args().collect();
|
let args: Vec<_> = env::args().collect();
|
||||||
if let [_, path, redis, list] = args.as_slice() {
|
if let [_, path, redis, list] = args.as_slice() {
|
||||||
if let Err(e) = watch(path, redis, list) {
|
watch(path, redis, list)?;
|
||||||
println!("error: {}", e)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
println!("usage: {} <path> <redis_connect> <redis_list>", args[0])
|
println!("usage: {} <path> <redis_connect> <redis_list>", args[0])
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue