From 9c44de5d9a914acf6ab1bfc2c0949ba69687bacd Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Wed, 13 Nov 2024 09:21:00 +0100 Subject: [PATCH] admin/upload_index: Reduce `spawn_blocking()` scope --- src/bin/crates-admin/upload_index.rs | 72 ++++++++++++++-------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/src/bin/crates-admin/upload_index.rs b/src/bin/crates-admin/upload_index.rs index d2cba59ee57..7cc6188944f 100644 --- a/src/bin/crates-admin/upload_index.rs +++ b/src/bin/crates-admin/upload_index.rs @@ -4,7 +4,6 @@ use crates_io::storage::Storage; use crates_io::tasks::spawn_blocking; use crates_io_index::{Repository, RepositoryConfig}; use indicatif::{ProgressBar, ProgressIterator, ProgressStyle}; -use tokio::runtime::Handle; #[derive(clap::Parser, Debug)] #[command( @@ -17,52 +16,55 @@ pub struct Opts { } pub async fn run(opts: Opts) -> anyhow::Result<()> { - spawn_blocking(move || { - let storage = Storage::from_environment(); + let storage = Storage::from_environment(); - println!("fetching git repo"); - let config = RepositoryConfig::from_environment()?; + println!("fetching git repo"); + let config = RepositoryConfig::from_environment()?; + let (repo, files) = spawn_blocking(move || { let repo = Repository::open(&config)?; repo.reset_head()?; println!("HEAD is at {}", repo.head_oid()?); let files = repo.get_files_modified_since(opts.incremental_commit.as_deref())?; println!("found {} files to upload", files.len()); - if !dialoguer::confirm("continue with upload?")? { - return Ok(()); - } - let pb = ProgressBar::new(files.len() as u64); - pb.set_style(ProgressStyle::with_template( - "{bar:60} ({pos}/{len}, ETA {eta})", - )?); + Ok::<_, anyhow::Error>((repo, files)) + }) + .await?; + + if !dialoguer::async_confirm("continue with upload?").await? { + return Ok(()); + } - for file in files.iter().progress_with(pb.clone()) { - let file_name = file.file_name().ok_or_else(|| { - let file = file.display(); - anyhow!("Failed to get file name from path: {file}") - })?; + let pb = ProgressBar::new(files.len() as u64); + pb.set_style(ProgressStyle::with_template( + "{bar:60} ({pos}/{len}, ETA {eta})", + )?); - let crate_name = file_name.to_str().ok_or_else(|| { - let file_name = file_name.to_string_lossy(); - anyhow!("Failed to convert file name to utf8: {file_name}",) - })?; + for file in files.iter().progress_with(pb.clone()) { + let file_name = file.file_name().ok_or_else(|| { + let file = file.display(); + anyhow!("Failed to get file name from path: {file}") + })?; - let path = repo.index_file(crate_name); - if !path.exists() { - pb.suspend(|| println!("skipping file `{crate_name}`")); - continue; - } + let crate_name = file_name.to_str().ok_or_else(|| { + let file_name = file_name.to_string_lossy(); + anyhow!("Failed to convert file name to utf8: {file_name}",) + })?; - let contents = std::fs::read_to_string(&path)?; - Handle::current().block_on(storage.sync_index(crate_name, Some(contents)))?; + let path = repo.index_file(crate_name); + if !path.exists() { + pb.suspend(|| println!("skipping file `{crate_name}`")); + continue; } - println!( - "uploading completed; use `upload-index {}` for an incremental run", - repo.head_oid()? - ); - Ok(()) - }) - .await + let contents = tokio::fs::read_to_string(&path).await?; + storage.sync_index(crate_name, Some(contents)).await?; + } + + println!( + "uploading completed; use `upload-index {}` for an incremental run", + repo.head_oid()? + ); + Ok(()) }