Skip to content

Commit 7204e92

Browse files
committed
Skip running a job if the crate/version deleted
1 parent 0f6c8f9 commit 7204e92

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/worker/jobs/readmes.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use diesel_async::AsyncConnection;
99
use diesel_async::scoped_futures::ScopedFutureExt;
1010
use serde::{Deserialize, Serialize};
1111
use std::sync::Arc;
12-
use tracing::{info, instrument};
12+
use tracing::{info, instrument, warn};
1313

1414
#[derive(Clone, Serialize, Deserialize)]
1515
pub struct RenderAndUploadReadme {
@@ -70,13 +70,31 @@ impl BackgroundJob for RenderAndUploadReadme {
7070
let mut conn = env.deadpool.get().await?;
7171
conn.transaction(|conn| {
7272
async move {
73-
Version::record_readme_rendering(job.version_id, conn).await?;
74-
let (crate_name, vers): (String, String) = versions::table
75-
.find(job.version_id)
76-
.inner_join(crates::table)
77-
.select((crates::name, versions::num))
78-
.first(conn)
79-
.await?;
73+
let (crate_name, vers): (String, String) =
74+
match Version::record_readme_rendering(job.version_id, conn)
75+
.await
76+
.and(
77+
versions::table
78+
.find(job.version_id)
79+
.inner_join(crates::table)
80+
.select((crates::name, versions::num))
81+
.first(conn)
82+
.await,
83+
) {
84+
Ok(r) => r,
85+
Err(diesel::result::Error::DatabaseError(
86+
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
87+
..,
88+
))
89+
| Err(diesel::result::Error::NotFound) => {
90+
warn!(
91+
"Skipping rendering README for vesion {}: no version found",
92+
job.version_id
93+
);
94+
return Ok(());
95+
}
96+
Err(err) => return Err(err.into()),
97+
};
8098

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

src/worker/jobs/send_publish_notifications.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ impl BackgroundJob for SendPublishNotificationsJob {
3939
let mut conn = ctx.deadpool.get().await?;
4040

4141
// Get crate name, version and other publish details
42-
let publish_details = PublishDetails::for_version(version_id, &mut conn).await?;
42+
let Some(publish_details) = PublishDetails::for_version(version_id, &mut conn).await?
43+
else {
44+
warn!("Skipping publish notifications for {version_id}: no version found",);
45+
46+
return Ok(());
47+
};
4348

4449
let publish_time = publish_details
4550
.publish_time
@@ -157,13 +162,17 @@ struct PublishDetails {
157162
}
158163

159164
impl PublishDetails {
160-
async fn for_version(version_id: i32, conn: &mut AsyncPgConnection) -> QueryResult<Self> {
165+
async fn for_version(
166+
version_id: i32,
167+
conn: &mut AsyncPgConnection,
168+
) -> QueryResult<Option<Self>> {
161169
versions::table
162170
.find(version_id)
163171
.inner_join(crates::table)
164172
.left_join(users::table)
165173
.select(PublishDetails::as_select())
166174
.first(conn)
167175
.await
176+
.optional()
168177
}
169178
}

src/worker/jobs/update_default_version.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::worker::Environment;
33
use crates_io_worker::BackgroundJob;
44
use serde::{Deserialize, Serialize};
55
use std::sync::Arc;
6-
use tracing::info;
6+
use tracing::{info, warn};
77

88
#[derive(Serialize, Deserialize)]
99
pub struct UpdateDefaultVersion {
@@ -28,8 +28,16 @@ impl BackgroundJob for UpdateDefaultVersion {
2828

2929
info!("Updating default version for crate {crate_id}");
3030
let mut conn = ctx.deadpool.get().await?;
31-
update_default_version(crate_id, &mut conn).await?;
31+
let res = update_default_version(crate_id, &mut conn).await;
32+
if let Err(diesel::result::Error::DatabaseError(
33+
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
34+
..,
35+
)) = res
36+
{
37+
warn!("Skipping update default version for crate for {crate_id}: no crate found",);
38+
return Ok(());
39+
}
3240

33-
Ok(())
41+
res.map_err(|e| e.into())
3442
}
3543
}

0 commit comments

Comments
 (0)