huskies: merge 1061

This commit is contained in:
dave
2026-05-14 20:12:51 +00:00
parent 54d9737428
commit 5678f2a556
11 changed files with 752 additions and 82 deletions
+159 -25
View File
@@ -1,27 +1,99 @@
//! I/O side of the timer service: filesystem persistence, clock reads,
//! I/O side of the timer service: SQLite persistence, clock reads,
//! background task spawning, and story-ID resolution.
//!
//! This is the **only** place inside `service/timer/` that may perform side
//! effects (filesystem reads/writes, clock reads, `tokio::spawn`).
use chrono::{DateTime, Utc};
use sqlx::SqlitePool;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use super::persist::{TimerEntry, deserialize_timers, serialize_timers};
use super::schedule::next_occurrence_at;
// ── TimerStore background writer ─────────────────────────────────────────────
enum TimerWriteCmd {
Upsert(TimerEntry),
Delete(String),
}
fn spawn_timer_writer(pool: SqlitePool, mut rx: mpsc::UnboundedReceiver<TimerWriteCmd>) {
tokio::spawn(async move {
while let Some(cmd) = rx.recv().await {
match cmd {
TimerWriteCmd::Upsert(e) => {
let result = sqlx::query(
"INSERT INTO timers (story_id, scheduled_at) VALUES (?1, ?2) \
ON CONFLICT(story_id) DO UPDATE SET scheduled_at = excluded.scheduled_at",
)
.bind(&e.story_id)
.bind(e.scheduled_at.to_rfc3339())
.execute(&pool)
.await;
if let Err(e) = result {
crate::slog!("[timer] DB upsert failed: {e}");
}
}
TimerWriteCmd::Delete(story_id) => {
let result = sqlx::query("DELETE FROM timers WHERE story_id = ?1")
.bind(&story_id)
.execute(&pool)
.await;
if let Err(e) = result {
crate::slog!("[timer] DB delete failed for '{story_id}': {e}");
}
}
}
}
});
}
// ── TimerStore ─────────────────────────────────────────────────────────────
/// Persistent store for pending timers, backed by a JSON file.
enum TimerPersistence {
Sqlite(mpsc::UnboundedSender<TimerWriteCmd>),
Json(PathBuf),
}
/// Persistent store for pending timers, backed by SQLite in production and
/// a JSON file in tests.
pub struct TimerStore {
path: PathBuf,
timers: Mutex<Vec<TimerEntry>>,
persistence: TimerPersistence,
}
impl TimerStore {
/// Load the timer store from `path`. Returns an empty store if the file
/// does not exist or cannot be parsed.
/// Load from the shared SQLite pool. This is the production constructor.
pub async fn from_pool(pool: SqlitePool) -> Result<Self, sqlx::Error> {
let rows: Vec<(String, String)> =
sqlx::query_as("SELECT story_id, scheduled_at FROM timers")
.fetch_all(&pool)
.await?;
let timers = rows
.into_iter()
.filter_map(|(story_id, scheduled_at_str)| {
let scheduled_at = scheduled_at_str.parse::<DateTime<Utc>>().ok()?;
Some(TimerEntry {
story_id,
scheduled_at,
})
})
.collect();
let (tx, rx) = mpsc::unbounded_channel();
spawn_timer_writer(pool, rx);
Ok(Self {
timers: Mutex::new(timers),
persistence: TimerPersistence::Sqlite(tx),
})
}
/// Load the timer store from a JSON file path. Used by unit tests.
pub fn load(path: PathBuf) -> Self {
let timers = if path.exists() {
std::fs::read_to_string(&path)
@@ -32,12 +104,12 @@ impl TimerStore {
Vec::new()
};
Self {
path,
timers: Mutex::new(timers),
persistence: TimerPersistence::Json(path),
}
}
fn save_locked(path: &Path, timers: &[TimerEntry]) -> Result<(), String> {
fn save_json(path: &Path, timers: &[TimerEntry]) -> Result<(), String> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create directory: {e}"))?;
@@ -46,14 +118,23 @@ impl TimerStore {
std::fs::write(path, content).map_err(|e| format!("Failed to write timers: {e}"))
}
/// Add a timer and persist to disk.
/// Add a timer and persist.
pub fn add(&self, story_id: String, scheduled_at: DateTime<Utc>) -> Result<(), String> {
let mut timers = self.timers.lock().unwrap();
timers.push(TimerEntry {
story_id,
story_id: story_id.clone(),
scheduled_at,
});
Self::save_locked(&self.path, &timers)
match &self.persistence {
TimerPersistence::Sqlite(tx) => {
let _ = tx.send(TimerWriteCmd::Upsert(TimerEntry {
story_id,
scheduled_at,
}));
Ok(())
}
TimerPersistence::Json(path) => Self::save_json(path, &timers),
}
}
/// Remove the timer for `story_id`. Returns `true` if one was removed.
@@ -63,7 +144,14 @@ impl TimerStore {
timers.retain(|t| t.story_id != story_id);
let removed = timers.len() < before;
if removed {
let _ = Self::save_locked(&self.path, &timers);
match &self.persistence {
TimerPersistence::Sqlite(tx) => {
let _ = tx.send(TimerWriteCmd::Delete(story_id.to_string()));
}
TimerPersistence::Json(path) => {
let _ = Self::save_json(path, &timers);
}
}
}
removed
}
@@ -73,33 +161,47 @@ impl TimerStore {
self.timers.lock().unwrap().clone()
}
/// Add or update a timer for `story_id`.
///
/// - If no timer exists for `story_id`, adds it.
/// - If a timer already exists and `scheduled_at` is **later**, updates it.
/// - If a timer already exists and `scheduled_at` is earlier or equal, no-op.
///
/// Use this instead of [`add`] when auto-scheduling from rate-limit events to
/// avoid creating duplicates and to always keep the latest reset time.
/// Add or update a timer for `story_id`. When an existing timer has an
/// earlier `scheduled_at`, it is updated to the later value. When it is
/// already later, this is a no-op.
pub fn upsert(&self, story_id: String, scheduled_at: DateTime<Utc>) -> Result<(), String> {
let mut timers = self.timers.lock().unwrap();
if let Some(existing) = timers.iter_mut().find(|t| t.story_id == story_id) {
if scheduled_at > existing.scheduled_at {
existing.scheduled_at = scheduled_at;
Self::save_locked(&self.path, &timers)?;
match &self.persistence {
TimerPersistence::Sqlite(tx) => {
let _ = tx.send(TimerWriteCmd::Upsert(TimerEntry {
story_id,
scheduled_at,
}));
}
TimerPersistence::Json(path) => {
Self::save_json(path, &timers)?;
}
}
}
} else {
timers.push(TimerEntry {
story_id,
story_id: story_id.clone(),
scheduled_at,
});
Self::save_locked(&self.path, &timers)?;
match &self.persistence {
TimerPersistence::Sqlite(tx) => {
let _ = tx.send(TimerWriteCmd::Upsert(TimerEntry {
story_id,
scheduled_at,
}));
}
TimerPersistence::Json(path) => {
Self::save_json(path, &timers)?;
}
}
}
Ok(())
}
/// Remove and return all timers whose `scheduled_at` is ≤ `now`.
/// Persists the updated list to disk if any timers were removed.
/// Remove and return all timers whose `scheduled_at` ≤ `now`.
pub fn take_due(&self, now: DateTime<Utc>) -> Vec<TimerEntry> {
let mut timers = self.timers.lock().unwrap();
let mut due = Vec::new();
@@ -113,7 +215,16 @@ impl TimerStore {
}
*timers = remaining;
if !due.is_empty() {
let _ = Self::save_locked(&self.path, &timers);
match &self.persistence {
TimerPersistence::Sqlite(tx) => {
for entry in &due {
let _ = tx.send(TimerWriteCmd::Delete(entry.story_id.clone()));
}
}
TimerPersistence::Json(path) => {
let _ = Self::save_json(path, &timers);
}
}
}
due
}
@@ -512,6 +623,29 @@ mod tests {
assert_eq!(list[0].story_id, "421_story_foo");
}
#[tokio::test]
async fn from_pool_persists_and_reloads() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("test.db");
let opts = sqlx::sqlite::SqliteConnectOptions::new()
.filename(&db_path)
.create_if_missing(true);
let pool = sqlx::SqlitePool::connect_with(opts).await.unwrap();
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
let t = Utc::now() + Duration::hours(2);
{
let store = TimerStore::from_pool(pool.clone()).await.unwrap();
store.add("421_story_foo".to_string(), t).unwrap();
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
let store2 = TimerStore::from_pool(pool.clone()).await.unwrap();
let list = store2.list();
assert_eq!(list.len(), 1);
assert_eq!(list[0].story_id, "421_story_foo");
}
#[test]
fn take_due_returns_only_past_entries() {
let dir = TempDir::new().unwrap();
+186 -9
View File
@@ -7,8 +7,10 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use tokio::sync::mpsc;
use uuid::Uuid;
// ── Action ────────────────────────────────────────────────────────────────────
@@ -76,16 +78,138 @@ impl ScheduledTimer {
}
}
// ── Background writer ─────────────────────────────────────────────────────────
enum SchedWriteCmd {
Upsert(ScheduledTimer),
Delete(String),
}
fn spawn_sched_writer(pool: SqlitePool, mut rx: mpsc::UnboundedReceiver<SchedWriteCmd>) {
tokio::spawn(async move {
while let Some(cmd) = rx.recv().await {
match cmd {
SchedWriteCmd::Upsert(t) => {
let action_json = match serde_json::to_string(&t.action) {
Ok(j) => j,
Err(e) => {
crate::slog!("[scheduled-timer] Serialize action failed: {e}");
continue;
}
};
let mode_json = match serde_json::to_string(&t.mode) {
Ok(j) => j,
Err(e) => {
crate::slog!("[scheduled-timer] Serialize mode failed: {e}");
continue;
}
};
let result = sqlx::query(
"INSERT INTO scheduled_timers \
(id, label, fire_at, action_json, mode_json, created_at) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6) \
ON CONFLICT(id) DO UPDATE SET \
label = excluded.label, \
fire_at = excluded.fire_at, \
action_json = excluded.action_json, \
mode_json = excluded.mode_json",
)
.bind(&t.id)
.bind(&t.label)
.bind(t.fire_at.to_rfc3339())
.bind(&action_json)
.bind(&mode_json)
.bind(t.created_at.to_rfc3339())
.execute(&pool)
.await;
if let Err(e) = result {
crate::slog!("[scheduled-timer] DB upsert failed for '{}': {e}", t.id);
}
}
SchedWriteCmd::Delete(id) => {
let result = sqlx::query("DELETE FROM scheduled_timers WHERE id = ?1")
.bind(&id)
.execute(&pool)
.await;
if let Err(e) = result {
crate::slog!("[scheduled-timer] DB delete failed for '{id}': {e}");
}
}
}
}
});
}
// ── Store ─────────────────────────────────────────────────────────────────────
/// Persistent store for generic scheduled timers, backed by a JSON file.
enum SchedPersistence {
Sqlite(mpsc::UnboundedSender<SchedWriteCmd>),
Json(PathBuf),
}
/// Persistent store for generic scheduled timers, backed by SQLite in
/// production and a JSON file in tests.
pub struct ScheduledTimerStore {
path: PathBuf,
timers: Mutex<Vec<ScheduledTimer>>,
persistence: SchedPersistence,
}
impl ScheduledTimerStore {
/// Load (or create empty) store from `path`.
/// Load from the shared SQLite pool. This is the production constructor.
pub async fn from_pool(pool: SqlitePool) -> Result<Self, sqlx::Error> {
let rows: Vec<(String, Option<String>, String, String, String, String)> = sqlx::query_as(
"SELECT id, label, fire_at, action_json, mode_json, created_at \
FROM scheduled_timers",
)
.fetch_all(&pool)
.await?;
let mut timers = Vec::with_capacity(rows.len());
for (id, label, fire_at_str, action_json, mode_json, created_at_str) in rows {
let fire_at = match fire_at_str.parse::<DateTime<Utc>>() {
Ok(t) => t,
Err(e) => {
crate::slog!("[scheduled-timer] Bad fire_at for {id}: {e}");
continue;
}
};
let action: TimerAction = match serde_json::from_str(&action_json) {
Ok(a) => a,
Err(e) => {
crate::slog!("[scheduled-timer] Bad action for {id}: {e}");
continue;
}
};
let mode: TimerMode = match serde_json::from_str(&mode_json) {
Ok(m) => m,
Err(e) => {
crate::slog!("[scheduled-timer] Bad mode for {id}: {e}");
continue;
}
};
let created_at = created_at_str
.parse::<DateTime<Utc>>()
.unwrap_or_else(|_| Utc::now());
timers.push(ScheduledTimer {
id,
label,
fire_at,
action,
mode,
created_at,
});
}
let (tx, rx) = mpsc::unbounded_channel();
spawn_sched_writer(pool, rx);
Ok(Self {
timers: Mutex::new(timers),
persistence: SchedPersistence::Sqlite(tx),
})
}
/// Load (or create empty) store from a JSON file path. Used by unit tests.
pub fn load(path: PathBuf) -> Self {
let timers = if path.exists() {
std::fs::read_to_string(&path)
@@ -96,12 +220,12 @@ impl ScheduledTimerStore {
Vec::new()
};
Self {
path,
timers: Mutex::new(timers),
persistence: SchedPersistence::Json(path),
}
}
fn save(path: &Path, timers: &[ScheduledTimer]) -> Result<(), String> {
fn save_json(path: &Path, timers: &[ScheduledTimer]) -> Result<(), String> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("mkdir failed: {e}"))?;
}
@@ -116,8 +240,14 @@ impl ScheduledTimerStore {
if timers.iter().any(|t| t.id == timer.id) {
return Err(format!("Timer with id '{}' already exists", timer.id));
}
timers.push(timer);
Self::save(&self.path, &timers)
timers.push(timer.clone());
match &self.persistence {
SchedPersistence::Sqlite(tx) => {
let _ = tx.send(SchedWriteCmd::Upsert(timer));
Ok(())
}
SchedPersistence::Json(path) => Self::save_json(path, &timers),
}
}
/// Remove a timer by ID. Returns `true` if one was removed.
@@ -127,7 +257,14 @@ impl ScheduledTimerStore {
timers.retain(|t| t.id != id);
let removed = timers.len() < before;
if removed {
let _ = Self::save(&self.path, &timers);
match &self.persistence {
SchedPersistence::Sqlite(tx) => {
let _ = tx.send(SchedWriteCmd::Delete(id.to_string()));
}
SchedPersistence::Json(path) => {
let _ = Self::save_json(path, &timers);
}
}
}
removed
}
@@ -151,7 +288,16 @@ impl ScheduledTimerStore {
}
*timers = remaining;
if !due.is_empty() {
let _ = Self::save(&self.path, &timers);
match &self.persistence {
SchedPersistence::Sqlite(tx) => {
for entry in &due {
let _ = tx.send(SchedWriteCmd::Delete(entry.id.clone()));
}
}
SchedPersistence::Json(path) => {
let _ = Self::save_json(path, &timers);
}
}
}
due
}
@@ -412,6 +558,37 @@ mod tests {
assert_eq!(due.len(), 1, "past timer must fire on next tick");
}
#[tokio::test]
async fn from_pool_persists_and_reloads() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("test.db");
let opts = sqlx::sqlite::SqliteConnectOptions::new()
.filename(&db_path)
.create_if_missing(true);
let pool = sqlx::SqlitePool::connect_with(opts).await.unwrap();
sqlx::query(
"CREATE TABLE IF NOT EXISTS scheduled_timers \
(id TEXT PRIMARY KEY, label TEXT, fire_at TEXT NOT NULL, \
action_json TEXT NOT NULL, mode_json TEXT NOT NULL, created_at TEXT NOT NULL)",
)
.execute(&pool)
.await
.unwrap();
let t = Utc::now() + chrono::Duration::hours(1);
{
let store = ScheduledTimerStore::from_pool(pool.clone()).await.unwrap();
store.add(make_timer("tm-aabbccdd", t)).unwrap();
// Give the background writer a moment to flush.
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
let store2 = ScheduledTimerStore::from_pool(pool).await.unwrap();
let list = store2.list();
assert_eq!(list.len(), 1);
assert_eq!(list[0].id, "tm-aabbccdd");
}
#[test]
fn new_id_has_tm_prefix() {
let id = ScheduledTimer::new_id();