@@ -11,18 +11,16 @@ use crates_io::{admin::on_call, db, schema::*};
1111use crates_io_env_vars:: { var, var_parsed} ;
1212use crates_io_worker:: BackgroundJob ;
1313use diesel:: prelude:: * ;
14+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
1415
1516#[ tokio:: main]
1617async fn main ( ) -> Result < ( ) > {
17- spawn_blocking ( move || {
18- let conn = & mut db:: oneoff_connection ( ) ?;
18+ let conn = & mut db:: oneoff_async_connection ( ) . await ?;
1919
20- check_failing_background_jobs ( conn) ?;
21- check_stalled_update_downloads ( conn) ?;
22- check_spam_attack ( conn) ?;
23- Ok ( ( ) )
24- } )
25- . await
20+ check_failing_background_jobs ( conn) . await ?;
21+ check_stalled_update_downloads ( conn) . await ?;
22+ check_spam_attack ( conn) . await ?;
23+ Ok ( ( ) )
2624}
2725
2826/// Check for old background jobs that are not currently running.
@@ -33,7 +31,7 @@ async fn main() -> Result<()> {
3331///
3432/// Within the default 15 minute time, a job should have already had several
3533/// failed retry attempts.
36- fn check_failing_background_jobs ( conn : & mut PgConnection ) -> Result < ( ) > {
34+ async fn check_failing_background_jobs ( conn : & mut AsyncPgConnection ) -> Result < ( ) > {
3735 use diesel:: dsl:: * ;
3836 use diesel:: sql_types:: Integer ;
3937
@@ -50,7 +48,8 @@ fn check_failing_background_jobs(conn: &mut PgConnection) -> Result<()> {
5048 . filter ( background_jobs:: priority. ge ( 0 ) )
5149 . for_update ( )
5250 . skip_locked ( )
53- . load ( conn) ?;
51+ . load ( conn)
52+ . await ?;
5453
5554 let stalled_job_count = stalled_jobs. len ( ) ;
5655
@@ -68,12 +67,13 @@ fn check_failing_background_jobs(conn: &mut PgConnection) -> Result<()> {
6867 }
6968 } ;
7069
71- log_and_trigger_event ( event) ?;
70+ spawn_blocking ( move || log_and_trigger_event ( event) ) . await ?;
71+
7272 Ok ( ( ) )
7373}
7474
7575/// Check for an `update_downloads` job that has run longer than expected
76- fn check_stalled_update_downloads ( conn : & mut PgConnection ) -> Result < ( ) > {
76+ async fn check_stalled_update_downloads ( conn : & mut AsyncPgConnection ) -> Result < ( ) > {
7777 use chrono:: { DateTime , NaiveDateTime , Utc } ;
7878
7979 const EVENT_KEY : & str = "update_downloads_stalled" ;
@@ -86,28 +86,35 @@ fn check_stalled_update_downloads(conn: &mut PgConnection) -> Result<()> {
8686 let start_time: Result < NaiveDateTime , _ > = background_jobs:: table
8787 . filter ( background_jobs:: job_type. eq ( jobs:: UpdateDownloads :: JOB_NAME ) )
8888 . select ( background_jobs:: created_at)
89- . first ( conn) ;
89+ . first ( conn)
90+ . await ;
9091
9192 if let Ok ( start_time) = start_time {
9293 let start_time = DateTime :: < Utc > :: from_naive_utc_and_offset ( start_time, Utc ) ;
9394 let minutes = Utc :: now ( ) . signed_duration_since ( start_time) . num_minutes ( ) ;
9495
9596 if minutes > max_job_time {
96- return log_and_trigger_event ( on_call:: Event :: Trigger {
97- incident_key : Some ( EVENT_KEY . into ( ) ) ,
98- description : format ! ( "update_downloads job running for {minutes} minutes" ) ,
99- } ) ;
97+ return spawn_blocking ( move || {
98+ log_and_trigger_event ( on_call:: Event :: Trigger {
99+ incident_key : Some ( EVENT_KEY . into ( ) ) ,
100+ description : format ! ( "update_downloads job running for {minutes} minutes" ) ,
101+ } )
102+ } )
103+ . await ;
100104 }
101105 } ;
102106
103- log_and_trigger_event ( on_call:: Event :: Resolve {
104- incident_key : EVENT_KEY . into ( ) ,
105- description : Some ( "No stalled update_downloads job" . into ( ) ) ,
107+ spawn_blocking ( move || {
108+ log_and_trigger_event ( on_call:: Event :: Resolve {
109+ incident_key : EVENT_KEY . into ( ) ,
110+ description : Some ( "No stalled update_downloads job" . into ( ) ) ,
111+ } )
106112 } )
113+ . await
107114}
108115
109116/// Check for known spam patterns
110- fn check_spam_attack ( conn : & mut PgConnection ) -> Result < ( ) > {
117+ async fn check_spam_attack ( conn : & mut AsyncPgConnection ) -> Result < ( ) > {
111118 use crates_io:: sql:: canon_crate_name;
112119
113120 const EVENT_KEY : & str = "spam_attack" ;
@@ -126,6 +133,7 @@ fn check_spam_attack(conn: &mut PgConnection) -> Result<()> {
126133 . filter ( canon_crate_name ( crates:: name) . eq_any ( bad_crate_names) )
127134 . select ( crates:: name)
128135 . first ( conn)
136+ . await
129137 . optional ( ) ?;
130138
131139 if let Some ( bad_crate) = bad_crate {
@@ -144,7 +152,7 @@ fn check_spam_attack(conn: &mut PgConnection) -> Result<()> {
144152 }
145153 } ;
146154
147- log_and_trigger_event ( event) ?;
155+ spawn_blocking ( move || log_and_trigger_event ( event) ) . await ?;
148156 Ok ( ( ) )
149157}
150158
0 commit comments