| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
use std::sync::Arc; |
| 48 |
|
| 49 |
use tauri::Manager; |
| 50 |
use tokio::time::{Duration, interval}; |
| 51 |
use tokio_util::sync::CancellationToken; |
| 52 |
use tracing::{debug, error, info}; |
| 53 |
|
| 54 |
use crate::commands::SendEmailInput; |
| 55 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const CHECK_INTERVAL_SECS: u64 = 60; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
const MAX_BACKOFF_TICKS: u32 = 32; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#[must_use] |
| 78 |
pub fn due_on_tick(attempts: i32, tick: u64) -> bool { |
| 79 |
if attempts <= 0 { |
| 80 |
return true; |
| 81 |
} |
| 82 |
let every = u64::from( |
| 83 |
2u32.saturating_pow(u32::try_from(attempts).unwrap_or(u32::MAX).min(16)) |
| 84 |
.min(MAX_BACKOFF_TICKS), |
| 85 |
); |
| 86 |
tick.is_multiple_of(every) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
pub async fn start_outbox_drainer(app: tauri::AppHandle, cancel: CancellationToken) { |
| 94 |
let mut check_interval = interval(Duration::from_secs(CHECK_INTERVAL_SECS)); |
| 95 |
let mut tick: u64 = 0; |
| 96 |
|
| 97 |
info!("Outbox drainer started (checking every {CHECK_INTERVAL_SECS} seconds)"); |
| 98 |
|
| 99 |
loop { |
| 100 |
tokio::select! { |
| 101 |
() = cancel.cancelled() => { |
| 102 |
info!("Outbox drainer shutting down"); |
| 103 |
break; |
| 104 |
} |
| 105 |
_ = check_interval.tick() => {} |
| 106 |
} |
| 107 |
tick = tick.wrapping_add(1); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
let Some(state) = app.try_state::<Arc<AppState>>() else { |
| 112 |
debug!("Outbox drainer: state not ready yet"); |
| 113 |
continue; |
| 114 |
}; |
| 115 |
let state: Arc<AppState> = state.inner().clone(); |
| 116 |
|
| 117 |
drain_once(&state, tick).await; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
pub async fn drain_once(state: &Arc<AppState>, tick: u64) { |
| 126 |
let now = chrono::Utc::now(); |
| 127 |
let due = match state.emails.list_due(DESKTOP_USER_ID, now) { |
| 128 |
Ok(due) => due, |
| 129 |
Err(error) => { |
| 130 |
error!("Outbox drainer: could not read the outbox: {error}"); |
| 131 |
return; |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
for email in due { |
| 136 |
if !due_on_tick(email.send_attempts, tick) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
let Some(account_id) = email.draft_account_id else { |
| 145 |
if let Err(error) = state.emails.record_send_failure( |
| 146 |
email.id, |
| 147 |
DESKTOP_USER_ID, |
| 148 |
"No account chosen, so there is nothing to send it from.", |
| 149 |
) { |
| 150 |
error!("Outbox drainer: could not record the failure: {error}"); |
| 151 |
} |
| 152 |
continue; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
let attachments = match state.attachments.list_for_email(email.id, DESKTOP_USER_ID) { |
| 163 |
Ok(files) => files, |
| 164 |
Err(error) => { |
| 165 |
error!( |
| 166 |
"Outbox drainer: could not read {}'s files: {error}", |
| 167 |
email.id |
| 168 |
); |
| 169 |
continue; |
| 170 |
} |
| 171 |
}; |
| 172 |
let attachment_paths = attachments |
| 173 |
.iter() |
| 174 |
.map(|file| { |
| 175 |
crate::commands::attachment::blob_path(&state.data_dir, &file.blob_hash) |
| 176 |
.to_string_lossy() |
| 177 |
.into_owned() |
| 178 |
}) |
| 179 |
.collect(); |
| 180 |
|
| 181 |
let input = SendEmailInput { |
| 182 |
account_id, |
| 183 |
to_address: email.to.clone(), |
| 184 |
cc_address: email.cc_address.clone(), |
| 185 |
bcc_address: email.bcc_address.clone(), |
| 186 |
subject: email.subject.clone(), |
| 187 |
body: email.body.clone(), |
| 188 |
project_id: email.project_id, |
| 189 |
in_reply_to: email.in_reply_to.clone(), |
| 190 |
references: None, |
| 191 |
thread_id: email.thread_id.clone(), |
| 192 |
attachment_paths, |
| 193 |
}; |
| 194 |
|
| 195 |
match crate::commands::send_email_inner(state, input).await { |
| 196 |
Ok(_) => { |
| 197 |
|
| 198 |
|
| 199 |
if let Err(error) = state.emails.delete(email.id, DESKTOP_USER_ID) { |
| 200 |
error!( |
| 201 |
"Outbox drainer: sent {} but could not clear it: {error}", |
| 202 |
email.id |
| 203 |
); |
| 204 |
} else { |
| 205 |
info!("Outbox drainer: sent {}", email.id); |
| 206 |
} |
| 207 |
} |
| 208 |
Err(error) => { |
| 209 |
let said = error.to_string(); |
| 210 |
debug!("Outbox drainer: {} did not go: {said}", email.id); |
| 211 |
if let Err(error) = |
| 212 |
state |
| 213 |
.emails |
| 214 |
.record_send_failure(email.id, DESKTOP_USER_ID, &said) |
| 215 |
{ |
| 216 |
error!("Outbox drainer: could not record the failure: {error}"); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
#[cfg(test)] |
| 224 |
mod tests { |
| 225 |
use super::*; |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn a_message_that_has_never_failed_goes_on_the_next_tick() { |
| 229 |
assert!(due_on_tick(0, 1)); |
| 230 |
assert!(due_on_tick(0, 7)); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn a_failed_message_backs_off_and_the_backoff_is_capped() { |
| 235 |
|
| 236 |
assert!(due_on_tick(1, 2)); |
| 237 |
assert!(!due_on_tick(1, 3)); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
let every_at_cap: Vec<u64> = (1..=64).filter(|t| due_on_tick(20, *t)).collect(); |
| 242 |
assert_eq!(every_at_cap, vec![32, 64]); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn a_wild_attempt_count_cannot_panic_the_drainer() { |
| 247 |
|
| 248 |
assert!(due_on_tick(i32::MAX, 32)); |
| 249 |
assert!(due_on_tick(-1, 1), "negative reads as never failed"); |
| 250 |
} |
| 251 |
} |
| 252 |
|