//! Declarative pipeline flow configuration (`config/pipeline_flows.json`). //! //! This module is the single source of truth for *how* the worker executes each //! pipeline stage. In particular, the `parallel` flag on a stage declares whether //! that stage runs in the background (non-blocking) or sequentially (the caller //! waits for it to finish). The worker reads `parallel` from here instead of //! hardcoding the execution mode per stage. //! //! Example: the `scene` stage (Scene VLM) is declared `"parallel": true`, so the //! worker dispatches it to a background task rather than awaiting it inline in the //! poll loop. //! //! Loaded once and cached. Edit `config/pipeline_flows.json` and restart the worker //! to apply changes. use std::collections::HashMap; use std::sync::OnceLock; use serde::Deserialize; fn default_true() -> bool { true } /// A single stage within a flow (one processor). #[derive(Debug, Clone, Deserialize)] pub struct Stage { pub name: String, #[serde(default)] pub processor_type: String, /// `true` = may run concurrently / dispatched to a background task (non-blocking). /// `false` = sequential, the caller waits for completion. #[serde(default = "default_true")] pub parallel: bool, #[serde(default)] pub dependencies: Vec, #[serde(default)] pub enabled: bool, #[serde(default)] pub timeout_secs: u64, } /// An ordered collection of stages (e.g. the `register` flow). #[derive(Debug, Clone, Deserialize, Default)] pub struct Flow { #[serde(default)] pub name: String, #[serde(default)] pub enabled: bool, #[serde(default)] pub stages: Vec, } /// A phase grouping (used for the progress UI and concurrency ceilings). #[derive(Debug, Clone, Deserialize, Default)] pub struct Phase { #[serde(default)] pub id: i64, #[serde(default)] pub name_en: Option, #[serde(default)] pub parallel_strategy: String, #[serde(default)] pub max_concurrent: Option, #[serde(default)] pub processors: Vec, } /// Root of `config/pipeline_flows.json`. #[derive(Debug, Clone, Deserialize, Default)] pub struct PipelineFlows { #[serde(default)] pub version: String, #[serde(default)] pub enabled: bool, #[serde(default)] pub max_concurrent_stages: u32, #[serde(default)] pub default_timeout_secs: u64, #[serde(default)] pub phases: Vec, #[serde(default)] pub flows: HashMap, } static CONFIG: OnceLock = OnceLock::new(); /// Get the loaded pipeline flow configuration (loaded once, cached). pub fn get() -> &'static PipelineFlows { CONFIG.get_or_init(load) } fn load() -> PipelineFlows { let candidates = [ crate::core::path_utils::config_dir().join("pipeline_flows.json"), std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("config/pipeline_flows.json"), ]; for path in candidates { match std::fs::read_to_string(&path) { Ok(s) => match serde_json::from_str::(&s) { Ok(cfg) => { tracing::info!( "pipeline_config: loaded {} (version={}, {} flows)", path.display(), cfg.version, cfg.flows.len() ); return cfg; } Err(e) => { tracing::warn!("pipeline_config: failed to parse {}: {}", path.display(), e) } }, Err(_) => continue, } } tracing::warn!("pipeline_config: pipeline_flows.json not found, using defaults"); PipelineFlows { default_timeout_secs: 3600, ..Default::default() } } impl PipelineFlows { /// Look up a stage by flow name and stage/processor name. pub fn stage(&self, flow: &str, name: &str) -> Option<&Stage> { self.flows.get(flow)?.stages.iter().find(|s| s.name == name) } /// Whether a stage is declared to run in parallel (non-blocking). /// /// Unknown stages default to `true` (parallel / non-blocking) so that a /// missing config entry never reintroduces blocking behavior. pub fn is_parallel(&self, flow: &str, name: &str) -> bool { self.stage(flow, name).map(|s| s.parallel).unwrap_or(true) } /// Declared dependencies for a stage (empty when unknown). pub fn dependencies(&self, flow: &str, name: &str) -> Vec { self.stage(flow, name) .map(|s| s.dependencies.clone()) .unwrap_or_default() } /// Effective timeout for a stage (falls back to `default_timeout_secs`). pub fn timeout_secs(&self, flow: &str, name: &str) -> u64 { self.stage(flow, name) .map(|s| s.timeout_secs) .filter(|&t| t > 0) .unwrap_or(self.default_timeout_secs) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_unknown_stage_defaults_parallel() { let cfg = PipelineFlows::default(); assert!(cfg.is_parallel("register", "does_not_exist")); assert!(cfg.dependencies("register", "does_not_exist").is_empty()); } #[test] fn test_scene_declared_parallel() { // The shipped config declares the scene stage as parallel. let cfg = get(); assert!( cfg.is_parallel("register", "scene"), "scene stage must be parallel in pipeline_flows.json" ); assert_eq!( cfg.dependencies("register", "scene"), vec!["cut".to_string()] ); } }