Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 60 additions & 63 deletions src/worker/jobs/send_publish_notifications.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::email::Email;
use crate::models::OwnerKind;
use crate::schema::{crate_owners, crates, emails, users, versions};
use crate::tasks::spawn_blocking;
use crate::worker::Environment;
use anyhow::anyhow;
use chrono::{NaiveDateTime, SecondsFormat};
Expand Down Expand Up @@ -69,68 +68,66 @@

// Sending emails is currently a blocking operation, so we have to use
// `spawn_blocking()` to run it in a separate thread.
spawn_blocking(move || {
let results = recipients
.into_iter()
.map(|(ref recipient, email_address)| {
let krate = &publish_details.krate;
let version = &publish_details.version;

let publisher_info = match &publish_details.publisher {
Some(publisher) if publisher == recipient => &format!(
" by your account (https://{domain}/users/{publisher})",
domain = ctx.config.domain_name
),
Some(publisher) => &format!(
" by {publisher} (https://{domain}/users/{publisher})",
domain = ctx.config.domain_name
),
None => "",
};

let email = PublishNotificationEmail {
recipient,
krate,
version,
publish_time: &publish_time,
publisher_info,
};

debug!("Sending publish notification for {krate}@{version} to {email_address}…");
ctx.emails.send(&email_address, email).inspect_err(|err| {
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")
})
})
.collect::<Vec<_>>();

let num_sent = results.iter().filter(|result| result.is_ok()).count();

// Check if *none* of the emails succeeded to send, in which case we
// consider the job failed and worth retrying.
if num_sent == 0 {
warn!(
"Failed to send publish notifications for {}@{}",
publish_details.krate, publish_details.version
);

return Err(anyhow!("Failed to send publish notifications"));
}

if num_sent == num_recipients {
info!(
"Sent {num_sent} publish notifications for {}@{}",
publish_details.krate, publish_details.version
);
} else {
warn!(
"Sent only {num_sent} of {num_recipients} publish notifications for {}@{}",
publish_details.krate, publish_details.version
);
}

Ok(())
})
.await
let mut results = Vec::with_capacity(recipients.len());

for (ref recipient, email_address) in recipients {
let krate = &publish_details.krate;
let version = &publish_details.version;

let publisher_info = match &publish_details.publisher {
Some(publisher) if publisher == recipient => &format!(
" by your account (https://{domain}/users/{publisher})",
domain = ctx.config.domain_name
),
Some(publisher) => &format!(
" by {publisher} (https://{domain}/users/{publisher})",
domain = ctx.config.domain_name
),
None => "",

Check warning on line 86 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L86

Added line #L86 was not covered by tests
};

let email = PublishNotificationEmail {
recipient,
krate,
version,
publish_time: &publish_time,
publisher_info,
};

debug!("Sending publish notification for {krate}@{version} to {email_address}…");
let result = ctx.emails.async_send(&email_address, email).await.inspect_err(|err| {
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")

Check warning on line 99 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L99

Added line #L99 was not covered by tests
});

results.push(result);
}

let num_sent = results.iter().filter(|result| result.is_ok()).count();

// Check if *none* of the emails succeeded to send, in which case we
// consider the job failed and worth retrying.
if num_sent == 0 {
warn!(
"Failed to send publish notifications for {}@{}",

Check warning on line 111 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L110-L111

Added lines #L110 - L111 were not covered by tests
publish_details.krate, publish_details.version
);

return Err(anyhow!("Failed to send publish notifications"));

Check warning on line 115 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L115

Added line #L115 was not covered by tests
}

if num_sent == num_recipients {
info!(
"Sent {num_sent} publish notifications for {}@{}",

Check warning on line 120 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L120

Added line #L120 was not covered by tests
publish_details.krate, publish_details.version
);
} else {
warn!(
"Sent only {num_sent} of {num_recipients} publish notifications for {}@{}",

Check warning on line 125 in src/worker/jobs/send_publish_notifications.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/jobs/send_publish_notifications.rs#L124-L125

Added lines #L124 - L125 were not covered by tests
publish_details.krate, publish_details.version
);
}

Ok(())
}
}

Expand Down