11use crate :: schema:: background_jobs;
2- use diesel:: connection:: LoadConnection ;
32use diesel:: dsl:: now;
43use diesel:: pg:: Pg ;
54use diesel:: prelude:: * ;
65use diesel:: sql_types:: { Bool , Integer , Interval } ;
76use diesel:: { delete, update} ;
7+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
88
99#[ derive( Queryable , Selectable , Identifiable , Debug , Clone ) ]
1010pub ( super ) struct BackgroundJob {
@@ -26,8 +26,8 @@ fn retriable() -> Box<dyn BoxableExpression<background_jobs::table, Pg, SqlType
2626
2727/// Finds the next job that is unlocked, and ready to be retried. If a row is
2828/// found, it will be locked.
29- pub ( super ) fn find_next_unlocked_job (
30- conn : & mut impl LoadConnection < Backend = Pg > ,
29+ pub ( super ) async fn find_next_unlocked_job (
30+ conn : & mut AsyncPgConnection ,
3131 job_types : & [ String ] ,
3232) -> QueryResult < BackgroundJob > {
3333 background_jobs:: table
@@ -38,34 +38,39 @@ pub(super) fn find_next_unlocked_job(
3838 . for_update ( )
3939 . skip_locked ( )
4040 . first :: < BackgroundJob > ( conn)
41+ . await
4142}
4243
4344/// The number of jobs that have failed at least once
44- pub ( super ) fn failed_job_count ( conn : & mut impl LoadConnection < Backend = Pg > ) -> QueryResult < i64 > {
45+ pub ( super ) async fn failed_job_count ( conn : & mut AsyncPgConnection ) -> QueryResult < i64 > {
4546 background_jobs:: table
4647 . count ( )
4748 . filter ( background_jobs:: retries. gt ( 0 ) )
4849 . get_result ( conn)
50+ . await
4951}
5052
5153/// Deletes a job that has successfully completed running
52- pub ( super ) fn delete_successful_job (
53- conn : & mut impl LoadConnection < Backend = Pg > ,
54+ pub ( super ) async fn delete_successful_job (
55+ conn : & mut AsyncPgConnection ,
5456 job_id : i64 ,
5557) -> QueryResult < ( ) > {
56- delete ( background_jobs:: table. find ( job_id) ) . execute ( conn) ?;
58+ delete ( background_jobs:: table. find ( job_id) )
59+ . execute ( conn)
60+ . await ?;
5761 Ok ( ( ) )
5862}
5963
6064/// Marks that we just tried and failed to run a job.
6165///
6266/// Ignores any database errors that may have occurred. If the DB has gone away,
6367/// we assume that just trying again with a new connection will succeed.
64- pub ( super ) fn update_failed_job ( conn : & mut impl LoadConnection < Backend = Pg > , job_id : i64 ) {
68+ pub ( super ) async fn update_failed_job ( conn : & mut AsyncPgConnection , job_id : i64 ) {
6569 let _ = update ( background_jobs:: table. find ( job_id) )
6670 . set ( (
6771 background_jobs:: retries. eq ( background_jobs:: retries + 1 ) ,
6872 background_jobs:: last_retry. eq ( now) ,
6973 ) )
70- . execute ( conn) ;
74+ . execute ( conn)
75+ . await ;
7176}
0 commit comments