slightly cleanup update check
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
84290bd668
commit
ff7dfec74c
2 changed files with 14 additions and 16 deletions
|
@ -3,13 +3,16 @@ use std::time::Duration;
|
||||||
use ruma::events::room::message::RoomMessageEventContent;
|
use ruma::events::room::message::RoomMessageEventContent;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::{task::JoinHandle, time::interval};
|
use tokio::{task::JoinHandle, time::interval};
|
||||||
use tracing::{debug, error, warn};
|
use tracing::{error, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
conduit::{Error, Result},
|
conduit::{Error, Result},
|
||||||
services,
|
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)]
|
#[derive(Deserialize)]
|
||||||
struct CheckForUpdatesResponseEntry {
|
struct CheckForUpdatesResponseEntry {
|
||||||
id: u64,
|
id: u64,
|
||||||
|
@ -22,39 +25,34 @@ struct CheckForUpdatesResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
pub async fn start_check_for_updates_task() -> Result<JoinHandle<()>> {
|
pub fn start_check_for_updates_task() -> JoinHandle<()> {
|
||||||
let timer_interval = Duration::from_secs(7200); // 2 hours
|
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);
|
let mut i = interval(timer_interval);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
i.tick().await;
|
||||||
_ = i.tick() => {
|
|
||||||
debug!(target: "start_check_for_updates_task", "Timer ticked");
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = try_handle_updates().await {
|
if let Err(e) = try_handle_updates().await {
|
||||||
warn!(%e, "Failed to check for updates");
|
warn!(%e, "Failed to check for updates");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all)]
|
||||||
async fn try_handle_updates() -> Result<()> {
|
async fn try_handle_updates() -> Result<()> {
|
||||||
let response = services()
|
let response = services()
|
||||||
.globals
|
.globals
|
||||||
.client
|
.client
|
||||||
.default
|
.default
|
||||||
.get("https://pupbrain.dev/check-for-updates/stable")
|
.get(CHECK_FOR_UPDATES_URL)
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response.text().await?).map_err(|e| {
|
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response.text().await?)
|
||||||
error!("Bad check for updates response: {e}");
|
.map_err(|e| Error::Err(format!("Bad check for updates response: {e}")))?;
|
||||||
Error::BadServerResponse("Bad version check response")
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut last_update_id = services().globals.last_check_for_updates_id()?;
|
let mut last_update_id = services().globals.last_check_for_updates_id()?;
|
||||||
for update in response.updates {
|
for update in response.updates {
|
||||||
|
|
|
@ -289,7 +289,7 @@ bad_signature_ratelimiter: {bad_signature_ratelimiter}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.globals.allow_check_for_updates() {
|
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
|
#[allow(clippy::let_underscore_must_use)] // needed for shutdown
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue