diff --git a/crates/iz-core/src/mail.rs b/crates/iz-core/src/mail.rs index 2e6788d..786bbde 100644 --- a/crates/iz-core/src/mail.rs +++ b/crates/iz-core/src/mail.rs @@ -30,9 +30,8 @@ use time::{Duration, OffsetDateTime}; use crate::board::Transition; use crate::detail::ActivityKind; use crate::store::{ - ActivityEvent, Audience, ClaimedSend, Event, Freeing, MailOutcome, MailRule, MailSend, SendKind, - Store, - Trigger, + ActivityEvent, Audience, ClaimedSend, Event, Freeing, MailOutcome, MailRule, MailSend, + SendKind, Store, Trigger, }; /// Why the mail server would not take it, and whether that can change. @@ -344,8 +343,7 @@ impl Engine { self.owe(rule, &event, task_id, &mut report).await?; } Trigger::StatusBecomes(watched) => { - let detail = - format!("unblocked:{}", watched.as_deref().unwrap_or("any")); + let detail = format!("unblocked:{}", watched.as_deref().unwrap_or("any")); self.store .record_mail_decision( &rule.id, @@ -739,9 +737,9 @@ impl Engine { let Some(mail) = mail else { return Ok(()); }; - let Some(send) = sends.first() else { + if sends.is_empty() { return Ok(()); - }; + } let now = OffsetDateTime::now_utc(); match self.mailer.send(&mail).await { Ok(()) => { @@ -768,17 +766,24 @@ impl Engine { report.held += 1; } Err(problem) => { - let attempts = send.attempts + 1; - // The last attempt is written down as given up on rather than - // left looking like it is still coming. - let retry_at = - (problem.retryable && attempts < MAX_ATTEMPTS).then(|| now + backoff(attempts)); + // The retry ladder is each row's own: a batch is one envelope, + // but the rows in it arrived at different times and may be at + // different rungs. Deciding from the first row would write a + // fresh row off on its first failure, or keep retrying a row + // whose five attempts are spent. + let mut any_retry = false; for one in sends { + let attempts = one.attempts + 1; + // The last attempt is written down as given up on rather + // than left looking like it is still coming. + let retry_at = (problem.retryable && attempts < MAX_ATTEMPTS) + .then(|| now + backoff(attempts)); + any_retry |= retry_at.is_some(); self.store .record_send_refused(&one.id, &problem.message, retry_at, now) .await?; } - if retry_at.is_some() { + if any_retry { report.failed += 1; } else { report.abandoned += 1; diff --git a/crates/iz-core/tests/store.rs b/crates/iz-core/tests/store.rs index f84b97d..17bf65f 100644 --- a/crates/iz-core/tests/store.rs +++ b/crates/iz-core/tests/store.rs @@ -3461,6 +3461,75 @@ async fn a_failing_send_is_retried_a_bounded_number_of_times_and_then_written_of let _ = std::fs::remove_dir_all(&dir); } +#[tokio::test] +async fn a_batch_mates_retry_clock_is_its_own() { + // Two owed rows, same card and reader, at different rungs of the retry + // ladder, answered by one envelope: the refusal writes the exhausted row + // off and sets the fresh one down to try again — the envelope is shared, + // the clocks are not. + let (dir, store, workspace, admin) = waiting(30).await; + let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await; + let task = add_task(&store, &workspace, "Backlog", "Ship it", None, &admin).await; + store.assign_task(&task, &mate).await.unwrap(); + let rule = a_rule(&store, &workspace, "Done", "Task completed").await; + + let mailer = Remembering::refusing( + (0..iz_core::mail::MAX_ATTEMPTS) + .map(|_| MailError::retryable("connection timed out")) + .collect(), + ); + let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh"); + + // Row A: owed by the first crossing, refused on four delivery passes, one + // rung from written off. + let first = moved_to(&store, &workspace, &task, "Backlog", "Done", &admin).await; + engine.on_transition(&first).await.unwrap(); + let mut when = OffsetDateTime::now_utc() + Duration::minutes(31); + for _ in 1..iz_core::mail::MAX_ATTEMPTS { + engine.deliver_owed(when, 10).await.unwrap(); + when += backoff(iz_core::mail::MAX_ATTEMPTS) + Duration::minutes(1); + } + + // Row B: a second crossing owes the same reader again, fresh on the + // ladder, held by the same quiet window. + moved_to(&store, &workspace, &task, "Done", "Backlog", &admin).await; + let second = moved_to(&store, &workspace, &task, "Backlog", "Done", &admin).await; + engine.on_transition(&second).await.unwrap(); + + // One envelope answers both rows and is refused: A's fifth attempt, B's + // first. + let due = OffsetDateTime::now_utc() + Duration::minutes(31); + let when = when.max(due); + engine.deliver_owed(when, 10).await.unwrap(); + + let rows = store.sends_for_rule(&rule.id, 10).await.unwrap(); + assert_eq!(rows.len(), 2); + let exhausted = rows + .iter() + .find(|row| row.attempts == iz_core::mail::MAX_ATTEMPTS); + let fresh = rows.iter().find(|row| row.attempts == 1); + let Some(exhausted) = exhausted else { + panic!("no row at the top of the ladder: {rows:?}"); + }; + let Some(fresh) = fresh else { + panic!("the fresh row was not marked with its own first attempt: {rows:?}"); + }; + assert_eq!(exhausted.state, SendState::Abandoned); + assert_eq!(fresh.state, SendState::Failed); + assert!( + fresh.next_attempt_at.is_some(), + "the fresh row rides its own retry clock: {fresh:?}" + ); + + // And when the host answers again, the fresh row still goes out. + let later = fresh.next_attempt_at.unwrap() + Duration::minutes(1); + engine.deliver_owed(later, 10).await.unwrap(); + let sent = mailer.sent(); + assert_eq!(sent.len(), 1); + assert_eq!(sent[0].to, "emre@iz.sh"); + let _ = std::fs::remove_dir_all(&dir); +} + #[tokio::test] async fn a_refused_address_is_written_off_at_the_first_answer() { let (dir, store, workspace, admin) = shared().await; diff --git a/crates/iz-web/src/server.rs b/crates/iz-web/src/server.rs index af76580..ab63cd5 100644 --- a/crates/iz-web/src/server.rs +++ b/crates/iz-web/src/server.rs @@ -105,7 +105,6 @@ impl Mail { let ev = store.event(&activity_id).await; let report = match ev { Ok(Some(iz_core::store::Event::Happened(ev))) => { - eprintln!("AFTER_ACTIVITY happened kind={:?}", ev.kind); match engine.on_activity(&ev).await { Ok(_) => { engine @@ -395,8 +394,7 @@ impl Refusal { Refusal::EmptyTitle => "Give the task a title.".to_string(), Refusal::EmptyName => "Give yourself a name.".to_string(), Refusal::BadLimit => { - "A limit has to be at least 1 MB, and no wider than 500 MB per file." - .to_string() + "A limit has to be at least 1 MB, and no wider than 500 MB per file.".to_string() } Refusal::BadPolicy => "A security number is out of range.".to_string(), Refusal::BadZone => "That is not a timezone.".to_string(), @@ -463,8 +461,7 @@ impl Refusal { Refusal::AlreadyMember => "Zaten üye.".to_string(), Refusal::Unavailable => "Bir şeyler ters gitti.".to_string(), Refusal::BadLimit => { - "Limit en az 1 MB, dosya başına en çok 500 MB olabilir." - .to_string() + "Limit en az 1 MB, dosya başına en çok 500 MB olabilir.".to_string() } Refusal::BadPolicy => "Güvenlik sayısı aralığın dışında.".to_string(), Refusal::BadZone => "Bu bir saat dilimi değil.".to_string(),