feat: event ids, outbox identity migration

This commit is contained in:
2026-10-25 23:57:24 +01:00
parent c31aa033cc
commit 32dfc84697
14 changed files with 287 additions and 39 deletions

View File

@@ -26,23 +26,14 @@ impl EventBusBackend {
}
}
/// The server's publisher is always the outbox writer: it never talks to the
/// bus, whatever `EVENT_BUS_BACKEND` says. The worker owns delivery.
pub async fn create_publisher(
db_pool: &sqlx::SqlitePool,
config: &config::AppConfig,
) -> anyhow::Result<Arc<dyn EventPublisher>> {
match EventBusBackend::from_config(&config.events)? {
EventBusBackend::Db => {
tracing::info!("event bus: DB queue");
Ok(sqlite::SqliteEventQueue::create_publisher(db_pool.clone(), &config.events).await?)
}
#[cfg(feature = "nats")]
EventBusBackend::Nats => {
let cfg = nats::NatsConfig::from_config(&config.nats)
.context("EVENT_BUS_BACKEND=nats requires NATS_URL to be set")?;
tracing::info!("event bus: NATS ({})", cfg.url);
Ok(nats::create_publisher(cfg).await?)
}
}
tracing::info!("event publisher: outbox (DB queue)");
sqlite::SqliteEventQueue::create_publisher(db_pool.clone(), &config.events).await
}
pub async fn create_channel(

View File

@@ -67,3 +67,43 @@ async fn federation_off_build_wires_local_social() {
"follower should be alice: {body}"
);
}
/// The server never talks to the bus: whatever `EVENT_BUS_BACKEND` says, a
/// change and its event land in the outbox table. No NATS server is reachable here.
#[tokio::test]
async fn server_publishes_to_the_outbox_even_when_bus_is_nats() {
let pool = support::memory_pool().await;
let mut config = support::test_config();
config.events.bus_backend = "nats".into();
config.nats.url = Some("nats://127.0.0.1:1".into());
let router = bootstrap::build_server_on(&config, pool.clone())
.await
.expect("server builds without a reachable NATS");
let session = support::register(&router, "alice", "alice@example.com").await;
let (status, _, body) = support::send(
&router,
support::json_request(
axum::http::Method::POST,
"/api/v1/reviews",
Some(&session.token),
serde_json::json!({
"manual_title": "Outbox",
"manual_release_year": 2001,
"rating": 4,
"comment": "queued",
"watched_at": "2024-01-01"
}),
),
)
.await;
assert_eq!(status, 201, "log review: {body}");
let queued: i64 = sqlx::query_scalar(
"SELECT COUNT(*) FROM event_queue WHERE event_type = 'ReviewLogged' AND event_id IS NOT NULL",
)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(queued, 1);
}