Skip to content

Commit

Permalink
[nexus] Put a SagaContext in a once cell
Browse files Browse the repository at this point in the history
This allows the ability to run sagas from background tasks. The
`SagaContext` already has a copy of an `Arc<BackgroundTasks>` 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
  • Loading branch information
andrewjstone committed Jun 6, 2024
1 parent afc6600 commit 097f1c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nexus/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions nexus/src/saga_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SagaContext> = OnceLock::new();

/// A type accessible to all saga methods
#[derive(Clone)]
Expand Down

0 comments on commit 097f1c2

Please sign in to comment.