db: enable WAL and index the pending-task lease query

The lease/earliest_run_after queries full-scanned tasks, and the
rollback journal blocked readers behind worker writes. journal_mode=WAL
(persistent, idempotent) plus idx_tasks_pending(status, run_after)
covers both without a schema migration.
This commit is contained in:
2026-08-08 20:26:07 +08:00
parent 44cba8abe0
commit ebc0122264
2 changed files with 8 additions and 2 deletions
+4
View File
@@ -16,6 +16,10 @@ use std::time::Duration;
pub fn open_db(path: &str) -> rusqlite::Result<Connection> { pub fn open_db(path: &str) -> rusqlite::Result<Connection> {
let conn = Connection::open(path)?; let conn = Connection::open(path)?;
conn.busy_timeout(Duration::from_secs(5))?; conn.busy_timeout(Duration::from_secs(5))?;
// WAL lets readers run alongside writer leases instead of blocking on
// the rollback journal; the mode persists in the DB header, so the
// idempotent pragma here and in ensure_schema only needs to win once.
conn.pragma_update(None, "journal_mode", "WAL")?;
Ok(conn) Ok(conn)
} }
+4 -2
View File
@@ -83,9 +83,11 @@ fn scaled_retry_delay(base: f64, attempts: i32) -> f64 {
fn ensure_schema(conn: &rusqlite::Connection) -> rusqlite::Result<()> { fn ensure_schema(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
conn.execute_batch( conn.execute_batch(
"CREATE TABLE IF NOT EXISTS tasks (id TEXT PRIMARY KEY, payload TEXT NOT NULL, \ "PRAGMA journal_mode=WAL; \
CREATE TABLE IF NOT EXISTS tasks (id TEXT PRIMARY KEY, payload TEXT NOT NULL, \
run_after REAL NOT NULL, attempts INTEGER NOT NULL, status TEXT NOT NULL, \ run_after REAL NOT NULL, attempts INTEGER NOT NULL, status TEXT NOT NULL, \
locked_until REAL NOT NULL, created_at REAL NOT NULL);", locked_until REAL NOT NULL, created_at REAL NOT NULL); \
CREATE INDEX IF NOT EXISTS idx_tasks_pending ON tasks(status, run_after);",
) )
} }