diff --git a/src/service/globals/updates.rs b/src/service/globals/updates.rs index 9413c54a..c6ac9fff 100644 --- a/src/service/globals/updates.rs +++ b/src/service/globals/updates.rs @@ -3,13 +3,16 @@ use std::time::Duration; use ruma::events::room::message::RoomMessageEventContent; use serde::Deserialize; use tokio::{task::JoinHandle, time::interval}; -use tracing::{debug, error, warn}; +use tracing::{error, warn}; use crate::{ conduit::{Error, Result}, services, }; +const CHECK_FOR_UPDATES_URL: &str = "https://pupbrain.dev/check-for-updates/stable"; +const CHECK_FOR_UPDATES_INTERVAL: u64 = 7200; // 2 hours + #[derive(Deserialize)] struct CheckForUpdatesResponseEntry { id: u64, @@ -22,39 +25,34 @@ struct CheckForUpdatesResponse { } #[tracing::instrument] -pub async fn start_check_for_updates_task() -> Result> { - let timer_interval = Duration::from_secs(7200); // 2 hours +pub fn start_check_for_updates_task() -> JoinHandle<()> { + let timer_interval = Duration::from_secs(CHECK_FOR_UPDATES_INTERVAL); - Ok(services().server.runtime().spawn(async move { + services().server.runtime().spawn(async move { let mut i = interval(timer_interval); loop { - tokio::select! { - _ = i.tick() => { - debug!(target: "start_check_for_updates_task", "Timer ticked"); - }, - } + i.tick().await; if let Err(e) = try_handle_updates().await { warn!(%e, "Failed to check for updates"); } } - })) + }) } +#[tracing::instrument(skip_all)] async fn try_handle_updates() -> Result<()> { let response = services() .globals .client .default - .get("https://pupbrain.dev/check-for-updates/stable") + .get(CHECK_FOR_UPDATES_URL) .send() .await?; - let response = serde_json::from_str::(&response.text().await?).map_err(|e| { - error!("Bad check for updates response: {e}"); - Error::BadServerResponse("Bad version check response") - })?; + let response = serde_json::from_str::(&response.text().await?) + .map_err(|e| Error::Err(format!("Bad check for updates response: {e}")))?; let mut last_update_id = services().globals.last_check_for_updates_id()?; for update in response.updates { diff --git a/src/service/services.rs b/src/service/services.rs index a9356562..05894753 100644 --- a/src/service/services.rs +++ b/src/service/services.rs @@ -289,7 +289,7 @@ bad_signature_ratelimiter: {bad_signature_ratelimiter} } if self.globals.allow_check_for_updates() { - let handle = globals::updates::start_check_for_updates_task().await?; + let handle = globals::updates::start_check_for_updates_task(); #[allow(clippy::let_underscore_must_use)] // needed for shutdown {