slightly cleanup update check

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-06-03 00:52:33 -04:00
parent 84290bd668
commit ff7dfec74c
2 changed files with 14 additions and 16 deletions

View file

@ -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<JoinHandle<()>> {
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::<CheckForUpdatesResponse>(&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::<CheckForUpdatesResponse>(&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 {

View file

@ -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
{