Skip to content

Commit 77cf848

Browse files
committed
dont panic and log when new config found
1 parent 3ae86a7 commit 77cf848

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

runtime_config_defaults.yaml

-2
This file was deleted.

src/main.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ async fn log_task_completion(name: &str, task: JoinHandle<Result<(), Error>>) {
5151
async fn main() -> Result<(), Error> {
5252
let args = Args::parse();
5353
let config = Arc::new(Config::from_args(&args)?);
54-
let runtime_config = Arc::new(RuntimeConfigManager::new(
55-
config.runtime_config_path.clone(),
56-
));
54+
let runtime_config =
55+
Arc::new(RuntimeConfigManager::new(config.runtime_config_path.clone()).await);
5756

5857
println!("taskbroker starting");
5958
println!("version: {}", get_version().trim());

src/runtime_config.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use serde::Deserialize;
2+
use tokio::fs;
23
use tokio::sync::RwLock;
3-
use tracing::info;
4+
use tracing::{error, info};
45

5-
#[derive(Debug, Deserialize, Clone)]
6+
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
67
pub struct RuntimeConfig {
78
/// A list of tasks to drop before inserting into sqlite.
89
pub drop_task_killswitch: Vec<String>,
@@ -14,25 +15,46 @@ pub struct RuntimeConfigManager {
1415
}
1516

1617
impl RuntimeConfigManager {
17-
pub fn new(path: String) -> Self {
18-
let runtime_config = Self::read_yaml_file(&path);
18+
pub async fn new(path: String) -> Self {
19+
let runtime_config = Self::read_yaml_file(&path).await;
1920
Self {
2021
config: RwLock::new(runtime_config),
2122
path,
2223
}
2324
}
2425

25-
fn read_yaml_file(path: &str) -> RuntimeConfig {
26-
let contents = std::fs::read_to_string(path)
27-
.unwrap_or_else(|_| panic!("Failed to read config file from {}", path));
28-
serde_yaml::from_str(&contents)
29-
.unwrap_or_else(|_| panic!("Failed to parse YAML from {}", path))
26+
async fn read_yaml_file(path: &str) -> RuntimeConfig {
27+
let contents = fs::read_to_string(path).await;
28+
match contents {
29+
Ok(contents) => {
30+
let runtime_config = serde_yaml::from_str::<RuntimeConfig>(&contents);
31+
match runtime_config {
32+
Ok(runtime_config) => runtime_config,
33+
Err(e) => {
34+
error!("Using default runtime configs. Failed to parse file: {}", e);
35+
RuntimeConfig::default()
36+
}
37+
}
38+
}
39+
Err(e) => {
40+
error!(
41+
"Using default runtime configs. Failed to read yaml file: {}",
42+
e
43+
);
44+
RuntimeConfig::default()
45+
}
46+
}
3047
}
3148

32-
pub async fn reload_config(&self) {
33-
let new_config = Self::read_yaml_file(&self.path);
34-
*self.config.write().await = new_config;
35-
info!("Reloaded runtime config from {}", self.path);
49+
pub async fn reload_config(&self) -> Result<bool, anyhow::Error> {
50+
let new_config = Self::read_yaml_file(&self.path).await;
51+
if new_config != *self.config.read().await {
52+
*self.config.write().await = new_config;
53+
info!("Reloaded new runtime config from {}", self.path);
54+
Ok(true)
55+
} else {
56+
Ok(false)
57+
}
3658
}
3759

3860
pub async fn read(&self) -> RuntimeConfig {
@@ -54,7 +76,7 @@ drop_task_killswitch:
5476
let test_path = "runtime_test_config.yaml";
5577
std::fs::write(test_path, test_yaml).unwrap();
5678

57-
let runtime_config = RuntimeConfigManager::new(test_path.to_string());
79+
let runtime_config = RuntimeConfigManager::new(test_path.to_string()).await;
5880
let config = runtime_config.read().await;
5981
assert_eq!(config.drop_task_killswitch.len(), 1);
6082
assert_eq!(config.drop_task_killswitch[0], "test:do_nothing");
@@ -68,7 +90,7 @@ drop_task_killswitch:
6890
)
6991
.unwrap();
7092

71-
runtime_config.reload_config().await;
93+
let _ = runtime_config.reload_config().await;
7294
let config = runtime_config.read().await;
7395
assert_eq!(config.drop_task_killswitch.len(), 2);
7496
assert_eq!(config.drop_task_killswitch[0], "test:do_nothing");

0 commit comments

Comments
 (0)