huskies: merge 1061
This commit is contained in:
@@ -29,7 +29,7 @@ pub mod shadow_write;
|
||||
|
||||
pub use content_store::{ContentKey, all_content_ids, delete_content, read_content, write_content};
|
||||
pub use ops::{ItemMeta, delete_item, move_item_stage, next_item_number, write_item_with_content};
|
||||
pub use shadow_write::init;
|
||||
pub use shadow_write::{get_shared_pool, init};
|
||||
|
||||
#[cfg(test)]
|
||||
pub use content_store::ensure_content_store;
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user