Skip to content

Commit

Permalink
Clear publish progress bar on retry
Browse files Browse the repository at this point in the history
Previously, we would add broken progress bars.
  • Loading branch information
konstin committed Oct 4, 2024
1 parent 312eeb8 commit 4d4c1d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
26 changes: 15 additions & 11 deletions crates/uv-publish/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ use std::{env, fmt, io};
use thiserror::Error;
use tokio::io::AsyncReadExt;
use tokio_util::io::ReaderStream;
use tracing::{debug, enabled, trace, warn, Level};
use tracing::{debug, enabled, trace, Level};
use url::Url;
use uv_client::UvRetryableStrategy;
use uv_configuration::{KeyringProviderType, TrustedPublishing};
use uv_distribution_filename::{DistFilename, SourceDistExtension, SourceDistFilename};
use uv_fs::{ProgressReader, Simplified};
use uv_metadata::read_metadata_async_seek;
use uv_pypi_types::{Metadata23, MetadataError};
use uv_warnings::warn_user_once;
use uv_warnings::{warn_user, warn_user_once};

pub use trusted_publishing::TrustedPublishingToken;

Expand Down Expand Up @@ -95,7 +95,7 @@ pub trait Reporter: Send + Sync + 'static {
fn on_progress(&self, name: &str, id: usize);
fn on_download_start(&self, name: &str, size: Option<u64>) -> usize;
fn on_download_progress(&self, id: usize, inc: u64);
fn on_download_complete(&self);
fn on_download_complete(&self, id: usize);
}

impl PublishSendError {
Expand Down Expand Up @@ -298,7 +298,7 @@ pub async fn upload(
let mut attempt = 0;
loop {
attempt += 1;
let request = build_request(
let (request, idx) = build_request(
file,
filename,
registry,
Expand All @@ -312,8 +312,9 @@ pub async fn upload(
.map_err(|err| PublishError::PublishPrepare(file.to_path_buf(), Box::new(err)))?;

let result = request.send().await;
if attempt <= retries && UvRetryableStrategy.handle(&result) == Some(Retryable::Transient) {
warn!("Transient request failure for {}, retrying", registry);
if attempt < retries && UvRetryableStrategy.handle(&result) == Some(Retryable::Transient) {
reporter.on_download_complete(idx);
warn_user!("Transient request failure for {}, retrying", registry);
continue;
}

Expand Down Expand Up @@ -478,6 +479,9 @@ async fn form_metadata(
Ok(form_metadata)
}

/// Build the upload request.
///
/// Returns the request and the reporter progress bar id.
async fn build_request(
file: &Path,
filename: &DistFilename,
Expand All @@ -487,7 +491,7 @@ async fn build_request(
password: Option<&str>,
form_metadata: &[(&'static str, String)],
reporter: Arc<impl Reporter>,
) -> Result<RequestBuilder, PublishPrepareError> {
) -> Result<(RequestBuilder, usize), PublishPrepareError> {
let mut form = reqwest::multipart::Form::new();
for (key, value) in form_metadata {
form = form.text(*key, value.clone());
Expand Down Expand Up @@ -534,7 +538,7 @@ async fn build_request(
let credentials = BASE64_STANDARD.encode(format!("{username}:{password}"));
request = request.header(AUTHORIZATION, format!("Basic {credentials}"));
}
Ok(request)
Ok((request, idx))
}

/// Returns `true` if the file was newly uploaded and `false` if it already existed.
Expand Down Expand Up @@ -636,7 +640,7 @@ mod tests {
0
}
fn on_download_progress(&self, _id: usize, _inc: u64) {}
fn on_download_complete(&self) {}
fn on_download_complete(&self, _id: usize) {}
}

/// Snapshot the data we send for an upload request for a source distribution.
Expand Down Expand Up @@ -700,7 +704,7 @@ mod tests {
project_urls: Source, https://github.com/unknown/tqdm
"###);

let request = build_request(
let (request, _) = build_request(
&file,
&filename,
&Url::parse("https://example.org/upload").unwrap(),
Expand Down Expand Up @@ -843,7 +847,7 @@ mod tests {
project_urls: wiki, https://github.com/tqdm/tqdm/wiki
"###);

let request = build_request(
let (request, _) = build_request(
&file,
&filename,
&Url::parse("https://example.org/upload").unwrap(),
Expand Down
5 changes: 2 additions & 3 deletions crates/uv/src/commands/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,8 @@ impl uv_publish::Reporter for PublishReporter {
self.reporter.on_download_progress(id, inc);
}

fn on_download_complete(&self) {
self.reporter.root.set_message("");
self.reporter.root.finish_and_clear();
fn on_download_complete(&self, id: usize) {
self.reporter.on_download_complete(id);
}
}

Expand Down

0 comments on commit 4d4c1d1

Please sign in to comment.