use std::time::Duration;
use super::failures::{ASK_ENGINE, checked};
use super::{Compose, ToolCx, engine_error};
use crate::Result;
use crate::cli::CliError;
use crate::cli::compose::{self, Failure};
use crate::error::{DownloadKind, Error};
use crate::events::{Stream, WarningCode};
pub(super) const PULL_STALL: Duration = Duration::from_secs(600);
pub(super) const RETRY_AFTER: [Duration; 2] = [Duration::from_secs(10), Duration::from_secs(30)];
const PULL_LIMIT: Duration = Duration::from_secs(3600);
const STEP: &str = "pull-up";
impl Compose {
pub(super) async fn pull(&self, cx: &ToolCx<'_>) -> Result<()> {
let watch = super::progress::Watch::new(&cx.steps);
watch.pulling();
let sink = |stream: Stream, line: &str| {
watch.see(line);
(cx.sink)(stream, line);
};
let mut call = cx.call(PULL_LIMIT);
call.sink = &sink;
let project = Self::project(cx);
let mut waits = RETRY_AFTER.iter();
loop {
match compose::pull(&call, &project, PULL_STALL).await {
Ok(()) => return Ok(()),
Err(
e @ CliError::Failed {
failure: Failure::PullTransient(_),
..
},
) if !compose::engine_answers(&cx.read(ASK_ENGINE)).await => {
return checked(cx, Err(e), |e| engine_error(STEP, e)).await;
}
Err(CliError::Failed {
failure: Failure::PullTransient(reason),
..
}) => {
let Some(wait) = waits.next() else {
return Err(Error::DownloadFailed {
what: DownloadKind::Images,
step: STEP.to_owned(),
reason,
});
};
let said = format!("an image's pull was cut short ({reason}): trying again in {}s", wait.as_secs());
(cx.warn)(WarningCode::Retrying, &said);
tokio::time::sleep(*wait).await;
}
Err(CliError::Failed {
failure: Failure::PullRefused(reason),
..
}) => {
return Err(Error::DownloadFailed {
what: DownloadKind::Images,
step: STEP.to_owned(),
reason,
});
}
Err(e) => return Err(engine_error(STEP, e)),
}
}
}
}