diff --git a/lib/libpk/src/db/repository/avatars.rs b/lib/libpk/src/db/repository/avatars.rs index 6732ac67..1ff10cc7 100644 --- a/lib/libpk/src/db/repository/avatars.rs +++ b/lib/libpk/src/db/repository/avatars.rs @@ -33,6 +33,23 @@ pub async fn get_by_attachment_id( ) } +pub async fn remove_deletion_queue(pool: &PgPool, attachment_id: u64) -> anyhow::Result<()> { + sqlx::query( + r#" + delete from image_cleanup_jobs + where id in ( + select id from images + where original_attachment_id = $1 + ) + "#, + ) + .bind(attachment_id as i64) + .execute(pool) + .await?; + + Ok(()) +} + pub async fn pop_queue( pool: &PgPool, ) -> anyhow::Result, ImageQueueEntry)>> { diff --git a/services/avatars/src/cleanup.rs b/services/avatars/src/cleanup.rs index 428d034e..0fb815e3 100644 --- a/services/avatars/src/cleanup.rs +++ b/services/avatars/src/cleanup.rs @@ -54,13 +54,14 @@ struct CleanupJobEntry { async fn cleanup_job(pool: sqlx::PgPool, bucket: Arc) -> anyhow::Result<()> { let mut tx = pool.begin().await?; - let image_id: Option = - sqlx::query_as(r#" + let image_id: Option = sqlx::query_as( + r#" select id from image_cleanup_jobs where ts < now() - interval '1 day' - for update skip locked limit 1;"#) - .fetch_optional(&mut *tx) - .await?; + for update skip locked limit 1;"#, + ) + .fetch_optional(&mut *tx) + .await?; if image_id.is_none() { info!("no job to run, sleeping for 1 minute"); tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; diff --git a/services/avatars/src/main.rs b/services/avatars/src/main.rs index 6a294f2d..dca35f77 100644 --- a/services/avatars/src/main.rs +++ b/services/avatars/src/main.rs @@ -89,6 +89,8 @@ async fn pull( if !req.force { if let Some(existing) = db::get_by_attachment_id(&state.pool, parsed.attachment_id).await? { + // remove any pending image cleanup + db::remove_deletion_queue(&state.pool, parsed.attachment_id).await?; return Ok(Json(PullResponse { url: existing.url, new: false,