/// Begin monitoring the specified configuration file, if it exists
Begin monitoring the specified configuration file, if it exists
Begin monitoring the specified configuration file if it exists
pub async fn watch<P: Into<PathBuf>>(log: Logger, path: P) -> Result<ConfigMonitor> {
        let path = path.into();
        let config = BotConfig::load(&path).await.map_err(|e| {
            crit!(log, "load"; "status" => "failed", "error" => %e, "path" => %path.display());
            anyhow!("Failed loading initial configuration")
        })?;
        let (tx, rx) = watch::channel(Arc::new(config));
        let tx = ConfigUpdater(Arc::new(Mutex::new(Some(tx))));
        let rx = ConfigMonitor(rx);
        #[cfg(not(unix))]
        {
            tokio::spawn(async move {
                if tokio::signal::ctrl_c().await.is_ok() {
                    warn!(log, "shutdown"; "signal" => "interrupt");
                    tx.close();
                }
            });
        }
        #[cfg(unix)]
        {
            use tokio::signal::unix::{signal, SignalKind};
            tokio::spawn(async move {
                let mut term = signal(SignalKind::terminate()).unwrap();
                let mut int = signal(SignalKind::interrupt()).unwrap();
                let mut hup = signal(SignalKind::hangup()).unwrap();
                loop {
                    tokio::select! {
                        Some(_) = term.recv() => {
                            warn!(log, "shutdown"; "signal" => "term");
                            tx.close();
                            break;
                        },
                        Some(_) = int.recv() => {
                            warn!(log, "shutdown"; "signal" => "interrupt");
                            tx.close();
                            break;
                        },
                        Some(_) = hup.recv() => {
                            match BotConfig::load(&path).await {
                                Ok(c) => {
                                    warn!(log, "reload"; "status" => "updating", "path" => %path.display());
                                    tx.update(c);
                                }
                                Err(e) => {
                                    error!(log, "reload"; "status" => "ignored", "error" => %e, "path" => %path.display());
                                }
                            }
                        },
                        else => {
                            info!(log, "signal"; "status" => "loop exit");
                            break;
                        }
                    }
                }
            });
        }
        Ok(rx)
    }
pub async fn watch < P : Into < PathBuf > > ( log : Logger , path : P ) -> Result < ConfigMonitor > { let path = path . into ( ) ; let config = BotConfig :: load ( & path ) . await . map_err ( | e | { crit ! ( log " " " " " " " " e " " path display ( ) ) ; anyhow ! ( " " ) } ) ? ; let ( tx , rx ) = watch :: channel ( Arc :: new ( config ) ) ; let tx = ConfigUpdater ( Arc :: new ( Mutex :: new ( Some ( tx ) ) ) ) ; let rx = ConfigMonitor ( rx ) ; # [ cfg ( not ( unix ) ) ] { tokio :: spawn ( async move { if tokio :: signal :: ctrl_c ( ) . await . is_ok ( ) { warn ! ( log " " " " " " ) ; tx . close ( ) ; } } ) ; } # [ cfg ( unix ) ] { use tokio :: signal :: unix :: { signal , SignalKind } ; tokio :: spawn ( async move { let mut term = signal ( SignalKind :: terminate ( ) ) . unwrap ( ) ; let mut int = signal ( SignalKind :: interrupt ( ) ) . unwrap ( ) ; let mut hup = signal ( SignalKind :: hangup ( ) ) . unwrap ( ) ; loop { tokio :: select ! { Some ( ) term recv ( ) { warn ( log " " " " " " ) tx close ( ) break } Some ( ) int recv ( ) { warn ( log " " " " " " ) tx close ( ) break } Some ( ) hup recv ( ) { match BotConfig load ( path ) await { Ok ( c ) { warn ( log " " " " " " " " path display ( ) ) tx update ( c ) } Err ( e ) { error ( log " " " " " " " " e " " path display ( ) ) } } } else { info ( log " " " " " " ) break } } } } ) ; } Ok ( rx ) }
Begin monitoring the specified configuration file, if it exists
Begin monitoring the specified configuration file if it exists
log
Logger
path
P