Skip to content

Commit 5b08d3d

Browse files
authored
Merge pull request #9598 from Turbo87/async-delete-version
admin/delete_version: Reduce `spawn_blocking()` usage
2 parents 16823fc + 9d1f7c3 commit 5b08d3d

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

src/admin/delete_version.rs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::tasks::spawn_blocking;
55
use crate::worker::jobs;
66
use crate::{admin::dialoguer, db, schema::versions};
77
use anyhow::Context;
8-
use diesel::prelude::*;
8+
use diesel::{Connection, ExpressionMethods, QueryDsl};
9+
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
910

1011
#[derive(clap::Parser, Debug)]
1112
#[command(
@@ -27,18 +28,25 @@ pub struct Opts {
2728
}
2829

2930
pub async fn run(opts: Opts) -> anyhow::Result<()> {
30-
spawn_blocking(move || {
31-
let crate_name = &opts.crate_name;
31+
let mut conn = db::oneoff_async_connection()
32+
.await
33+
.context("Failed to establish database connection")?;
3234

33-
let conn = &mut db::oneoff_connection().context("Failed to establish database connection")?;
35+
let store = Storage::from_environment();
3436

35-
let store = Storage::from_environment();
37+
let crate_id: i32 = {
38+
use diesel_async::RunQueryDsl;
3639

37-
let crate_id: i32 = crates::table
40+
crates::table
3841
.select(crates::id)
39-
.filter(crates::name.eq(crate_name))
40-
.first(conn)
41-
.context("Failed to look up crate id from the database")?;
42+
.filter(crates::name.eq(&opts.crate_name))
43+
.first(&mut conn)
44+
.await
45+
.context("Failed to look up crate id from the database")
46+
}?;
47+
48+
{
49+
let crate_name = &opts.crate_name;
4250

4351
println!("Deleting the following versions of the `{crate_name}` crate:");
4452
println!();
@@ -47,9 +55,20 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
4755
}
4856
println!();
4957

50-
if !opts.yes && !dialoguer::confirm("Do you want to permanently delete these versions?")? {
58+
if !opts.yes
59+
&& !dialoguer::async_confirm("Do you want to permanently delete these versions?")
60+
.await?
61+
{
5162
return Ok(());
5263
}
64+
}
65+
66+
let opts = spawn_blocking::<_, _, anyhow::Error>(move || {
67+
use diesel::RunQueryDsl;
68+
69+
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();
70+
71+
let crate_name = &opts.crate_name;
5372

5473
conn.transaction(|conn| {
5574
info!(%crate_name, %crate_id, versions = ?opts.versions, "Deleting versions from the database");
@@ -87,27 +106,26 @@ pub async fn run(opts: Opts) -> anyhow::Result<()> {
87106
warn!(%crate_name, ?error, "Failed to enqueue index sync jobs");
88107
}
89108

90-
let rt = tokio::runtime::Builder::new_current_thread()
91-
.enable_all()
92-
.build()
93-
.context("Failed to initialize tokio runtime")?;
109+
Ok(opts)
110+
}).await?;
94111

95-
for version in &opts.versions {
96-
debug!(%crate_name, %version, "Deleting crate file from S3");
97-
if let Err(error) = rt.block_on(store.delete_crate_file(crate_name, version)) {
98-
warn!(%crate_name, %version, ?error, "Failed to delete crate file from S3");
99-
}
112+
let crate_name = &opts.crate_name;
100113

101-
debug!(%crate_name, %version, "Deleting readme file from S3");
102-
match rt.block_on(store.delete_readme(crate_name, version)) {
103-
Err(object_store::Error::NotFound { .. }) => {}
104-
Err(error) => {
105-
warn!(%crate_name, %version, ?error, "Failed to delete readme file from S3")
106-
}
107-
Ok(_) => {}
114+
for version in &opts.versions {
115+
debug!(%crate_name, %version, "Deleting crate file from S3");
116+
if let Err(error) = store.delete_crate_file(crate_name, version).await {
117+
warn!(%crate_name, %version, ?error, "Failed to delete crate file from S3");
118+
}
119+
120+
debug!(%crate_name, %version, "Deleting readme file from S3");
121+
match store.delete_readme(crate_name, version).await {
122+
Err(object_store::Error::NotFound { .. }) => {}
123+
Err(error) => {
124+
warn!(%crate_name, %version, ?error, "Failed to delete readme file from S3")
108125
}
126+
Ok(_) => {}
109127
}
128+
}
110129

111-
Ok(())
112-
}).await
130+
Ok(())
113131
}

src/admin/dialoguer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
use crate::tasks::spawn_blocking;
12
use ::dialoguer::{theme::Theme, Confirm};
23

3-
pub fn confirm(msg: &str) -> dialoguer::Result<bool> {
4+
pub async fn async_confirm(msg: impl Into<String>) -> anyhow::Result<bool> {
5+
let msg = msg.into();
6+
spawn_blocking(move || confirm(msg).map_err(anyhow::Error::from)).await
7+
}
8+
9+
pub fn confirm(msg: impl Into<String>) -> dialoguer::Result<bool> {
410
Confirm::with_theme(&CustomTheme)
511
.with_prompt(msg)
612
.default(false)

0 commit comments

Comments
 (0)