huskies: merge 1061
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user