Skip to content

Commit 01625ba

Browse files
authored
worker/jobs/send_publish_notifications: Migrate to Emails::async_send() (#9965)
1 parent bbd1ba6 commit 01625ba

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

src/worker/jobs/send_publish_notifications.rs

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::email::Email;
22
use crate::models::OwnerKind;
33
use crate::schema::{crate_owners, crates, emails, users, versions};
4-
use crate::tasks::spawn_blocking;
54
use crate::worker::Environment;
65
use anyhow::anyhow;
76
use chrono::{NaiveDateTime, SecondsFormat};
@@ -69,68 +68,66 @@ impl BackgroundJob for SendPublishNotificationsJob {
6968

7069
// Sending emails is currently a blocking operation, so we have to use
7170
// `spawn_blocking()` to run it in a separate thread.
72-
spawn_blocking(move || {
73-
let results = recipients
74-
.into_iter()
75-
.map(|(ref recipient, email_address)| {
76-
let krate = &publish_details.krate;
77-
let version = &publish_details.version;
78-
79-
let publisher_info = match &publish_details.publisher {
80-
Some(publisher) if publisher == recipient => &format!(
81-
" by your account (https://{domain}/users/{publisher})",
82-
domain = ctx.config.domain_name
83-
),
84-
Some(publisher) => &format!(
85-
" by {publisher} (https://{domain}/users/{publisher})",
86-
domain = ctx.config.domain_name
87-
),
88-
None => "",
89-
};
90-
91-
let email = PublishNotificationEmail {
92-
recipient,
93-
krate,
94-
version,
95-
publish_time: &publish_time,
96-
publisher_info,
97-
};
98-
99-
debug!("Sending publish notification for {krate}@{version} to {email_address}…");
100-
ctx.emails.send(&email_address, email).inspect_err(|err| {
101-
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")
102-
})
103-
})
104-
.collect::<Vec<_>>();
105-
106-
let num_sent = results.iter().filter(|result| result.is_ok()).count();
107-
108-
// Check if *none* of the emails succeeded to send, in which case we
109-
// consider the job failed and worth retrying.
110-
if num_sent == 0 {
111-
warn!(
112-
"Failed to send publish notifications for {}@{}",
113-
publish_details.krate, publish_details.version
114-
);
115-
116-
return Err(anyhow!("Failed to send publish notifications"));
117-
}
118-
119-
if num_sent == num_recipients {
120-
info!(
121-
"Sent {num_sent} publish notifications for {}@{}",
122-
publish_details.krate, publish_details.version
123-
);
124-
} else {
125-
warn!(
126-
"Sent only {num_sent} of {num_recipients} publish notifications for {}@{}",
127-
publish_details.krate, publish_details.version
128-
);
129-
}
130-
131-
Ok(())
132-
})
133-
.await
71+
let mut results = Vec::with_capacity(recipients.len());
72+
73+
for (ref recipient, email_address) in recipients {
74+
let krate = &publish_details.krate;
75+
let version = &publish_details.version;
76+
77+
let publisher_info = match &publish_details.publisher {
78+
Some(publisher) if publisher == recipient => &format!(
79+
" by your account (https://{domain}/users/{publisher})",
80+
domain = ctx.config.domain_name
81+
),
82+
Some(publisher) => &format!(
83+
" by {publisher} (https://{domain}/users/{publisher})",
84+
domain = ctx.config.domain_name
85+
),
86+
None => "",
87+
};
88+
89+
let email = PublishNotificationEmail {
90+
recipient,
91+
krate,
92+
version,
93+
publish_time: &publish_time,
94+
publisher_info,
95+
};
96+
97+
debug!("Sending publish notification for {krate}@{version} to {email_address}…");
98+
let result = ctx.emails.async_send(&email_address, email).await.inspect_err(|err| {
99+
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")
100+
});
101+
102+
results.push(result);
103+
}
104+
105+
let num_sent = results.iter().filter(|result| result.is_ok()).count();
106+
107+
// Check if *none* of the emails succeeded to send, in which case we
108+
// consider the job failed and worth retrying.
109+
if num_sent == 0 {
110+
warn!(
111+
"Failed to send publish notifications for {}@{}",
112+
publish_details.krate, publish_details.version
113+
);
114+
115+
return Err(anyhow!("Failed to send publish notifications"));
116+
}
117+
118+
if num_sent == num_recipients {
119+
info!(
120+
"Sent {num_sent} publish notifications for {}@{}",
121+
publish_details.krate, publish_details.version
122+
);
123+
} else {
124+
warn!(
125+
"Sent only {num_sent} of {num_recipients} publish notifications for {}@{}",
126+
publish_details.krate, publish_details.version
127+
);
128+
}
129+
130+
Ok(())
134131
}
135132
}
136133

0 commit comments

Comments
 (0)