use std::{
collections::HashSet,
path::PathBuf,
time::{Duration, Instant},
};
use super::wasm_cache::{
SharedIncrementalTargetMaintenanceConfig, SharedIncrementalTargetMaintenanceOutcome,
SharedIncrementalTargetPrunePolicy, WasmBuildCacheMode, WasmBuildError, WasmBuildOutcome,
WasmBuildProgressConfig, WasmBuildProgressEvent, WasmBuildSpec, build_wasm_canisters_cached,
build_wasm_canisters_cached_with_progress,
};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct WasmBuildBatchConfig {
shared_incremental_maintenance: Option<SharedIncrementalTargetMaintenanceConfig>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WasmBuildBatchOutcome {
outcomes: Vec<WasmBuildOutcome>,
total: Duration,
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum WasmBuildBatchProgressEvent {
BuildStarted {
index: usize,
total: usize,
},
BuildProgress {
index: usize,
event: WasmBuildProgressEvent,
},
BuildFinished {
index: usize,
},
}
#[derive(Debug)]
pub struct WasmBuildBatchError {
failed_index: usize,
completed: Vec<WasmBuildOutcome>,
total: Duration,
source: WasmBuildError,
}
impl WasmBuildBatchOutcome {
#[must_use]
pub fn outcomes(&self) -> &[WasmBuildOutcome] {
&self.outcomes
}
#[must_use]
pub fn into_outcomes(self) -> Vec<WasmBuildOutcome> {
self.outcomes
}
#[must_use]
pub const fn total(&self) -> Duration {
self.total
}
pub fn shared_incremental_maintenance_outcomes(
&self,
) -> impl Iterator<Item = (usize, &SharedIncrementalTargetMaintenanceOutcome)> {
self.outcomes
.iter()
.enumerate()
.filter_map(|(index, outcome)| {
outcome
.record()
.shared_incremental_maintenance()
.map(|maintenance| (index, maintenance))
})
}
}
impl WasmBuildBatchConfig {
#[must_use]
pub const fn new() -> Self {
Self {
shared_incremental_maintenance: None,
}
}
#[must_use]
pub const fn with_shared_incremental_target_maintenance(
mut self,
config: SharedIncrementalTargetMaintenanceConfig,
) -> Self {
self.shared_incremental_maintenance = Some(config);
self
}
#[must_use]
pub const fn with_shared_incremental_target_maintenance_at_most_every(
self,
policy: SharedIncrementalTargetPrunePolicy,
minimum_interval: Duration,
) -> Self {
self.with_shared_incremental_target_maintenance(
SharedIncrementalTargetMaintenanceConfig::new(policy, minimum_interval),
)
}
#[must_use]
pub const fn shared_incremental_target_maintenance(
self,
) -> Option<SharedIncrementalTargetMaintenanceConfig> {
self.shared_incremental_maintenance
}
}
impl WasmBuildBatchError {
#[must_use]
pub const fn failed_index(&self) -> usize {
self.failed_index
}
#[must_use]
pub fn completed(&self) -> &[WasmBuildOutcome] {
&self.completed
}
#[must_use]
pub fn into_parts(self) -> (Vec<WasmBuildOutcome>, WasmBuildError) {
(self.completed, self.source)
}
#[must_use]
pub const fn total(&self) -> Duration {
self.total
}
}
impl std::fmt::Display for WasmBuildBatchOutcome {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let reused = self
.outcomes
.iter()
.filter(|outcome| outcome.is_reused())
.count();
write!(
formatter,
"builds={} built={} reused={} total={:?}",
self.outcomes.len(),
self.outcomes.len().saturating_sub(reused),
reused,
self.total,
)
}
}
pub fn build_wasm_canisters_cached_batch(
specs: &[WasmBuildSpec],
) -> Result<WasmBuildBatchOutcome, WasmBuildBatchError> {
build_wasm_canisters_cached_batch_with_config(specs, WasmBuildBatchConfig::new())
}
pub fn build_wasm_canisters_cached_batch_with_config(
specs: &[WasmBuildSpec],
config: WasmBuildBatchConfig,
) -> Result<WasmBuildBatchOutcome, WasmBuildBatchError> {
build_wasm_batch(specs, config, |spec, _index| {
build_wasm_canisters_cached(spec)
})
}
pub fn build_wasm_canisters_cached_batch_with_progress<F>(
specs: &[WasmBuildSpec],
config: WasmBuildProgressConfig,
observer: F,
) -> Result<WasmBuildBatchOutcome, WasmBuildBatchError>
where
F: FnMut(WasmBuildBatchProgressEvent),
{
build_wasm_canisters_cached_batch_with_config_and_progress(
specs,
WasmBuildBatchConfig::new(),
config,
observer,
)
}
pub fn build_wasm_canisters_cached_batch_with_config_and_progress<F>(
specs: &[WasmBuildSpec],
batch_config: WasmBuildBatchConfig,
progress_config: WasmBuildProgressConfig,
mut observer: F,
) -> Result<WasmBuildBatchOutcome, WasmBuildBatchError>
where
F: FnMut(WasmBuildBatchProgressEvent),
{
let count = specs.len();
build_wasm_batch(specs, batch_config, |spec, index| {
observer(WasmBuildBatchProgressEvent::BuildStarted {
index,
total: count,
});
let outcome = build_wasm_canisters_cached_with_progress(spec, progress_config, |event| {
observer(WasmBuildBatchProgressEvent::BuildProgress { index, event });
})?;
observer(WasmBuildBatchProgressEvent::BuildFinished { index });
Ok(outcome)
})
}
fn build_wasm_batch<F>(
specs: &[WasmBuildSpec],
config: WasmBuildBatchConfig,
mut build: F,
) -> Result<WasmBuildBatchOutcome, WasmBuildBatchError>
where
F: FnMut(&WasmBuildSpec, usize) -> Result<WasmBuildOutcome, WasmBuildError>,
{
let started = Instant::now();
if let Some(failed_index) = config.shared_incremental_maintenance.and_then(|_| {
specs
.iter()
.position(|spec| spec.shared_incremental_target_maintenance().is_some())
}) {
return Err(WasmBuildBatchError {
failed_index,
completed: Vec::new(),
total: started.elapsed(),
source: batch_maintenance_ownership_error(),
});
}
let mut outcomes = Vec::with_capacity(specs.len());
let mut maintenance = BatchMaintenanceTracker::new(config.shared_incremental_maintenance);
for (index, spec) in specs.iter().enumerate() {
let configured = maintenance.prepare_spec(spec);
match build(configured.as_ref().unwrap_or(spec), index) {
Ok(outcome) => outcomes.push(outcome),
Err(source) => {
return Err(WasmBuildBatchError {
failed_index: index,
completed: outcomes,
total: started.elapsed(),
source,
});
}
}
}
Ok(WasmBuildBatchOutcome {
outcomes,
total: started.elapsed(),
})
}
struct BatchMaintenanceTracker {
config: Option<SharedIncrementalTargetMaintenanceConfig>,
configured_targets: HashSet<PathBuf>,
}
impl BatchMaintenanceTracker {
fn new(config: Option<SharedIncrementalTargetMaintenanceConfig>) -> Self {
Self {
config,
configured_targets: HashSet::new(),
}
}
fn prepare_spec(&mut self, spec: &WasmBuildSpec) -> Option<WasmBuildSpec> {
let config = self.config?;
debug_assert!(spec.shared_incremental_target_maintenance().is_none());
let WasmBuildCacheMode::SharedIncremental { target_dir } = spec.cache_mode() else {
return None;
};
if !self.configured_targets.insert(target_dir.clone()) {
return None;
}
Some(
spec.clone()
.with_shared_incremental_target_maintenance(config),
)
}
}
fn batch_maintenance_ownership_error() -> WasmBuildError {
WasmBuildError::InvalidSpec {
message:
"batch-owned shared-target maintenance cannot be combined with per-spec maintenance"
.to_owned(),
}
}
impl std::fmt::Display for WasmBuildBatchError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
formatter,
"independent Wasm build {} failed after {} successful build(s): {}",
self.failed_index,
self.completed.len(),
self.source,
)
}
}
impl std::error::Error for WasmBuildBatchError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[cfg(test)]
mod tests;