feat: implement background old session cleanup task

This commit is contained in:
Jef Roosens 2025-03-15 21:21:35 +01:00
parent f00d842bad
commit bc80515474
No known key found for this signature in database
GPG key ID: 21FD3D77D56BAF49
5 changed files with 65 additions and 1 deletions

View file

@ -1,3 +1,5 @@
use std::time::Duration;
use crate::{db, server};
pub fn serve(config: &crate::config::Config) -> u8 {
@ -11,7 +13,7 @@ pub fn serve(config: &crate::config::Config) -> u8 {
let ctx = server::Context {
store: crate::gpodder::GpodderRepository::new(repo),
};
let app = server::app(ctx);
let app = server::app(ctx.clone());
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
@ -21,7 +23,28 @@ pub fn serve(config: &crate::config::Config) -> u8 {
let address = format!("{}:{}", config.domain, config.port);
tracing::info!("Starting server on {address}");
let session_removal_duration = Duration::from_secs(config.session_cleanup_interval);
rt.block_on(async {
tokio::task::spawn(async move {
let mut interval = tokio::time::interval(session_removal_duration);
loop {
interval.tick().await;
tracing::info!("Performing session cleanup");
match ctx.store.remove_old_sessions() {
Ok(n) => {
tracing::info!("Removed {} old sessions", n);
}
Err(err) => {
tracing::error!("Error occured during session cleanup: {}", err);
}
}
}
});
let listener = tokio::net::TcpListener::bind(address).await.unwrap();
axum::serve(listener, app.into_make_service())
.await