diff --git a/src/config.rs b/src/config.rs index 02fd99b..1a6ad98 100644 --- a/src/config.rs +++ b/src/config.rs @@ -46,6 +46,10 @@ pub struct Config { /// If set, the bridge will check for new Factorio Friday Facts and send them to the chat. #[arg(long, env = "FACTORIO_FRIDAY_FACTS")] pub fff: bool, + + /// Retry connection if app crashed or server is down + #[arg(short = 'r', long, env)] + pub retry: bool, } impl Config { diff --git a/src/tasks/factorio.rs b/src/tasks/factorio.rs index 137879f..c35af3b 100644 --- a/src/tasks/factorio.rs +++ b/src/tasks/factorio.rs @@ -8,6 +8,20 @@ use tokio::sync::mpsc; use tracing::{error, info}; pub async fn run(tx: mpsc::Sender) -> Result<()> { + if CONFIG.retry { + loop { + if let Err(e) = create_reader(&tx).await { + error!("Error reading log file: {}", e); + } + } + } + + create_reader(&tx).await?; + + Ok(()) +} + +async fn create_reader(tx: &mpsc::Sender) -> Result<()> { crate::log_reader::read_log(|line| async { if let Some((username, message)) = parse_log_line(line) { tx.send(Signal::MessageFromFactorio { username, message }) @@ -15,9 +29,7 @@ pub async fn run(tx: mpsc::Sender) -> Result<()> { .unwrap(); } }) - .await?; - - Ok(()) + .await } pub async fn send(message: &str) {