From 097f1c265677cb8c0a695eff73b75b59dcd6ee67 Mon Sep 17 00:00:00 2001 From: "Andrew J. Stone" Date: Thu, 6 Jun 2024 21:51:36 +0000 Subject: [PATCH] [nexus] Put a `SagaContext` in a once cell This allows the ability to run sagas from background tasks. The `SagaContext` already has a copy of an `Arc` which means that individual background tasks must have been created/ initialized before the `SagaContext`. We get around this build time circular dependency by putting the `SagaContext` inside a static `OnceCell` where the background tasks can access it at runtime. Builds upon #5856 --- nexus/src/app/mod.rs | 6 ++++++ nexus/src/saga_interface.rs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/nexus/src/app/mod.rs b/nexus/src/app/mod.rs index 830fd64d32..c87d67eeec 100644 --- a/nexus/src/app/mod.rs +++ b/nexus/src/app/mod.rs @@ -669,6 +669,12 @@ impl Nexus { Arc::clone(&background_tasks), ); + // Initialize a global `SagaContext`. This provides access to + // background tasks. + saga_interface::SAGA_CONTEXT + .set(saga_context.clone()) + .expect("SAGA_CONTEXT already initialized"); + let nexus = Nexus { id: config.deployment.id, rack_id, diff --git a/nexus/src/saga_interface.rs b/nexus/src/saga_interface.rs index f87dd9536c..d5951ab237 100644 --- a/nexus/src/saga_interface.rs +++ b/nexus/src/saga_interface.rs @@ -16,6 +16,11 @@ use nexus_db_queries::{authz, db}; use slog::Logger; use std::fmt; use std::sync::Arc; +use std::sync::OnceLock; + +/// Callers that want to execute a saga can clone a context from here, and +/// modify the logger if desired. +pub static SAGA_CONTEXT: OnceLock = OnceLock::new(); /// A type accessible to all saga methods #[derive(Clone)]