Skip to content

brokkr-broker::utils::config_watcher

Rust

Configuration file watcher for hot-reload support.

This module provides functionality to watch for changes to the broker’s configuration file and trigger configuration reloads automatically.

pub

Derives: Debug, Clone

Configuration for the file watcher.

Name Type Description
config_file_path String Path to the configuration file to watch.
debounce_duration Duration Debounce duration to prevent rapid successive reloads.
enabled bool Whether the watcher is enabled.

pub

fn from_environment() -> Option<Self>

Creates a new ConfigWatcherConfig from environment variables.

Looks for BROKKR_CONFIG_FILE environment variable to determine the config file path. If not set, returns None (watcher disabled).

Source
pub fn from_environment() -> Option<Self> {
// Check if config file path is specified
let config_file_path = match std::env::var("BROKKR_CONFIG_FILE") {
Ok(path) if !path.is_empty() => path,
_ => {
debug!("BROKKR_CONFIG_FILE not set, config file watcher disabled");
return None;
}
};
// Verify the file exists
if !Path::new(&config_file_path).exists() {
warn!(
"Config file '{}' does not exist, config file watcher disabled",
config_file_path
);
return None;
}
// Check if watcher is explicitly disabled
let enabled = std::env::var("BROKKR_CONFIG_WATCHER_ENABLED")
.map(|v| v.to_lowercase() != "false" && v != "0")
.unwrap_or(true);
if !enabled {
info!("Config file watcher explicitly disabled via environment variable");
return None;
}
// Get debounce duration from environment (in seconds)
let debounce_secs = std::env::var("BROKKR_CONFIG_WATCHER_DEBOUNCE_SECONDS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(5);
Some(Self {
config_file_path,
debounce_duration: Duration::from_secs(debounce_secs),
enabled: true,
})
}

pub

fn start_config_watcher(
config: ReloadableConfig,
watcher_config: ConfigWatcherConfig,
) -> Option<tokio::task::JoinHandle<()>>

Starts the configuration file watcher as a background task.

This function spawns a tokio task that watches for changes to the specified configuration file and triggers configuration reloads with debouncing.

Parameters:

Name Type Description
config ReloadableConfig The ReloadableConfig instance to reload on changes.
watcher_config ConfigWatcherConfig Configuration for the watcher.

Returns:

A handle to the spawned task, or None if the watcher couldn’t be started.

Source
pub fn start_config_watcher(
config: ReloadableConfig,
watcher_config: ConfigWatcherConfig,
) -> Option<tokio::task::JoinHandle<()>> {
if !watcher_config.enabled {
info!("Config file watcher is disabled");
return None;
}
info!(
"Starting config file watcher for '{}' with {}s debounce",
watcher_config.config_file_path,
watcher_config.debounce_duration.as_secs()
);
let handle = tokio::spawn(async move {
if let Err(e) = run_config_watcher(config, watcher_config).await {
error!("Config file watcher error: {}", e);
}
});
Some(handle)
}