use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, Deserialize, Serialize, confers::Config)]
#[config(env_prefix = "CRAWLRS__RUNTIME__")]
pub struct RuntimeConfig {
pub worker: WorkerConfig,
pub background_tasks: BackgroundTaskConfig,
}
#[derive(Debug, Clone, Deserialize, Serialize, confers::Config)]
#[config(env_prefix = "CRAWLRS__RUNTIME__WORKER__")]
pub struct WorkerConfig {
#[config(default = 5)]
pub initial_count: usize,
#[config(default = 20)]
pub max_count: usize,
#[config(default = 100)]
pub queue_size: usize,
}
#[derive(Debug, Clone, Deserialize, Serialize, confers::Config)]
#[config(env_prefix = "CRAWLRS__RUNTIME__BACKGROUND_TASKS__")]
pub struct BackgroundTaskConfig {
#[config(default = 5)]
pub webhook_interval_secs: u64,
#[config(default = 30)]
pub backlog_interval_secs: u64,
#[config(default = 60)]
pub cache_cleanup_interval_secs: u64,
#[config(default = 300)]
pub dns_cache_ttl_secs: u64,
}
impl BackgroundTaskConfig {
pub fn webhook_interval(&self) -> Duration {
Duration::from_secs(self.webhook_interval_secs)
}
pub fn backlog_interval(&self) -> Duration {
Duration::from_secs(self.backlog_interval_secs)
}
pub fn cache_cleanup_interval(&self) -> Duration {
Duration::from_secs(self.cache_cleanup_interval_secs)
}
pub fn dns_cache_ttl(&self) -> Duration {
Duration::from_secs(self.dns_cache_ttl_secs)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_runtime_config_defaults() {
let config = RuntimeConfig::default();
assert_eq!(config.worker.initial_count, 5);
assert_eq!(config.background_tasks.webhook_interval_secs, 5);
}
#[test]
fn test_background_task_intervals() {
let config = BackgroundTaskConfig::default();
assert_eq!(config.webhook_interval(), Duration::from_secs(5));
assert_eq!(config.backlog_interval(), Duration::from_secs(30));
assert_eq!(config.cache_cleanup_interval(), Duration::from_secs(60));
assert_eq!(config.dns_cache_ttl(), Duration::from_secs(300));
}
#[test]
fn test_worker_config_defaults() {
let config = WorkerConfig::default();
assert_eq!(config.initial_count, 5);
assert_eq!(config.max_count, 20);
assert_eq!(config.queue_size, 100);
}
#[test]
fn test_runtime_config_worker_fields() {
let config = RuntimeConfig::default();
assert_eq!(config.worker.max_count, 20);
assert_eq!(config.worker.queue_size, 100);
assert_eq!(config.background_tasks.cache_cleanup_interval_secs, 60);
assert_eq!(config.background_tasks.dns_cache_ttl_secs, 300);
}
}