Skip to content

Commit 6fcac60

Browse files
authored
worker: Extract enqueue_simple/deduplicated() fns (#9630)
1 parent 261f91e commit 6fcac60

File tree

1 file changed

+58
-39
lines changed

1 file changed

+58
-39
lines changed

crates/crates_io_worker/src/background_job.rs

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use diesel::prelude::*;
77
use diesel::sql_types::{Int2, Jsonb, Text};
88
use serde::de::DeserializeOwned;
99
use serde::Serialize;
10+
use serde_json::Value;
1011
use std::future::Future;
1112
use tracing::instrument;
1213

@@ -49,49 +50,67 @@ pub trait BackgroundJob: Serialize + DeserializeOwned + Send + Sync + 'static {
4950
fn enqueue_with_priority(
5051
&self,
5152
conn: &mut impl LoadConnection<Backend = Pg>,
52-
job_priority: i16,
53+
priority: i16,
5354
) -> Result<Option<i64>, EnqueueError> {
54-
let job_data = serde_json::to_value(self)?;
55+
let data = serde_json::to_value(self)?;
5556

5657
if Self::DEDUPLICATED {
57-
let similar_jobs = background_jobs::table
58-
.select(background_jobs::id)
59-
.filter(background_jobs::job_type.eq(Self::JOB_NAME))
60-
.filter(background_jobs::data.eq(&job_data))
61-
.filter(background_jobs::priority.eq(job_priority))
62-
.for_update()
63-
.skip_locked();
64-
65-
let deduplicated_select = diesel::select((
66-
Self::JOB_NAME.into_sql::<Text>(),
67-
(&job_data).into_sql::<Jsonb>(),
68-
job_priority.into_sql::<Int2>(),
69-
))
70-
.filter(not(exists(similar_jobs)));
71-
72-
let id = diesel::insert_into(background_jobs::table)
73-
.values(deduplicated_select)
74-
.into_columns((
75-
background_jobs::job_type,
76-
background_jobs::data,
77-
background_jobs::priority,
78-
))
79-
.returning(background_jobs::id)
80-
.get_result::<i64>(conn)
81-
.optional()?;
82-
83-
Ok(id)
58+
Ok(enqueue_deduplicated(conn, Self::JOB_NAME, &data, priority)?)
8459
} else {
85-
let id = diesel::insert_into(background_jobs::table)
86-
.values((
87-
background_jobs::job_type.eq(Self::JOB_NAME),
88-
background_jobs::data.eq(job_data),
89-
background_jobs::priority.eq(job_priority),
90-
))
91-
.returning(background_jobs::id)
92-
.get_result(conn)?;
93-
94-
Ok(Some(id))
60+
Ok(Some(enqueue_simple(conn, Self::JOB_NAME, &data, priority)?))
9561
}
9662
}
9763
}
64+
65+
fn enqueue_deduplicated(
66+
conn: &mut impl LoadConnection<Backend = Pg>,
67+
job_type: &str,
68+
data: &Value,
69+
priority: i16,
70+
) -> Result<Option<i64>, EnqueueError> {
71+
let similar_jobs = background_jobs::table
72+
.select(background_jobs::id)
73+
.filter(background_jobs::job_type.eq(job_type))
74+
.filter(background_jobs::data.eq(data))
75+
.filter(background_jobs::priority.eq(priority))
76+
.for_update()
77+
.skip_locked();
78+
79+
let deduplicated_select = diesel::select((
80+
job_type.into_sql::<Text>(),
81+
data.into_sql::<Jsonb>(),
82+
priority.into_sql::<Int2>(),
83+
))
84+
.filter(not(exists(similar_jobs)));
85+
86+
let id = diesel::insert_into(background_jobs::table)
87+
.values(deduplicated_select)
88+
.into_columns((
89+
background_jobs::job_type,
90+
background_jobs::data,
91+
background_jobs::priority,
92+
))
93+
.returning(background_jobs::id)
94+
.get_result::<i64>(conn)
95+
.optional()?;
96+
97+
Ok(id)
98+
}
99+
100+
fn enqueue_simple(
101+
conn: &mut impl LoadConnection<Backend = Pg>,
102+
job_type: &str,
103+
data: &Value,
104+
priority: i16,
105+
) -> Result<i64, EnqueueError> {
106+
let id = diesel::insert_into(background_jobs::table)
107+
.values((
108+
background_jobs::job_type.eq(job_type),
109+
background_jobs::data.eq(data),
110+
background_jobs::priority.eq(priority),
111+
))
112+
.returning(background_jobs::id)
113+
.get_result(conn)?;
114+
115+
Ok(id)
116+
}

0 commit comments

Comments
 (0)