Skip to content

Commit aa967aa

Browse files
committed
Auto merge of #4175 - Turbo87:sync-yanked, r=JohnTitor
worker::git: Read `yanked` status from database This PR implements one part of #4172. By no longer passing `yanked` into the `yank` (now `sync_yanked`) background worker task, and instead reading `yanked` from the database, we can avoid issues from tasks running out of order.
2 parents 372601e + 552ae26 commit aa967aa

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/controllers/version/yank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn modify_yank(req: &mut dyn RequestExt, yanked: bool) -> EndpointResult {
6161

6262
insert_version_owner_action(&conn, version.id, user.id, api_token_id, action)?;
6363

64-
worker::yank(krate.name, version, yanked).enqueue(&conn)?;
64+
worker::sync_yanked(krate.name, version.num).enqueue(&conn)?;
6565

6666
ok_true()
6767
}

src/worker/git.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::background_jobs::Environment;
22
use crate::git::{Crate, Credentials};
3-
use crate::models::Version;
3+
use crate::schema;
4+
use anyhow::Context;
45
use chrono::Utc;
6+
use diesel::prelude::*;
57
use std::fs::{self, OpenOptions};
68
use std::io::prelude::*;
79
use swirl::PerformError;
@@ -29,12 +31,24 @@ pub fn add_crate(env: &Environment, krate: Crate) -> Result<(), PerformError> {
2931
/// `true` or `false`, write all the lines back out, and commit and
3032
/// push the changes.
3133
#[swirl::background_job]
32-
pub fn yank(
34+
pub fn sync_yanked(
3335
env: &Environment,
36+
conn: &PgConnection,
3437
krate: String,
35-
version: Version,
36-
yanked: bool,
38+
version_num: String,
3739
) -> Result<(), PerformError> {
40+
trace!(?krate, ?version_num, "Load yanked status from database");
41+
42+
let yanked: bool = schema::versions::table
43+
.inner_join(schema::crates::table)
44+
.filter(schema::crates::name.eq(&krate))
45+
.filter(schema::versions::num.eq(&version_num))
46+
.select(schema::versions::yanked)
47+
.get_result(conn)
48+
.context("Failed to load yanked status from database")?;
49+
50+
trace!(?krate, ?version_num, yanked);
51+
3852
let repo = env.lock_index()?;
3953
let dst = repo.index_file(&krate);
4054

@@ -44,24 +58,25 @@ pub fn yank(
4458
.map(|line| {
4559
let mut git_crate = serde_json::from_str::<Crate>(line)
4660
.map_err(|_| format!("couldn't decode: `{}`", line))?;
47-
if git_crate.name != krate || git_crate.vers != version.num {
61+
if git_crate.name != krate || git_crate.vers != version_num {
4862
return Ok(line.to_string());
4963
}
5064
git_crate.yanked = Some(yanked);
5165
Ok(serde_json::to_string(&git_crate)?)
5266
})
5367
.collect::<Result<Vec<_>, PerformError>>();
5468
let new = new?.join("\n") + "\n";
55-
fs::write(&dst, new.as_bytes())?;
5669

57-
let message: String = format!(
58-
"{} crate `{}#{}`",
59-
if yanked { "Yanking" } else { "Unyanking" },
60-
krate,
61-
version.num
62-
);
70+
if new != prev {
71+
fs::write(&dst, new.as_bytes())?;
6372

64-
repo.commit_and_push(&message, &dst)?;
73+
let action = if yanked { "Yanking" } else { "Unyanking" };
74+
let message = format!("{} crate `{}#{}`", action, krate, version_num);
75+
76+
repo.commit_and_push(&message, &dst)?;
77+
} else {
78+
debug!("Skipping `yanked` update because index is up-to-date");
79+
}
6580

6681
Ok(())
6782
}

src/worker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ mod update_downloads;
1111

1212
pub use daily_db_maintenance::daily_db_maintenance;
1313
pub use dump_db::dump_db;
14-
pub use git::{add_crate, squash_index, yank};
14+
pub use git::{add_crate, squash_index, sync_yanked};
1515
pub use readmes::render_and_upload_readme;
1616
pub use update_downloads::update_downloads;

0 commit comments

Comments
 (0)