11use crate :: schema:: { crates, versions} ;
22use crate :: storage:: FeedId ;
3- use crate :: tasks:: spawn_blocking;
4- use crate :: util:: diesel:: Conn ;
53use crate :: worker:: Environment ;
64use chrono:: Duration ;
75use crates_io_worker:: BackgroundJob ;
86use diesel:: prelude:: * ;
9- use diesel_async:: async_connection_wrapper :: AsyncConnectionWrapper ;
7+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
108use std:: sync:: Arc ;
119
1210/// Items younger than this will always be included in the feed.
@@ -42,16 +40,9 @@ impl BackgroundJob for SyncCrateFeed {
4240 let domain = & ctx. config . domain_name ;
4341
4442 info ! ( "Loading latest {NUM_ITEMS} version updates for `{name}` from the database…" ) ;
45- let conn = ctx. deadpool . get ( ) . await ?;
46-
47- let version_updates = spawn_blocking ( {
48- let name = name. clone ( ) ;
49- move || {
50- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
51- Ok :: < _ , anyhow:: Error > ( load_version_updates ( & name, conn) ?)
52- }
53- } )
54- . await ?;
43+ let mut conn = ctx. deadpool . get ( ) . await ?;
44+
45+ let version_updates = load_version_updates ( name, & mut conn) . await ?;
5546
5647 let feed_id = FeedId :: Crate { name } ;
5748
@@ -102,7 +93,10 @@ impl BackgroundJob for SyncCrateFeed {
10293/// than [`ALWAYS_INCLUDE_AGE`]. If there are less than [`NUM_ITEMS`] versions
10394/// then the list will be padded with older versions until [`NUM_ITEMS`] are
10495/// returned.
105- fn load_version_updates ( name : & str , conn : & mut impl Conn ) -> QueryResult < Vec < VersionUpdate > > {
96+ async fn load_version_updates (
97+ name : & str ,
98+ conn : & mut AsyncPgConnection ,
99+ ) -> QueryResult < Vec < VersionUpdate > > {
106100 let threshold_dt = chrono:: Utc :: now ( ) . naive_utc ( ) - ALWAYS_INCLUDE_AGE ;
107101
108102 let updates = versions:: table
@@ -111,7 +105,8 @@ fn load_version_updates(name: &str, conn: &mut impl Conn) -> QueryResult<Vec<Ver
111105 . filter ( versions:: created_at. gt ( threshold_dt) )
112106 . order ( versions:: created_at. desc ( ) )
113107 . select ( VersionUpdate :: as_select ( ) )
114- . load ( conn) ?;
108+ . load ( conn)
109+ . await ?;
115110
116111 let num_updates = updates. len ( ) ;
117112 if num_updates as i64 >= NUM_ITEMS {
@@ -125,6 +120,7 @@ fn load_version_updates(name: &str, conn: &mut impl Conn) -> QueryResult<Vec<Ver
125120 . select ( VersionUpdate :: as_select ( ) )
126121 . limit ( NUM_ITEMS )
127122 . load ( conn)
123+ . await
128124}
129125
130126#[ derive( Debug , Queryable , Selectable ) ]
@@ -183,65 +179,67 @@ mod tests {
183179 use super :: * ;
184180 use chrono:: NaiveDateTime ;
185181 use crates_io_test_db:: TestDatabase ;
182+ use diesel_async:: AsyncConnection ;
186183 use insta:: assert_debug_snapshot;
187184
188- #[ test]
189- fn test_load_version_updates ( ) {
185+ #[ tokio :: test]
186+ async fn test_load_version_updates ( ) {
190187 crate :: util:: tracing:: init_for_test ( ) ;
191188
192189 let db = TestDatabase :: new ( ) ;
193- let mut conn = db. connect ( ) ;
190+ let mut conn = AsyncPgConnection :: establish ( db. url ( ) ) . await . unwrap ( ) ;
194191
195192 let now = chrono:: Utc :: now ( ) . naive_utc ( ) ;
196193
197- let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) ) ;
194+ let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) . await ) ;
198195 assert_eq ! ( updates. len( ) , 0 ) ;
199196
200- let foo = create_crate ( & mut conn, "foo" ) ;
197+ let foo = create_crate ( & mut conn, "foo" ) . await ;
201198
202199 // If there are less than NUM_ITEMS versions, they should all be returned
203- create_version ( & mut conn, foo, "1.0.0" , now - Duration :: days ( 123 ) ) ;
204- create_version ( & mut conn, foo, "1.0.1" , now - Duration :: days ( 110 ) ) ;
205- create_version ( & mut conn, foo, "1.1.0" , now - Duration :: days ( 100 ) ) ;
206- create_version ( & mut conn, foo, "1.2.0" , now - Duration :: days ( 90 ) ) ;
200+ create_version ( & mut conn, foo, "1.0.0" , now - Duration :: days ( 123 ) ) . await ;
201+ create_version ( & mut conn, foo, "1.0.1" , now - Duration :: days ( 110 ) ) . await ;
202+ create_version ( & mut conn, foo, "1.1.0" , now - Duration :: days ( 100 ) ) . await ;
203+ create_version ( & mut conn, foo, "1.2.0" , now - Duration :: days ( 90 ) ) . await ;
207204
208- let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) ) ;
205+ let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) . await ) ;
209206 assert_eq ! ( updates. len( ) , 4 ) ;
210207 assert_debug_snapshot ! ( updates. iter( ) . map( |u| & u. version) . collect:: <Vec <_>>( ) ) ;
211208
212209 // If there are more than NUM_ITEMS versions, only the most recent NUM_ITEMS should be returned
213210 for i in 1 ..=NUM_ITEMS {
214211 let version = format ! ( "1.2.{i}" ) ;
215212 let publish_time = now - Duration :: days ( 90 ) + Duration :: hours ( i) ;
216- create_version ( & mut conn, foo, & version, publish_time) ;
213+ create_version ( & mut conn, foo, & version, publish_time) . await ;
217214 }
218215
219- let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) ) ;
216+ let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) . await ) ;
220217 assert_eq ! ( updates. len( ) as i64 , NUM_ITEMS ) ;
221218 assert_debug_snapshot ! ( updates. iter( ) . map( |u| & u. version) . collect:: <Vec <_>>( ) ) ;
222219
223220 // But if there are more than NUM_ITEMS versions that are younger than ALWAYS_INCLUDE_AGE, all of them should be returned
224221 for i in 1 ..=( NUM_ITEMS + 10 ) {
225222 let version = format ! ( "1.3.{i}" ) ;
226223 let publish_time = now - Duration :: minutes ( 30 ) + Duration :: seconds ( i) ;
227- create_version ( & mut conn, foo, & version, publish_time) ;
224+ create_version ( & mut conn, foo, & version, publish_time) . await ;
228225 }
229226
230- let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) ) ;
227+ let updates = assert_ok ! ( load_version_updates( "foo" , & mut conn) . await ) ;
231228 assert_eq ! ( updates. len( ) as i64 , NUM_ITEMS + 10 ) ;
232229 assert_debug_snapshot ! ( updates. iter( ) . map( |u| & u. version) . collect:: <Vec <_>>( ) ) ;
233230 }
234231
235- fn create_crate ( conn : & mut impl Conn , name : & str ) -> i32 {
232+ async fn create_crate ( conn : & mut AsyncPgConnection , name : & str ) -> i32 {
236233 diesel:: insert_into ( crates:: table)
237234 . values ( ( crates:: name. eq ( name) , ) )
238235 . returning ( crates:: id)
239236 . get_result ( conn)
237+ . await
240238 . unwrap ( )
241239 }
242240
243- fn create_version (
244- conn : & mut impl Conn ,
241+ async fn create_version (
242+ conn : & mut AsyncPgConnection ,
245243 crate_id : i32 ,
246244 version : & str ,
247245 publish_time : NaiveDateTime ,
@@ -256,6 +254,7 @@ mod tests {
256254 ) )
257255 . returning ( versions:: id)
258256 . get_result ( conn)
257+ . await
259258 . unwrap ( )
260259 }
261260}
0 commit comments