@@ -4,7 +4,7 @@ use crate::{email::Email, models::User, worker::Environment, Emails};
4
4
use anyhow:: anyhow;
5
5
use chrono:: SecondsFormat ;
6
6
use crates_io_worker:: BackgroundJob ;
7
- use diesel:: dsl:: { now, IntervalDsl } ;
7
+ use diesel:: dsl:: now;
8
8
use diesel:: prelude:: * ;
9
9
use std:: sync:: Arc ;
10
10
@@ -43,7 +43,8 @@ impl BackgroundJob for SendTokenExpiryNotifications {
43
43
// Check if the token is about to expire and send a notification if it is.
44
44
fn check ( emails : & Emails , conn : & mut PgConnection ) -> anyhow:: Result < ( ) > {
45
45
info ! ( "Checking if tokens are about to expire" ) ;
46
- let expired_tokens = find_tokens_expiring_within_days ( conn, EXPIRY_THRESHOLD ) ?;
46
+ let before = chrono:: Utc :: now ( ) + EXPIRY_THRESHOLD ;
47
+ let expired_tokens = find_expiring_tokens ( conn, before) ?;
47
48
if expired_tokens. len ( ) == MAX_ROWS as usize {
48
49
warn ! ( "The maximum number of API tokens per query has been reached. More API tokens might be processed on the next run." ) ;
49
50
}
@@ -79,10 +80,14 @@ fn handle_expiring_token(
79
80
Ok ( ( ) )
80
81
}
81
82
82
- /// Find all tokens that are not revoked and will expire within the specified number of days.
83
- pub fn find_tokens_expiring_within_days (
83
+ /// Find tokens that will expire before the given date, but haven't expired yet
84
+ /// and haven't been notified about their impending expiry. Revoked tokens are
85
+ /// also ignored.
86
+ ///
87
+ /// This function returns at most `MAX_ROWS` tokens.
88
+ pub fn find_expiring_tokens (
84
89
conn : & mut PgConnection ,
85
- days_until_expiry : chrono:: TimeDelta ,
90
+ before : chrono:: DateTime < chrono :: Utc > ,
86
91
) -> QueryResult < Vec < ApiToken > > {
87
92
api_tokens:: table
88
93
. filter ( api_tokens:: revoked. eq ( false ) )
@@ -92,7 +97,7 @@ pub fn find_tokens_expiring_within_days(
92
97
. filter (
93
98
api_tokens:: expired_at
94
99
. assume_not_null ( )
95
- . lt ( now + days_until_expiry . num_days ( ) . day ( ) ) ,
100
+ . lt ( before . naive_utc ( ) ) ,
96
101
)
97
102
. filter ( api_tokens:: expiry_notification_at. is_null ( ) )
98
103
. select ( ApiToken :: as_select ( ) )
0 commit comments