| 1 |
|
| 2 |
|
| 3 |
use std::collections::HashSet; |
| 4 |
use std::sync::Arc; |
| 5 |
use std::time::Duration; |
| 6 |
|
| 7 |
use sqlx::PgPool; |
| 8 |
|
| 9 |
use crate::storage::S3Storage; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
const PURGE_BATCH: i64 = 500; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
pub async fn continuously_purge_removed_images(db: PgPool, s3: Arc<S3Storage>, interval: Duration) { |
| 26 |
loop { |
| 27 |
match purge_removed_image_objects(&db, &s3).await { |
| 28 |
Ok(0) => {} |
| 29 |
Ok(n) => tracing::info!(purged = n, "reconcile: purged orphaned image objects"), |
| 30 |
Err(e) => tracing::error!(error = %e, "reconcile: image purge sweep failed"), |
| 31 |
} |
| 32 |
tokio::time::sleep(interval).await; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
async fn purge_removed_image_objects(db: &PgPool, s3: &S3Storage) -> Result<usize, String> { |
| 41 |
let mut purged = 0usize; |
| 42 |
loop { |
| 43 |
let pending = mt_db::queries::list_images_pending_s3_purge(db, PURGE_BATCH) |
| 44 |
.await |
| 45 |
.map_err(|e| format!("db error listing images to purge: {e}"))?; |
| 46 |
if pending.is_empty() { |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
let keys: Vec<String> = pending.iter().map(|p| p.s3_key.clone()).collect(); |
| 51 |
let failures = s3.delete_objects(&keys).await?; |
| 52 |
let failed: HashSet<&str> = failures.iter().map(|(k, _)| k.as_str()).collect(); |
| 53 |
for (key, msg) in &failures { |
| 54 |
tracing::warn!(s3_key = %key, error = %msg, "reconcile: failed to delete image object"); |
| 55 |
} |
| 56 |
|
| 57 |
let purged_ids: Vec<uuid::Uuid> = pending |
| 58 |
.iter() |
| 59 |
.filter(|p| !failed.contains(p.s3_key.as_str())) |
| 60 |
.map(|p| p.id) |
| 61 |
.collect(); |
| 62 |
|
| 63 |
if purged_ids.is_empty() { |
| 64 |
|
| 65 |
break; |
| 66 |
} |
| 67 |
|
| 68 |
purged += purged_ids.len(); |
| 69 |
mt_db::mutations::mark_images_s3_purged(db, &purged_ids) |
| 70 |
.await |
| 71 |
.map_err(|e| format!("db error marking images purged: {e}"))?; |
| 72 |
|
| 73 |
if pending.len() < PURGE_BATCH as usize { |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
Ok(purged) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub const CHAT_SWEEP_INTERVAL: Duration = Duration::from_mins(15); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pub async fn continuously_sweep_chat(db: PgPool, interval: Duration) { |
| 105 |
loop { |
| 106 |
sweep_chat_once(&db).await; |
| 107 |
tokio::time::sleep(interval).await; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
pub async fn sweep_chat_once(db: &PgPool) -> (u64, u64) { |
| 118 |
let expired = match mt_db::mutations::sweep_expired_chat_messages(db).await { |
| 119 |
Ok(n) => { |
| 120 |
if n > 0 { |
| 121 |
tracing::info!(expired = n, "chat: swept expired messages"); |
| 122 |
} |
| 123 |
n |
| 124 |
} |
| 125 |
Err(e) => { |
| 126 |
tracing::error!(error = %e, "chat: expiry sweep failed"); |
| 127 |
0 |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
let trimmed = match mt_db::mutations::trim_chat_rooms_to_cap(db).await { |
| 134 |
Ok(n) => { |
| 135 |
if n > 0 { |
| 136 |
tracing::info!(trimmed = n, "chat: trimmed rooms to their message cap"); |
| 137 |
} |
| 138 |
n |
| 139 |
} |
| 140 |
Err(e) => { |
| 141 |
tracing::error!(error = %e, "chat: room cap trim failed"); |
| 142 |
0 |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
(expired, trimmed) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
pub async fn continuously_sweep_chat_rate_limits(chat: Arc<livechat::Chat>) { |
| 161 |
let interval = chat.sweep_interval(); |
| 162 |
loop { |
| 163 |
tokio::time::sleep(interval).await; |
| 164 |
let dropped = chat.sweep(std::time::Instant::now()); |
| 165 |
if dropped > 0 { |
| 166 |
tracing::debug!(dropped, "chat: evicted idle rate-limit buckets"); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|