Anchor: each run ends its transaction itself
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 1m18s
CI / release (push) Skipped

A run that was not due dropped its transaction, and sqlx rolls a dropped
one back only when the connection is returned, from a background task.
Until then the advisory lock stays taken, so a run right after it (the
test's next call on a busy CI runner; on the server a tick that comes at
the wrong moment) found the lock taken and skipped. Every way out of
run_daily now rolls back explicitly (sending happens on the pool; the
transaction only holds the lock). The test checks from a connection of
its own that no lock is left after a run that sent nothing.
This commit is contained in:
grabowski committed 2026-11-23 00:31:51 +07:00
1 parent 4dba0475c7
commit 62a37b677a
2 files changed
+22 -4

No files matched your search

+9 -4
View File
@@ -319,12 +319,16 @@ pub async fn run_daily(cfg: &Config, db: &PgPool, now: DateTime<Utc>) -> Result<
if cfg.mail.is_none() {
return Ok(None);
}
// one server sends: the lock keeps two runs from both sending
// one server sends: the lock keeps two runs from both sending. Every
// way out ends the transaction itself: a dropped one is only rolled
// back when its connection is used again, and until then it keeps the
// lock, so the next run would find it taken and skip the day.
let mut tx = db.begin().await?;
let locked: bool = sqlx::query_scalar("SELECT pg_try_advisory_xact_lock(7464530001)")
.fetch_one(&mut *tx)
.await?;
if !locked {
tx.rollback().await?;
return Ok(None);
}
let today = now.with_timezone(&dates::bangkok()).date_naive();
@@ -334,11 +338,12 @@ pub async fn run_daily(cfg: &Config, db: &PgPool, now: DateTime<Utc>) -> Result<
.fetch_all(db)
.await?;
if !due(now, &rows) {
tx.rollback().await?;
return Ok(None);
}
let out = send(cfg, db, true, "daily anchor", now).await?;
tx.commit().await?;
Ok(Some(out))
let out = send(cfg, db, true, "daily anchor", now).await;
tx.rollback().await?;
Ok(Some(out?))
}
/// Checks every ten minutes whether the day's anchor is due.