Skip to content

Skip running a job if the crate/version deleted #11500

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/tests/worker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod git;
mod readmes;
mod rss;
mod send_publish_notifications;
mod sync_admins;
mod update_default_version;
17 changes: 17 additions & 0 deletions src/tests/worker/readmes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::tests::util::TestApp;
use crate::worker::jobs;
use crates_io_worker::BackgroundJob;

#[tokio::test(flavor = "multi_thread")]
async fn skips_when_crate_deleted() -> anyhow::Result<()> {
let (app, _) = TestApp::full().empty().await;
let mut conn = app.db_conn().await;

let job =
jobs::RenderAndUploadReadme::new(-1, "deleted".to_string(), ".".to_string(), None, None);

job.enqueue(&mut conn).await?;
app.run_pending_background_jobs().await;

Ok(())
}
16 changes: 16 additions & 0 deletions src/tests/worker/send_publish_notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::tests::util::TestApp;
use crate::worker::jobs;
use crates_io_worker::BackgroundJob;

#[tokio::test(flavor = "multi_thread")]
async fn skips_when_crate_deleted() -> anyhow::Result<()> {
let (app, _) = TestApp::full().empty().await;
let mut conn = app.db_conn().await;

let job = jobs::SendPublishNotificationsJob::new(-1);

job.enqueue(&mut conn).await?;
app.run_pending_background_jobs().await;

Ok(())
}
16 changes: 16 additions & 0 deletions src/tests/worker/update_default_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::tests::util::TestApp;
use crate::worker::jobs;
use crates_io_worker::BackgroundJob;

#[tokio::test(flavor = "multi_thread")]
async fn skips_when_crate_deleted() -> anyhow::Result<()> {
let (app, _) = TestApp::full().empty().await;
let mut conn = app.db_conn().await;

let job = jobs::UpdateDefaultVersion::new(-1);

job.enqueue(&mut conn).await?;
app.run_pending_background_jobs().await;

Ok(())
}
34 changes: 26 additions & 8 deletions src/worker/jobs/readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use diesel_async::AsyncConnection;
use diesel_async::scoped_futures::ScopedFutureExt;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::{info, instrument};
use tracing::{info, instrument, warn};

#[derive(Clone, Serialize, Deserialize)]
pub struct RenderAndUploadReadme {
Expand Down Expand Up @@ -70,13 +70,31 @@ impl BackgroundJob for RenderAndUploadReadme {
let mut conn = env.deadpool.get().await?;
conn.transaction(|conn| {
async move {
Version::record_readme_rendering(job.version_id, conn).await?;
let (crate_name, vers): (String, String) = versions::table
.find(job.version_id)
.inner_join(crates::table)
.select((crates::name, versions::num))
.first(conn)
.await?;
let (crate_name, vers): (String, String) =
match Version::record_readme_rendering(job.version_id, conn)
.await
.and(
versions::table
.find(job.version_id)
.inner_join(crates::table)
.select((crates::name, versions::num))
.first(conn)
.await,
) {
Ok(r) => r,
Err(diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
..,
))
| Err(diesel::result::Error::NotFound) => {
warn!(
"Skipping rendering README for version {}: no version found",
job.version_id
);
return Ok(());
}
Err(err) => return Err(err.into()),
};

tracing::Span::current().record("krate.name", tracing::field::display(&crate_name));

Expand Down
13 changes: 11 additions & 2 deletions src/worker/jobs/send_publish_notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ impl BackgroundJob for SendPublishNotificationsJob {
let mut conn = ctx.deadpool.get().await?;

// Get crate name, version and other publish details
let publish_details = PublishDetails::for_version(version_id, &mut conn).await?;
let Some(publish_details) = PublishDetails::for_version(version_id, &mut conn).await?
else {
warn!("Skipping publish notifications for {version_id}: no version found");

return Ok(());
};

let publish_time = publish_details
.publish_time
Expand Down Expand Up @@ -157,13 +162,17 @@ struct PublishDetails {
}

impl PublishDetails {
async fn for_version(version_id: i32, conn: &mut AsyncPgConnection) -> QueryResult<Self> {
async fn for_version(
version_id: i32,
conn: &mut AsyncPgConnection,
) -> QueryResult<Option<Self>> {
versions::table
.find(version_id)
.inner_join(crates::table)
.left_join(users::table)
.select(PublishDetails::as_select())
.first(conn)
.await
.optional()
}
}
27 changes: 18 additions & 9 deletions src/worker/jobs/update_default_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::info;
use tracing::{info, warn};

#[derive(Serialize, Deserialize)]
pub struct UpdateDefaultVersion {
Expand All @@ -32,16 +32,25 @@ impl BackgroundJob for UpdateDefaultVersion {

info!("Updating default version for crate {crate_id}");
let mut conn = ctx.deadpool.get().await?;
update_default_version(crate_id, &mut conn).await?;

// Get the crate name for OG image generation
let crate_name: String = crates::table
.filter(crates::id.eq(crate_id))
.select(crates::name)
.first(&mut conn)
.await?;
let crate_name = update_default_version(crate_id, &mut conn).await.and(
// Get the crate name for OG image generation
crates::table
.filter(crates::id.eq(crate_id))
.select(crates::name)
.first::<String>(&mut conn)
.await,
);
if let Err(diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
..,
)) = crate_name
{
warn!("Skipping update default version for crate for {crate_id}: no crate found");
return Ok(());
}

// Generate OG image after updating default version
let crate_name = crate_name?;
info!("Enqueueing OG image generation for crate {crate_name}");
GenerateOgImage::new(crate_name).enqueue(&mut conn).await?;

Expand Down
Loading