Skip to main content

faucet_cli/commands/
run.rs

1//! `faucet run` — load a pipeline config, expand the matrix, execute every
2//! invocation under bounded concurrency.
3
4use crate::cli::RunArgs;
5use crate::config::PipelineConfig;
6use crate::error::{CliError, CliResult};
7use crate::executor::{ExecuteOptions, run_expanded};
8use crate::expand::expand;
9
10/// Parse the optional `--clock` override (RFC3339 or `YYYY-MM-DD`), or default
11/// to process start. Returned as a UTC fixed-offset clock for `${now.*}`.
12/// Shared with `faucet test` (the `--clock` flag and per-case `clock:` field).
13pub(crate) fn resolve_run_clock(
14    flag: Option<&str>,
15) -> CliResult<chrono::DateTime<chrono::FixedOffset>> {
16    use chrono::{DateTime, NaiveDate, TimeZone, Utc};
17    match flag {
18        None => Ok(Utc::now().fixed_offset()),
19        Some(s) => {
20            if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
21                return Ok(dt);
22            }
23            if let Ok(d) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
24                let ndt = d.and_hms_opt(0, 0, 0).expect("00:00:00 is valid");
25                return Ok(Utc.from_utc_datetime(&ndt).fixed_offset());
26            }
27            Err(CliError::Config(format!(
28                "--clock '{s}' is not RFC3339 (2026-01-31T00:00:00Z) or a date (2026-01-31)"
29            )))
30        }
31    }
32}
33
34/// Execute the `run` subcommand.
35pub async fn run(args: RunArgs) -> CliResult<()> {
36    let cwd = std::env::current_dir()?;
37    let env_path =
38        crate::env_loader::resolve_env_file(args.env_file.as_deref(), args.no_env_file, &cwd)?;
39    crate::env_loader::load_env_file_if_present(env_path.as_deref())?;
40
41    let resolved_config_path: Option<std::path::PathBuf> = if args.from_env {
42        None
43    } else {
44        Some(match args.config.as_ref() {
45            Some(p) => p.clone(),
46            None => {
47                crate::env_loader::discover_config_path(&cwd).ok_or(CliError::NoConfigOrFromEnv)?
48            }
49        })
50    };
51
52    let cfg = if args.from_env {
53        if args.profile.is_some() {
54            tracing::warn!(
55                "--profile / FAUCET_PROFILE has no effect in --from-env mode (no config file to compose); ignoring"
56            );
57        }
58        crate::env_config::from_process_env()?
59    } else {
60        PipelineConfig::from_path_async(
61            resolved_config_path
62                .as_ref()
63                .expect("YAML mode always resolves a path above"),
64            args.profile.as_deref(),
65        )
66        .await?
67    };
68
69    crate::obs::install(&cfg)?;
70
71    let pipeline_name = cfg.name.clone().unwrap_or_else(|| {
72        resolved_config_path
73            .as_ref()
74            .and_then(|p| p.file_stem())
75            .and_then(|s| s.to_str())
76            .unwrap_or("pipeline")
77            .to_owned()
78    });
79
80    let auth = crate::auth_catalog::build_auth_catalog(cfg.auth.as_ref())?;
81    #[cfg(feature = "lineage")]
82    let lineage = crate::lineage_glue::build_emitter(cfg.lineage.as_ref())
83        .map_err(|e| CliError::Config(format!("lineage: {e}")))?;
84    let resilience = match &cfg.resilience {
85        Some(spec) => Some(spec.to_policy()?),
86        None => None,
87    };
88    #[cfg(feature = "notify")]
89    let notifier = crate::notify::Notifier::from_specs(&cfg.notifications)?;
90    #[cfg(feature = "catalog")]
91    let catalog = match cfg.catalog.as_ref() {
92        Some(spec) => Some(crate::catalog::connect_from_spec(spec).await?),
93        None => None,
94    };
95    let nodes = expand(&cfg)?;
96    let summary = run_expanded(
97        nodes,
98        ExecuteOptions {
99            pipeline_name: pipeline_name.clone(),
100            execution: cfg.execution.clone(),
101            dry_run: args.dry_run,
102            limit: args.limit,
103            state_path_override: args.state_path.clone(),
104            shard: None,
105            auth,
106            clock: resolve_run_clock(args.clock.as_deref())?,
107            // `faucet run` has no external cancel signal; the executor still
108            // cooperatively cancels in-flight rows on `on_error: stop`.
109            cancel: None,
110            resilience,
111            sla: cfg.sla.clone(),
112            #[cfg(feature = "lineage")]
113            lineage,
114            #[cfg(feature = "lineage")]
115            lineage_cfg: cfg.lineage.clone(),
116            #[cfg(feature = "notify")]
117            notifier,
118            #[cfg(feature = "catalog")]
119            catalog,
120        },
121    )
122    .await?;
123
124    let total_written: usize = summary.invocations.iter().map(|i| i.records_written).sum();
125    let success = summary
126        .invocations
127        .iter()
128        .filter(|i| i.error.is_none())
129        .count();
130    let failed = summary.failure_count();
131
132    tracing::info!(
133        pipeline = %pipeline_name,
134        invocations = summary.invocations.len(),
135        succeeded = success,
136        failed,
137        records_written = total_written,
138        "pipeline completed"
139    );
140    println!(
141        "{}: {} invocation{}, {} ok, {} failed, wrote {} record{}",
142        pipeline_name,
143        summary.invocations.len(),
144        if summary.invocations.len() == 1 {
145            ""
146        } else {
147            "s"
148        },
149        success,
150        failed,
151        total_written,
152        if total_written == 1 { "" } else { "s" }
153    );
154
155    // Flush any buffered OTLP telemetry before the process exits (no-op without
156    // the `otel` feature). Done on both the success and failure exit paths.
157    faucet_core::shutdown_otel();
158
159    if summary.had_failures() {
160        return Err(CliError::PipelineHadFailures { count: failed });
161    }
162    Ok(())
163}
164
165#[cfg(test)]
166mod tests {
167    use super::*;
168
169    #[test]
170    fn run_clock_parses_rfc3339_date_and_defaults() {
171        // RFC3339
172        let c = resolve_run_clock(Some("2026-01-31T12:00:00Z")).unwrap();
173        assert_eq!(c.format("%Y-%m-%d %H:%M").to_string(), "2026-01-31 12:00");
174        // date-only → midnight UTC
175        let c = resolve_run_clock(Some("2026-01-31")).unwrap();
176        assert_eq!(c.format("%Y-%m-%d %H:%M").to_string(), "2026-01-31 00:00");
177        // default = now (just assert it's Ok / recent year)
178        let c = resolve_run_clock(None).unwrap();
179        assert!(c.format("%Y").to_string().parse::<i32>().unwrap() >= 2025);
180        // bad input errors
181        assert!(resolve_run_clock(Some("not-a-date")).is_err());
182    }
183}