| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
use uuid::Uuid; |
| 5 |
use crate::error::Result; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Debug, sqlx::FromRow)] |
| 9 |
pub struct PendingS3Deletion { |
| 10 |
pub id: Uuid, |
| 11 |
pub s3_key: String, |
| 12 |
pub bucket: String, |
| 13 |
pub source: String, |
| 14 |
pub attempts: i32, |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
#[tracing::instrument(skip_all)] |
| 19 |
pub async fn enqueue_deletions( |
| 20 |
pool: &PgPool, |
| 21 |
keys: &[(String, String)], |
| 22 |
source: &str, |
| 23 |
) -> Result<()> { |
| 24 |
if keys.is_empty() { |
| 25 |
return Ok(()); |
| 26 |
} |
| 27 |
let s3_keys: Vec<&str> = keys.iter().map(|(k, _)| k.as_str()).collect(); |
| 28 |
let buckets: Vec<&str> = keys.iter().map(|(_, b)| b.as_str()).collect(); |
| 29 |
sqlx::query( |
| 30 |
"INSERT INTO pending_s3_deletions (s3_key, bucket, source) SELECT * FROM unnest($1::text[], $2::text[], $3::text[]) ON CONFLICT DO NOTHING", |
| 31 |
) |
| 32 |
.bind(&s3_keys) |
| 33 |
.bind(&buckets) |
| 34 |
.bind(vec![source; keys.len()]) |
| 35 |
.execute(pool) |
| 36 |
.await?; |
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
#[tracing::instrument(skip_all)] |
| 42 |
pub async fn remove_completed(pool: &PgPool, ids: &[Uuid]) -> Result<()> { |
| 43 |
if ids.is_empty() { |
| 44 |
return Ok(()); |
| 45 |
} |
| 46 |
sqlx::query("DELETE FROM pending_s3_deletions WHERE id = ANY($1)") |
| 47 |
.bind(ids) |
| 48 |
.execute(pool) |
| 49 |
.await?; |
| 50 |
Ok(()) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#[tracing::instrument(skip_all)] |
| 63 |
pub async fn move_to_dead_letter(pool: &PgPool, ids: &[Uuid]) -> Result<u64> { |
| 64 |
if ids.is_empty() { |
| 65 |
return Ok(0); |
| 66 |
} |
| 67 |
let result = sqlx::query( |
| 68 |
r#" |
| 69 |
WITH moved AS ( |
| 70 |
DELETE FROM pending_s3_deletions |
| 71 |
WHERE id = ANY($1) |
| 72 |
RETURNING id, s3_key, bucket, source, created_at, attempts, last_attempted_at |
| 73 |
) |
| 74 |
INSERT INTO pending_s3_deletions_dead_letter |
| 75 |
(id, s3_key, bucket, source, created_at, attempts, last_attempted_at) |
| 76 |
SELECT id, s3_key, bucket, source, created_at, attempts, last_attempted_at |
| 77 |
FROM moved |
| 78 |
ON CONFLICT (id) DO NOTHING |
| 79 |
"#, |
| 80 |
) |
| 81 |
.bind(ids) |
| 82 |
.execute(pool) |
| 83 |
.await?; |
| 84 |
Ok(result.rows_affected()) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
#[derive(Clone, Copy)] |
| 89 |
enum S3KeyMatch { |
| 90 |
|
| 91 |
Exact, |
| 92 |
|
| 93 |
|
| 94 |
UrlSuffix, |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
struct S3KeyRef { |
| 103 |
bucket: &'static str, |
| 104 |
table: &'static str, |
| 105 |
column: &'static str, |
| 106 |
matching: S3KeyMatch, |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
const S3_KEY_REFS: &[S3KeyRef] = &[ |
| 111 |
|
| 112 |
S3KeyRef { bucket: "main", table: "media_files", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 113 |
S3KeyRef { bucket: "main", table: "versions", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 114 |
S3KeyRef { bucket: "main", table: "items", column: "audio_s3_key", matching: S3KeyMatch::Exact }, |
| 115 |
S3KeyRef { bucket: "main", table: "items", column: "cover_s3_key", matching: S3KeyMatch::Exact }, |
| 116 |
S3KeyRef { bucket: "main", table: "items", column: "video_s3_key", matching: S3KeyMatch::Exact }, |
| 117 |
|
| 118 |
|
| 119 |
S3KeyRef { bucket: "main", table: "items", column: "cover_image_url", matching: S3KeyMatch::UrlSuffix }, |
| 120 |
S3KeyRef { bucket: "main", table: "projects", column: "cover_image_url", matching: S3KeyMatch::UrlSuffix }, |
| 121 |
|
| 122 |
S3KeyRef { bucket: "main", table: "item_images", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 123 |
S3KeyRef { bucket: "main", table: "project_images", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 124 |
S3KeyRef { bucket: "main", table: "content_insertions", column: "storage_key", matching: S3KeyMatch::Exact }, |
| 125 |
|
| 126 |
S3KeyRef { bucket: "synckit", table: "sync_blobs", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 127 |
S3KeyRef { bucket: "synckit", table: "ota_artifacts", column: "s3_key", matching: S3KeyMatch::Exact }, |
| 128 |
]; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[tracing::instrument(skip_all)] |
| 146 |
pub async fn is_s3_key_live(pool: &PgPool, bucket: &str, s3_key: &str) -> Result<bool> { |
| 147 |
let refs: Vec<&S3KeyRef> = S3_KEY_REFS.iter().filter(|r| r.bucket == bucket).collect(); |
| 148 |
if refs.is_empty() { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
tracing::warn!(bucket, "is_s3_key_live: no registered tables for bucket; treating key as live"); |
| 153 |
return Ok(true); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
let needs_url_suffix = refs.iter().any(|r| matches!(r.matching, S3KeyMatch::UrlSuffix)); |
| 159 |
let clauses: Vec<String> = refs |
| 160 |
.iter() |
| 161 |
.map(|r| match r.matching { |
| 162 |
S3KeyMatch::Exact => format!("EXISTS(SELECT 1 FROM {} WHERE {} = $1)", r.table, r.column), |
| 163 |
S3KeyMatch::UrlSuffix => { |
| 164 |
format!("EXISTS(SELECT 1 FROM {} WHERE {} LIKE $2 ESCAPE '\\')", r.table, r.column) |
| 165 |
} |
| 166 |
}) |
| 167 |
.collect(); |
| 168 |
let sql = format!("SELECT {}", clauses.join(" OR ")); |
| 169 |
|
| 170 |
let mut query = sqlx::query_scalar::<_, bool>(&sql).bind(s3_key); |
| 171 |
if needs_url_suffix { |
| 172 |
|
| 173 |
|
| 174 |
let escaped = s3_key.replace('\\', "\\\\").replace('%', "\\%").replace('_', "\\_"); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
query = query.bind(format!("%/{escaped}")); |
| 182 |
} |
| 183 |
Ok(query.fetch_one(pool).await?) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#[tracing::instrument(skip_all)] |
| 189 |
pub async fn get_stale_pending( |
| 190 |
pool: &PgPool, |
| 191 |
min_age: chrono::Duration, |
| 192 |
limit: i64, |
| 193 |
) -> Result<Vec<PendingS3Deletion>> { |
| 194 |
let cutoff = chrono::Utc::now() - min_age; |
| 195 |
let rows = sqlx::query_as::<_, PendingS3Deletion>( |
| 196 |
r#" |
| 197 |
UPDATE pending_s3_deletions |
| 198 |
SET attempts = attempts + 1, last_attempted_at = NOW() |
| 199 |
WHERE id IN ( |
| 200 |
SELECT id FROM pending_s3_deletions |
| 201 |
WHERE created_at < $1 |
| 202 |
ORDER BY created_at |
| 203 |
LIMIT $2 |
| 204 |
FOR UPDATE SKIP LOCKED |
| 205 |
) |
| 206 |
RETURNING id, s3_key, bucket, source, attempts |
| 207 |
"#, |
| 208 |
) |
| 209 |
.bind(cutoff) |
| 210 |
.bind(limit) |
| 211 |
.fetch_all(pool) |
| 212 |
.await?; |
| 213 |
Ok(rows) |
| 214 |
} |
| 215 |
|