Skip to content

Commit 287b56d

Browse files
committed
move spawn task into RuntimeConfigManager
1 parent 225f564 commit 287b56d

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ async fn main() -> Result<(), Error> {
5353
let config = Arc::new(Config::from_args(&args)?);
5454
let runtime_config =
5555
Arc::new(RuntimeConfigManager::new(config.runtime_config_path.clone()).await);
56+
let rt_config = runtime_config.read().await;
57+
println!("rt_config: {:?}", rt_config);
5658

5759
println!("taskbroker starting");
5860
println!("version: {}", get_version().trim());
@@ -102,7 +104,6 @@ async fn main() -> Result<(), Error> {
102104
_ = timer.tick() => {
103105
let _ = maintenance_store.vacuum_db().await;
104106
info!("ran maintenance vacuum");
105-
let _ = runtime_config.reload_config().await;
106107
},
107108
_ = guard.wait() => {
108109
break;

src/runtime_config.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use serde::Deserialize;
2+
use std::sync::Arc;
3+
use std::time::Duration;
24
use tokio::fs;
35
use tokio::sync::RwLock;
6+
use tokio::task::JoinHandle;
47
use tracing::{error, info};
58

69
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
@@ -10,22 +13,29 @@ pub struct RuntimeConfig {
1013
}
1114

1215
pub struct RuntimeConfigManager {
13-
pub config: RwLock<RuntimeConfig>,
14-
pub path: String,
16+
pub config: Arc<RwLock<RuntimeConfig>>,
17+
pub handler: JoinHandle<()>,
1518
}
1619

1720
impl RuntimeConfigManager {
1821
pub async fn new(path: String) -> Self {
19-
let runtime_config = Self::read_yaml_file(&path).await;
22+
let runtime_config = Arc::new(RwLock::new(Default::default()));
23+
let _ = Self::reload_config(&path, &runtime_config).await;
2024
Self {
21-
config: RwLock::new(runtime_config),
22-
path,
25+
config: runtime_config.clone(),
26+
handler: tokio::spawn(async move {
27+
let mut interval = tokio::time::interval(Duration::from_secs(60));
28+
loop {
29+
Self::reload_config(&path, &runtime_config).await;
30+
interval.tick().await;
31+
}
32+
}),
2333
}
2434
}
2535

26-
async fn read_yaml_file(path: &str) -> RuntimeConfig {
36+
pub async fn reload_config(path: &str, config: &Arc<RwLock<RuntimeConfig>>) {
2737
let contents = fs::read_to_string(path).await;
28-
match contents {
38+
let new_config: RuntimeConfig = match contents {
2939
Ok(contents) => {
3040
let runtime_config = serde_yaml::from_str::<RuntimeConfig>(&contents);
3141
match runtime_config {
@@ -43,17 +53,10 @@ impl RuntimeConfigManager {
4353
);
4454
RuntimeConfig::default()
4555
}
46-
}
47-
}
48-
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)
56+
};
57+
if new_config != *config.read().await {
58+
*config.write().await = new_config;
59+
info!("Reloaded new runtime config from {}", path);
5760
}
5861
}
5962

@@ -82,21 +85,6 @@ drop_task_killswitch:
8285
assert_eq!(config.drop_task_killswitch.len(), 1);
8386
assert_eq!(config.drop_task_killswitch[0], "test:do_nothing");
8487

85-
std::fs::write(
86-
test_path,
87-
r#"
88-
drop_task_killswitch:
89-
- test:do_nothing
90-
- test:also_do_nothing"#,
91-
)
92-
.unwrap();
93-
94-
let _ = runtime_config.reload_config().await;
95-
let config = runtime_config.read().await;
96-
assert_eq!(config.drop_task_killswitch.len(), 2);
97-
assert_eq!(config.drop_task_killswitch[0], "test:do_nothing");
98-
assert_eq!(config.drop_task_killswitch[1], "test:also_do_nothing");
99-
10088
fs::remove_file(test_path).await.unwrap();
10189
}
10290
}

0 commit comments

Comments
 (0)