pub struct AppState {
pub store: Arc<dyn Store>,
pub engine: Arc<Engine>,
pub jwt_config: Arc<JwtConfig>,
pub worker_token: String,
pub event_sender: Sender<Event>,
pub event_bus: Option<WorkflowEventBus>,
pub blob_store: Option<Arc<dyn BlobStore>>,
}Expand description
Global application state.
Holds the shared store (runs, users, API keys, secrets) and engine, extracted by handlers using Axum’s state extraction mechanism.
§Examples
use ironflow_api::state::AppState;
use ironflow_auth::jwt::JwtConfig;
use ironflow_store::prelude::*;
use ironflow_store::store::Store;
use ironflow_engine::engine::Engine;
use ironflow_core::providers::claude::ClaudeCodeProvider;
use std::sync::Arc;
let store: Arc<dyn Store> = Arc::new(InMemoryStore::new());
let provider = Arc::new(ClaudeCodeProvider::new());
let engine = Arc::new(Engine::new(store.clone(), provider));
let jwt_config = Arc::new(JwtConfig {
secret: "secret".to_string(),
access_token_ttl_secs: 900,
refresh_token_ttl_secs: 604800,
cookie_domain: None,
cookie_secure: false,
});
let broadcaster = ironflow_api::sse::SseBroadcaster::new();
let state = AppState::new(store, engine, jwt_config, "token".to_string(), broadcaster.sender());Fields§
§store: Arc<dyn Store>The unified backing store for runs, steps, users, API keys, and secrets.
engine: Arc<Engine>The workflow orchestration engine.
jwt_config: Arc<JwtConfig>JWT configuration for auth tokens.
worker_token: StringStatic token for worker-to-API authentication.
event_sender: Sender<Event>Broadcast sender for SSE event streaming.
event_bus: Option<WorkflowEventBus>Per-run event bus for real-time workflow monitoring.
When set, the GET /api/v1/runs/{id}/events route subscribes to this
bus and streams WorkflowEvents
via SSE. None when the engine was not configured with a bus.
blob_store: Option<Arc<dyn BlobStore>>Where artifact bytes live, when artifacts are enabled.
None on a deployment that has not configured artifact storage: the
artifact routes answer 501 and every other endpoint is unaffected.
Implementations§
Source§impl AppState
impl AppState
Sourcepub fn new(
store: Arc<dyn Store>,
engine: Arc<Engine>,
jwt_config: Arc<JwtConfig>,
worker_token: String,
event_sender: Sender<Event>,
) -> Self
pub fn new( store: Arc<dyn Store>, engine: Arc<Engine>, jwt_config: Arc<JwtConfig>, worker_token: String, event_sender: Sender<Event>, ) -> Self
Create a new AppState.
When the prometheus feature is enabled, a global Prometheus recorder
is installed (once) and its handle is stored in the state.
§Panics
Panics if a Prometheus recorder cannot be installed (should only happen if another incompatible recorder was set elsewhere).
Sourcepub fn with_blob_store(self, blob_store: Arc<dyn BlobStore>) -> Self
pub fn with_blob_store(self, blob_store: Arc<dyn BlobStore>) -> Self
Enable artifacts by attaching the backend that holds their bytes.
§Examples
use std::sync::Arc;
use ironflow_api::state::AppState;
use ironflow_artifacts::blob_store::BlobStore;
use ironflow_artifacts::local::LocalBlobStore;
let blob: Arc<dyn BlobStore> = Arc::new(LocalBlobStore::new("/var/lib/ironflow/artifacts"));
state.with_blob_store(blob)Sourcepub fn with_event_bus(self, bus: WorkflowEventBus) -> Self
pub fn with_event_bus(self, bus: WorkflowEventBus) -> Self
Attach a WorkflowEventBus for per-run SSE streaming.
When set, GET /api/v1/runs/{id}/events streams step-level events
for a specific workflow run. When absent the route returns an empty
SSE stream (with keep-alive).
§Examples
use ironflow_api::state::AppState;
use ironflow_engine::notify::WorkflowEventBus;
state.with_event_bus(WorkflowEventBus::new())Sourcepub fn blob_store_or_501(&self) -> Result<&Arc<dyn BlobStore>, ApiError>
pub fn blob_store_or_501(&self) -> Result<&Arc<dyn BlobStore>, ApiError>
The artifact backend, or a 501 error when artifacts are disabled.
§Errors
Returns ApiError::ArtifactStorageUnavailable when no backend is attached.
Sourcepub async fn get_run_or_404(&self, id: Uuid) -> Result<Run, ApiError>
pub async fn get_run_or_404(&self, id: Uuid) -> Result<Run, ApiError>
Fetch a run by ID or return 404.
§Errors
Returns ApiError::RunNotFound if the run does not exist.
Returns ApiError::Store if there is a store error.
Sourcepub async fn spawn_background_tasks(&self) -> CancellationToken
pub async fn spawn_background_tasks(&self) -> CancellationToken
Spawn the built-in background tasks and return their shared shutdown token.
This starts:
- Schedule sync: seeds DB rows for handler-declared schedules.
- Schedule ticker: polls due schedules and creates runs.
- Reaper: recovers runs abandoned by dead workers.
Call this once after building the AppState, before serving requests.
Drop the returned CancellationToken (or call .cancel()) to stop
all tasks gracefully.
§Examples
use ironflow_api::state::AppState;
let shutdown = state.spawn_background_tasks().await;
// ... serve requests ...
shutdown.cancel();