feat: implement background old session cleanup task
This commit is contained in:
parent
f00d842bad
commit
bc80515474
5 changed files with 65 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue