Skip to content

Commit 6a69e1f

Browse files
committed
Keep uptime kuma connection alive between syncs to reduce load on uptime kuma
1 parent baae118 commit 6a69e1f

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

autokuma/src/sync.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Sync {
1818
app_state: Arc<AppState>,
1919
auth_token: Option<String>,
2020
sources: Vec<Box<dyn Source>>,
21+
client: Option<Arc<Client>>,
2122
}
2223

2324
impl Sync {
@@ -27,6 +28,7 @@ impl Sync {
2728
app_state: state.clone(),
2829
auth_token: state.config.kuma.auth_token.clone(),
2930
sources: crate::sources::get_sources(state),
31+
client: None,
3032
})
3133
}
3234

@@ -199,12 +201,21 @@ impl Sync {
199201
})
200202
}
201203

204+
async fn get_connection(&mut self) -> Result<Arc<Client>> {
205+
if self.client.is_none() {
206+
let kuma_config = kuma_client::Config {
207+
auth_token: self.auth_token.clone(),
208+
..self.app_state.config.kuma.clone()
209+
};
210+
let kuma = Client::connect(kuma_config).await?;
211+
self.client = Some(Arc::new(kuma));
212+
}
213+
214+
Ok(self.client.as_ref().unwrap().clone())
215+
}
216+
202217
async fn do_sync(&mut self) -> Result<()> {
203-
let kuma_config = kuma_client::Config {
204-
auth_token: self.auth_token.clone(),
205-
..self.app_state.config.kuma.clone()
206-
};
207-
let kuma = Client::connect(kuma_config).await?;
218+
let kuma = self.get_connection().await?;
208219

209220
crate::migrations::migrate(&self.app_state, &kuma).await?;
210221

kuma-client/src/client.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ impl Worker {
235235
Ok(())
236236
}
237237

238+
async fn on_delete_monitor_from_list(self: &Arc<Self>, monitor_id: i32) -> Result<()> {
239+
self.monitors.lock().await.remove(&monitor_id.to_string());
240+
Ok(())
241+
}
242+
243+
async fn on_update_monitor_into_list(self: &Arc<Self>, monitors: MonitorList) -> Result<()> {
244+
self.monitors.lock().await.extend(monitors);
245+
Ok(())
246+
}
247+
238248
async fn on_event(self: &Arc<Self>, event: Event, payload: Value) -> Result<()> {
239249
match event {
240250
Event::MonitorList => {
@@ -280,6 +290,20 @@ impl Worker {
280290
Event::Info => self.on_info().await?,
281291
Event::AutoLogin => self.on_auto_login().await?,
282292
Event::LoginRequired => self.on_login_required().await?,
293+
Event::UpdateMonitorIntoList => {
294+
self.on_update_monitor_into_list(
295+
serde_json::from_value(payload)
296+
.log_error(module_path!(), |_| {
297+
"Failed to deserialize UpdateMonitorIntoList"
298+
})
299+
.unwrap(),
300+
)
301+
.await?
302+
}
303+
Event::DeleteMonitorFromList => {
304+
self.on_delete_monitor_from_list(payload.as_i64().unwrap().try_into().unwrap())
305+
.await?
306+
}
283307
_ => {}
284308
}
285309

kuma-client/src/models/event.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ pub(crate) enum Event {
2222
StatusPageList,
2323
Uptime,
2424
LoginRequired,
25+
UpdateMonitorIntoList,
26+
DeleteMonitorFromList,
2527
}

0 commit comments

Comments
 (0)