Skip to content

jobs/send_publish_notifications: Add logging calls #9357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024
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
43 changes: 34 additions & 9 deletions src/worker/jobs/send_publish_notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ impl BackgroundJob for SendPublishNotificationsJob {
type Context = Arc<Environment>;

async fn run(&self, ctx: Self::Context) -> anyhow::Result<()> {
let version_id = self.version_id;

info!("Sending publish notifications for version {version_id}…");

let mut conn = ctx.deadpool.get().await?;

// Get crate name, version and other publish details
let publish_details = PublishDetails::for_version(self.version_id, &mut conn).await?;
let publish_details = PublishDetails::for_version(version_id, &mut conn).await?;

let publish_time = publish_details
.publish_time
Expand All @@ -51,6 +55,8 @@ impl BackgroundJob for SendPublishNotificationsJob {
.load::<(String, String)>(&mut conn)
.await?;

let num_recipients = recipients.len();

// Sending emails is currently a blocking operation, so we have to use
// `spawn_blocking()` to run it in a separate thread.
spawn_blocking(move || {
Expand Down Expand Up @@ -80,22 +86,41 @@ impl BackgroundJob for SendPublishNotificationsJob {
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<_>>();

// Check if any of the emails succeeded to send, in which case we
// consider the job successful enough and not worth retrying.
match results.iter().any(|result| result.is_ok()) {
true => Ok(()),
false => Err(anyhow!("Failed to send publish notifications")),
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
);
}
})
.await?;

Ok(())
Ok(())
})
.await
}
}

Expand Down