1
1
use crate :: schema:: version_downloads;
2
- use crate :: tasks:: spawn_blocking;
3
- use crate :: util:: diesel:: Conn ;
4
2
use crate :: worker:: Environment ;
5
3
use crates_io_worker:: BackgroundJob ;
6
4
use diesel:: prelude:: * ;
7
5
use diesel:: sql_types:: BigInt ;
8
- use diesel_async:: async_connection_wrapper :: AsyncConnectionWrapper ;
6
+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
9
7
use std:: sync:: Arc ;
10
8
use std:: time:: { Duration , Instant } ;
11
9
@@ -19,16 +17,12 @@ impl BackgroundJob for UpdateDownloads {
19
17
type Context = Arc < Environment > ;
20
18
21
19
async fn run ( & self , env : Self :: Context ) -> anyhow:: Result < ( ) > {
22
- let conn = env. deadpool . get ( ) . await ?;
23
- spawn_blocking ( move || {
24
- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
25
- Ok ( update ( conn) ?)
26
- } )
27
- . await ?
20
+ let mut conn = env. deadpool . get ( ) . await ?;
21
+ Ok ( update ( & mut conn) . await ?)
28
22
}
29
23
}
30
24
31
- fn update ( conn : & mut impl Conn ) -> QueryResult < ( ) > {
25
+ async fn update ( conn : & mut AsyncPgConnection ) -> QueryResult < ( ) > {
32
26
use diesel:: dsl:: now;
33
27
use diesel:: select;
34
28
@@ -46,7 +40,7 @@ fn update(conn: &mut impl Conn) -> QueryResult<()> {
46
40
47
41
let start_time = Instant :: now ( ) ;
48
42
loop {
49
- let update_count = batch_update ( BATCH_SIZE , conn) ?;
43
+ let update_count = batch_update ( BATCH_SIZE , conn) . await ?;
50
44
info ! ( "Updated {update_count} versions" ) ;
51
45
if update_count < BATCH_SIZE {
52
46
break ;
@@ -67,18 +61,23 @@ fn update(conn: &mut impl Conn) -> QueryResult<()> {
67
61
. filter ( version_downloads:: date. lt ( diesel:: dsl:: date ( now) ) )
68
62
. filter ( version_downloads:: downloads. eq ( version_downloads:: counted) )
69
63
. filter ( version_downloads:: processed. eq ( false ) )
70
- . execute ( conn) ?;
64
+ . execute ( conn)
65
+ . await ?;
71
66
info ! ( "Finished freezing old version_downloads" ) ;
72
67
73
68
define_sql_function ! ( fn refresh_recent_crate_downloads( ) ) ;
74
- select ( refresh_recent_crate_downloads ( ) ) . execute ( conn) ?;
69
+
70
+ select ( refresh_recent_crate_downloads ( ) )
71
+ . execute ( conn)
72
+ . await ?;
73
+
75
74
info ! ( "Finished running refresh_recent_crate_downloads" ) ;
76
75
77
76
Ok ( ( ) )
78
77
}
79
78
80
79
#[ instrument( skip_all) ]
81
- fn batch_update ( batch_size : i64 , conn : & mut impl Conn ) -> QueryResult < i64 > {
80
+ async fn batch_update ( batch_size : i64 , conn : & mut AsyncPgConnection ) -> QueryResult < i64 > {
82
81
table ! {
83
82
/// Imaginary table to make Diesel happy when using the `sql_query` function.
84
83
sql_query_results ( count) {
@@ -97,7 +96,8 @@ fn batch_update(batch_size: i64, conn: &mut impl Conn) -> QueryResult<i64> {
97
96
98
97
let result = diesel:: sql_query ( include_str ! ( "update_metadata.sql" ) )
99
98
. bind :: < BigInt , _ > ( batch_size)
100
- . get_result :: < SqlQueryResult > ( conn) ?;
99
+ . get_result :: < SqlQueryResult > ( conn)
100
+ . await ?;
101
101
102
102
Ok ( result. count )
103
103
}
@@ -107,13 +107,16 @@ mod tests {
107
107
use super :: * ;
108
108
use crate :: models:: { Crate , NewCrate , NewUser , NewVersion , User , Version } ;
109
109
use crate :: schema:: { crate_downloads, crates, users, versions} ;
110
+ use crate :: util:: diesel:: Conn ;
110
111
use crates_io_test_db:: TestDatabase ;
112
+ use diesel_async:: AsyncConnection ;
111
113
112
- fn user ( conn : & mut impl Conn ) -> User {
114
+ async fn user ( conn : & mut AsyncPgConnection ) -> User {
113
115
let user = NewUser :: new ( 2 , "login" , None , None , "access_token" ) ;
114
116
diesel:: insert_into ( users:: table)
115
117
. values ( user)
116
118
. get_result ( conn)
119
+ . await
117
120
. unwrap ( )
118
121
}
119
122
@@ -134,57 +137,66 @@ mod tests {
134
137
( krate, version)
135
138
}
136
139
137
- #[ test]
138
- fn increment ( ) {
140
+ #[ tokio :: test]
141
+ async fn increment ( ) {
139
142
use diesel:: dsl:: * ;
140
143
141
144
let test_db = TestDatabase :: new ( ) ;
142
145
let conn = & mut test_db. connect ( ) ;
143
- let user = user ( conn) ;
146
+ let mut async_conn = test_db. async_connect ( ) . await ;
147
+
148
+ let user = user ( & mut async_conn) . await ;
144
149
let ( krate, version) = crate_and_version ( conn, user. id ) ;
145
150
insert_into ( version_downloads:: table)
146
151
. values ( version_downloads:: version_id. eq ( version. id ) )
147
- . execute ( conn)
152
+ . execute ( & mut async_conn)
153
+ . await
148
154
. unwrap ( ) ;
149
155
insert_into ( version_downloads:: table)
150
156
. values ( (
151
157
version_downloads:: version_id. eq ( version. id ) ,
152
158
version_downloads:: date. eq ( date ( now - 1 . day ( ) ) ) ,
153
159
version_downloads:: processed. eq ( true ) ,
154
160
) )
155
- . execute ( conn)
161
+ . execute ( & mut async_conn)
162
+ . await
156
163
. unwrap ( ) ;
157
164
158
- super :: update ( conn ) . unwrap ( ) ;
165
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
159
166
160
167
let version_downloads = versions:: table
161
168
. find ( version. id )
162
169
. select ( versions:: downloads)
163
- . first ( conn) ;
170
+ . first ( & mut async_conn)
171
+ . await ;
164
172
assert_eq ! ( version_downloads, Ok ( 1 ) ) ;
165
173
166
174
let crate_downloads = crate_downloads:: table
167
175
. find ( krate. id )
168
176
. select ( crate_downloads:: downloads)
169
- . first ( conn) ;
177
+ . first ( & mut async_conn)
178
+ . await ;
170
179
assert_eq ! ( crate_downloads, Ok ( 1 ) ) ;
171
180
172
- super :: update ( conn ) . unwrap ( ) ;
181
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
173
182
174
183
let version_downloads = versions:: table
175
184
. find ( version. id )
176
185
. select ( versions:: downloads)
177
- . first ( conn) ;
186
+ . first ( & mut async_conn)
187
+ . await ;
178
188
assert_eq ! ( version_downloads, Ok ( 1 ) ) ;
179
189
}
180
190
181
- #[ test]
182
- fn set_processed_true ( ) {
191
+ #[ tokio :: test]
192
+ async fn set_processed_true ( ) {
183
193
use diesel:: dsl:: * ;
184
194
185
195
let test_db = TestDatabase :: new ( ) ;
186
196
let conn = & mut test_db. connect ( ) ;
187
- let user = user ( conn) ;
197
+ let mut async_conn = test_db. async_connect ( ) . await ;
198
+
199
+ let user = user ( & mut async_conn) . await ;
188
200
let ( _, version) = crate_and_version ( conn, user. id ) ;
189
201
insert_into ( version_downloads:: table)
190
202
. values ( (
@@ -194,22 +206,26 @@ mod tests {
194
206
version_downloads:: date. eq ( date ( now - 2 . days ( ) ) ) ,
195
207
version_downloads:: processed. eq ( false ) ,
196
208
) )
197
- . execute ( conn)
209
+ . execute ( & mut async_conn)
210
+ . await
198
211
. unwrap ( ) ;
199
- super :: update ( conn ) . unwrap ( ) ;
212
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
200
213
let processed = version_downloads:: table
201
214
. filter ( version_downloads:: version_id. eq ( version. id ) )
202
215
. select ( version_downloads:: processed)
203
- . first ( conn) ;
216
+ . first ( & mut async_conn)
217
+ . await ;
204
218
assert_eq ! ( processed, Ok ( true ) ) ;
205
219
}
206
220
207
- #[ test]
208
- fn dont_process_recent_row ( ) {
221
+ #[ tokio :: test]
222
+ async fn dont_process_recent_row ( ) {
209
223
use diesel:: dsl:: * ;
210
224
let test_db = TestDatabase :: new ( ) ;
211
225
let conn = & mut test_db. connect ( ) ;
212
- let user = user ( conn) ;
226
+ let mut async_conn = test_db. async_connect ( ) . await ;
227
+
228
+ let user = user ( & mut async_conn) . await ;
213
229
let ( _, version) = crate_and_version ( conn, user. id ) ;
214
230
insert_into ( version_downloads:: table)
215
231
. values ( (
@@ -219,32 +235,38 @@ mod tests {
219
235
version_downloads:: date. eq ( date ( now) ) ,
220
236
version_downloads:: processed. eq ( false ) ,
221
237
) )
222
- . execute ( conn)
238
+ . execute ( & mut async_conn)
239
+ . await
223
240
. unwrap ( ) ;
224
- super :: update ( conn ) . unwrap ( ) ;
241
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
225
242
let processed = version_downloads:: table
226
243
. filter ( version_downloads:: version_id. eq ( version. id ) )
227
244
. select ( version_downloads:: processed)
228
- . first ( conn) ;
245
+ . first ( & mut async_conn)
246
+ . await ;
229
247
assert_eq ! ( processed, Ok ( false ) ) ;
230
248
}
231
249
232
- #[ test]
233
- fn increment_a_little ( ) {
250
+ #[ tokio :: test]
251
+ async fn increment_a_little ( ) {
234
252
use diesel:: dsl:: * ;
235
253
use diesel:: update;
236
254
237
255
let test_db = TestDatabase :: new ( ) ;
238
256
let conn = & mut test_db. connect ( ) ;
239
- let user = user ( conn) ;
257
+ let mut async_conn = test_db. async_connect ( ) . await ;
258
+
259
+ let user = user ( & mut async_conn) . await ;
240
260
let ( krate, version) = crate_and_version ( conn, user. id ) ;
241
261
update ( versions:: table)
242
262
. set ( versions:: updated_at. eq ( now - 2 . hours ( ) ) )
243
- . execute ( conn)
263
+ . execute ( & mut async_conn)
264
+ . await
244
265
. unwrap ( ) ;
245
266
update ( crates:: table)
246
267
. set ( crates:: updated_at. eq ( now - 2 . hours ( ) ) )
247
- . execute ( conn)
268
+ . execute ( & mut async_conn)
269
+ . await
248
270
. unwrap ( ) ;
249
271
insert_into ( version_downloads:: table)
250
272
. values ( (
@@ -254,70 +276,89 @@ mod tests {
254
276
version_downloads:: date. eq ( date ( now) ) ,
255
277
version_downloads:: processed. eq ( false ) ,
256
278
) )
257
- . execute ( conn)
279
+ . execute ( & mut async_conn)
280
+ . await
258
281
. unwrap ( ) ;
259
282
insert_into ( version_downloads:: table)
260
283
. values ( (
261
284
version_downloads:: version_id. eq ( version. id ) ,
262
285
version_downloads:: date. eq ( date ( now - 1 . day ( ) ) ) ,
263
286
) )
264
- . execute ( conn)
287
+ . execute ( & mut async_conn)
288
+ . await
265
289
. unwrap ( ) ;
266
290
267
- let version_before: Version = versions:: table. find ( version. id ) . first ( conn) . unwrap ( ) ;
291
+ let version_before: Version = versions:: table
292
+ . find ( version. id )
293
+ . first ( & mut async_conn)
294
+ . await
295
+ . unwrap ( ) ;
268
296
let krate_before: Crate = Crate :: all ( )
269
297
. filter ( crates:: id. eq ( krate. id ) )
270
- . first ( conn)
298
+ . first ( & mut async_conn)
299
+ . await
271
300
. unwrap ( ) ;
272
301
273
- super :: update ( conn ) . unwrap ( ) ;
302
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
274
303
275
- let version2: Version = versions:: table. find ( version. id ) . first ( conn) . unwrap ( ) ;
304
+ let version2: Version = versions:: table
305
+ . find ( version. id )
306
+ . first ( & mut async_conn)
307
+ . await
308
+ . unwrap ( ) ;
276
309
assert_eq ! ( version2. downloads, 2 ) ;
277
310
assert_eq ! ( version2. updated_at, version_before. updated_at) ;
278
311
279
312
let krate2: Crate = Crate :: all ( )
280
313
. filter ( crates:: id. eq ( krate. id ) )
281
- . first ( conn)
314
+ . first ( & mut async_conn)
315
+ . await
282
316
. unwrap ( ) ;
283
317
assert_eq ! ( krate2. updated_at, krate_before. updated_at) ;
284
318
285
319
let krate2_downloads: i64 = crate_downloads:: table
286
320
. find ( krate. id )
287
321
. select ( crate_downloads:: downloads)
288
- . first ( conn)
322
+ . first ( & mut async_conn)
323
+ . await
289
324
. unwrap ( ) ;
290
325
assert_eq ! ( krate2_downloads, 2 ) ;
291
326
292
- super :: update ( conn ) . unwrap ( ) ;
327
+ super :: update ( & mut async_conn ) . await . unwrap ( ) ;
293
328
294
- let version3: Version = versions:: table. find ( version. id ) . first ( conn) . unwrap ( ) ;
329
+ let version3: Version = versions:: table
330
+ . find ( version. id )
331
+ . first ( & mut async_conn)
332
+ . await
333
+ . unwrap ( ) ;
295
334
assert_eq ! ( version3. downloads, 2 ) ;
296
335
}
297
336
298
- #[ test]
299
- fn set_processed_no_set_updated_at ( ) {
337
+ #[ tokio :: test]
338
+ async fn set_processed_no_set_updated_at ( ) {
300
339
use diesel:: dsl:: * ;
301
340
use diesel:: update;
302
341
303
342
let test_db = TestDatabase :: new ( ) ;
304
343
let mut conn = test_db. connect ( ) ;
344
+ let mut async_conn = test_db. async_connect ( ) . await ;
345
+
346
+ let user = user ( & mut async_conn) . await ;
347
+ let ( _, version) = crate_and_version ( & mut conn, user. id ) ;
305
348
306
349
// This test is using a transaction to ensure `now` is the same for all
307
350
// queries within this test.
308
- conn. begin_test_transaction ( ) . unwrap ( ) ;
309
-
310
- let conn = & mut conn;
351
+ async_conn. begin_test_transaction ( ) . await . unwrap ( ) ;
311
352
312
- let user = user ( conn) ;
313
- let ( _, version) = crate_and_version ( conn, user. id ) ;
314
353
update ( versions:: table)
315
354
. set ( versions:: updated_at. eq ( now - 2 . days ( ) ) )
316
- . execute ( conn)
355
+ . execute ( & mut async_conn)
356
+ . await
317
357
. unwrap ( ) ;
318
358
update ( crates:: table)
319
359
. set ( crates:: updated_at. eq ( now - 2 . days ( ) ) )
320
- . execute ( conn)
360
+ . execute ( & mut async_conn)
361
+ . await
321
362
. unwrap ( ) ;
322
363
insert_into ( version_downloads:: table)
323
364
. values ( (
@@ -327,16 +368,20 @@ mod tests {
327
368
version_downloads:: date. eq ( date ( now - 2 . days ( ) ) ) ,
328
369
version_downloads:: processed. eq ( false ) ,
329
370
) )
330
- . execute ( conn)
371
+ . execute ( & mut async_conn)
372
+ . await
331
373
. unwrap ( ) ;
332
374
333
- super :: update ( conn) . unwrap ( ) ;
375
+ super :: update ( & mut async_conn) . await . unwrap ( ) ;
376
+
334
377
let versions_changed = versions:: table
335
378
. select ( versions:: updated_at. ne ( now - 2 . days ( ) ) )
336
- . get_result ( conn) ;
379
+ . get_result ( & mut async_conn)
380
+ . await ;
337
381
let crates_changed = crates:: table
338
382
. select ( crates:: updated_at. ne ( now - 2 . days ( ) ) )
339
- . get_result ( conn) ;
383
+ . get_result ( & mut async_conn)
384
+ . await ;
340
385
assert_eq ! ( versions_changed, Ok ( false ) ) ;
341
386
assert_eq ! ( crates_changed, Ok ( false ) ) ;
342
387
}
0 commit comments