huskies: merge 1061

This commit is contained in:
dave
2026-05-14 20:08:09 +00:00
parent 54d9737428
commit 5678f2a556
11 changed files with 752 additions and 82 deletions
+19
View File
@@ -3,6 +3,10 @@
//! `init` opens the database, runs migrations, loads existing content into the
//! in-memory store, and spawns the write loop. All subsequent writes are sent
//! over an unbounded channel so callers never block on I/O.
//!
//! The opened pool is stored in [`SHARED_POOL`] so that other subsystems
//! (event-trigger store, timer store, scheduled-timer store) can share the
//! same database connection without re-opening the file.
use crate::slog;
use sqlx::SqlitePool;
use sqlx::sqlite::SqliteConnectOptions;
@@ -11,6 +15,18 @@ use std::path::Path;
use std::sync::OnceLock;
use tokio::sync::mpsc;
/// The process-global SQLite pool, set once by [`init`].
///
/// Other modules call [`get_shared_pool`] to access the pool without needing
/// to pass it through every call-site.
static SHARED_POOL: OnceLock<SqlitePool> = OnceLock::new();
/// Return a reference to the shared pipeline database pool, if it has been
/// initialised by a prior call to [`init`].
pub fn get_shared_pool() -> Option<&'static SqlitePool> {
SHARED_POOL.get()
}
/// A pending shadow write for one pipeline item.
pub(super) struct PipelineWriteMsg {
pub(super) story_id: String,
@@ -47,6 +63,9 @@ pub async fn init(db_path: &Path) -> Result<(), sqlx::Error> {
let pool = SqlitePool::connect_with(options).await?;
sqlx::migrate!("./migrations").run(&pool).await?;
// Store pool in global static so other subsystems can reuse it.
let _ = SHARED_POOL.set(pool.clone());
// Load existing content into the in-memory store.
let rows: Vec<(String, Option<String>)> =
sqlx::query_as("SELECT id, content FROM pipeline_items WHERE content IS NOT NULL")