Start work on #34
Some checks failed
Testing/Linting / test (analysis) (push) Has been cancelled
Testing/Linting / test (backend) (push) Has been cancelled
Testing/Linting / lint (analysis) (push) Has been cancelled
Testing/Linting / lint (backend) (push) Has been cancelled
build-docker-image / docker (push) Has been cancelled

Started work on the garbage-collection module. This for now just loads the stored demo ids
and checks if they are also found in the database, logging every demo that could not be found
and should at some point delete these demos
This commit is contained in:
Lol3rrr
2024-11-17 20:55:55 +01:00
parent fa21804cc3
commit 48e7c5c5b7
4 changed files with 87 additions and 1 deletions

36
backend/src/gc.rs Normal file
View File

@@ -0,0 +1,36 @@
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
#[tracing::instrument(skip(storage))]
pub async fn run_gc(storage: &mut dyn crate::storage::DemoStorage) -> Result<(), ()> {
let stored_demos = match storage.list_demos().await {
Ok(ds) => ds,
Err(e) => return Err(()),
};
tracing::info!("Found {} demos in storage", stored_demos.len());
let mut db_con = crate::db_connection().await;
let db_res = db_con
.build_transaction()
.run(move |con| {
Box::pin(async move {
for demo in stored_demos {
let query = crate::schema::demos::dsl::demos
.filter(crate::schema::demos::dsl::demo_id.eq(&demo)).select(crate::models::Demo::as_select());
let matching: Vec<crate::models::Demo> = query.load(con).await?;
if matching.is_empty() {
tracing::debug!("Should delete old demo {:?}", demo);
}
}
Ok::<(), diesel::result::Error>(())
})
})
.await;
db_res.map_err(|e| ())
}