//! Phase bodies: the only bridge between the reactor and lane modules. use std::path::PathBuf; use std::sync::Arc; use crate::db::DbHandle; use crate::db::model::Counts; use crate::nvd::{FetchResult, NvdClient}; use crate::pds::Publisher; use crate::publish::{self, PublishConfig, PublishStats}; use crate::release::artifacts::{ArtifactCounters, ArtifactResolver, S3Mirror}; use crate::release::host::{GovernedHttp, HostLimiter, HostLimiterConfig}; use crate::release::http::{RegistryHttp, ReqwestRegistryHttp}; use crate::release::source::SourceTransports; use crate::release::{ self, AffectedConfig, AffectedCounters, AllModeCounters, ExtrasConfig, ExtrasCounters, HistoryModeConfig, HistoryModeCounters, ReleaseConfig, ReleaseCounters, ReleaseCursor, }; use crate::runtime::events::EventTx; use crate::s3::{self, S3Config, S3Stats}; use crate::software::{self, SoftwareConfig, SoftwareCounters}; pub struct FetchPhaseOutcome { pub fetch: FetchResult, pub counts_before: Counts, pub cache_new: i64, pub cache_updated: i64, pub cache_hits: i64, } /// Fetch NVD, cache rows, advance the cursor, and commit. pub async fn fetch_phase( client: &NvdClient, db: &DbHandle, repo_did: &str, cursor: Option<&str>, ) -> anyhow::Result { let fetch = crate::nvd::fetch_nvd(client, cursor).await?; let counts_before = db.counts(repo_did).await?; let (cache_new, cache_updated, cache_hits) = db.upsert_cves(repo_did, &fetch.cves).await?; db.set_cursor(&fetch.cursor).await?; db.commit().await?; Ok(FetchPhaseOutcome { fetch, counts_before, cache_new, cache_updated, cache_hits, }) } pub async fn publish_phase( publisher: &Publisher, db: &DbHandle, config: &PublishConfig, events: EventTx, ) -> anyhow::Result { publish::drain_pending(publisher, db, config, events).await } pub async fn software_phase( publisher: &Publisher, db: &DbHandle, config: &SoftwareConfig, events: EventTx, ) -> anyhow::Result { software::sync_software(publisher, db, config, events).await } pub async fn release_phase( publisher: &Publisher, db: &DbHandle, config: &ReleaseConfig, events: EventTx, artifact_s3: Option, artifact_max_bytes: u64, ) -> anyhow::Result<(ReleaseCounters, ArtifactCounters)> { let http = governed_http(db, config.repo_did.as_str()).await?; let resolver = artifact_resolver_with_http(http.clone(), db, &config.repo_did, artifact_s3).await?; let counters = release::sync_releases_with_http_resolver( publisher, db, config, events, http, &resolver, artifact_max_bytes, ) .await?; Ok((counters, resolver.counters())) } pub async fn release_affected_phase( publisher: &Publisher, db: &DbHandle, config: &AffectedConfig, cursor: &mut ReleaseCursor, events: EventTx, artifact_s3: Option, artifact_max_bytes: u64, ) -> anyhow::Result<(AffectedCounters, ArtifactCounters)> { let http = governed_http(db, config.repo_did.as_str()).await?; let resolver = artifact_resolver_with_http(http.clone(), db, &config.repo_did, artifact_s3).await?; let counters = release::sync_affected_with_cursor_resolver( publisher, db, config, cursor, events, http, Some(&resolver), artifact_max_bytes, ) .await?; Ok((counters, resolver.counters())) } /// Build the shared artifact resolver (digest cache + optional S3 mirror). pub async fn artifact_resolver( db: &DbHandle, repo_did: &str, mirror: Option, ) -> anyhow::Result { let http = governed_http(db, repo_did).await?; artifact_resolver_with_http(http, db, repo_did, mirror).await } pub async fn artifact_resolver_with_http( http: Arc, db: &DbHandle, repo_did: &str, mirror: Option, ) -> anyhow::Result { let mut resolver = ArtifactResolver::new(http).with_cache(db.clone(), repo_did); if let Some(config) = mirror { let client = s3::client(&config).await?; resolver = resolver.with_mirror(Arc::new(S3Mirror::new( client, config.bucket.clone(), config.path.clone(), ))); } Ok(resolver) } pub async fn release_history_phase( publisher: &Publisher, db: &DbHandle, config: &HistoryModeConfig, cursor: &mut ReleaseCursor, events: EventTx, ) -> anyhow::Result<(HistoryModeCounters, ArtifactCounters)> { let resolver = artifact_resolver(db, &config.repo_did, config.artifact_s3.clone()).await?; let counters = release::sync_history_mode(publisher, db, config, cursor, events, &resolver).await?; Ok((counters, resolver.counters())) } pub async fn release_extras_phase( publisher: &Publisher, db: &DbHandle, config: &ExtrasConfig, cursor: &mut ReleaseCursor, events: EventTx, artifact_s3: Option, ) -> anyhow::Result<(ExtrasCounters, ArtifactCounters)> { let http = governed_http(db, config.repo_did.as_str()).await?; let resolver = artifact_resolver_with_http(http.clone(), db, &config.repo_did, artifact_s3).await?; let transports = SourceTransports::production()?; let counters = release::sync_extras_with_cursor_resolver( publisher, db, config, cursor, events, http, &resolver, &transports, ) .await?; Ok((counters, resolver.counters())) } /// `--release-mode all`: latest -> affected -> history (optional) -> extras /// with one shared resolver and rotation cursor. #[allow(clippy::too_many_arguments)] pub async fn release_all_phase( publisher: &Publisher, db: &DbHandle, latest: &ReleaseConfig, affected: &AffectedConfig, history: Option<&HistoryModeConfig>, extras: &ExtrasConfig, cursor: &mut ReleaseCursor, events: EventTx, artifact_s3: Option, artifact_max_bytes: u64, ) -> anyhow::Result<(AllModeCounters, ArtifactCounters)> { let http = governed_http(db, &latest.repo_did).await?; let resolver = artifact_resolver_with_http(http.clone(), db, &latest.repo_did, artifact_s3).await?; let transports = SourceTransports::production()?; let extras = ExtrasConfig { artifact_max_bytes, ..extras.clone() }; let counters = release::sync_all_with_http_resolver( publisher, db, latest, affected, history, &extras, cursor, events, http, &resolver, &transports, ) .await?; Ok((counters, resolver.counters())) } /// Production registry transport shared by every release mode: per-host token /// buckets with persisted `source_host_state` backoff. async fn governed_http(db: &DbHandle, repo_did: &str) -> anyhow::Result> { let inner: Arc = Arc::new(ReqwestRegistryHttp::new()?); let limiter = Arc::new(HostLimiter::new(HostLimiterConfig::default())); Ok(Arc::new( GovernedHttp::new(inner, limiter, Some(db.clone()), repo_did).await?, )) } /// Owned-argument wrapper: keeps borrowed S3 config/path out of the reactor's /// spawned future, whose `Send` check is higher-ranked. pub async fn s3_download_phase(config: S3Config, dest: PathBuf) -> anyhow::Result> { s3::download(&config, &dest).await } pub async fn snapshot_phase( config: S3Config, db: DbHandle, cache_db: PathBuf, ) -> anyhow::Result { s3::snapshot_upload(&config, &db, &cache_db).await }