@@ -6,8 +6,8 @@ use chrono::SecondsFormat;
6
6
use crates_io_worker:: BackgroundJob ;
7
7
use diesel:: dsl:: IntervalDsl ;
8
8
use diesel:: {
9
- dsl:: now, BoolExpressionMethods , Connection , ExpressionMethods , NullableExpressionMethods ,
10
- PgConnection , QueryDsl , QueryResult , RunQueryDsl , SelectableHelper ,
9
+ dsl:: now, BoolExpressionMethods , ExpressionMethods , NullableExpressionMethods , PgConnection ,
10
+ QueryDsl , QueryResult , RunQueryDsl , SelectableHelper ,
11
11
} ;
12
12
use std:: sync:: Arc ;
13
13
@@ -51,35 +51,37 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
51
51
warn ! ( "The maximum number of API tokens per query has been reached. More API tokens might be processed on the next run." ) ;
52
52
}
53
53
for token in & expired_tokens {
54
- conn. transaction ( |conn| {
55
- // Send notification.
56
- let user = User :: find ( conn, token. user_id ) ?;
57
- let Some ( recipient) = user. email ( conn) ? else {
58
- return Err ( anyhow ! ( "No address found" ) ) ;
59
- } ;
60
- let email = ExpiryNotificationEmail {
61
- name : & user. gh_login ,
62
- token_name : & token. name ,
63
- expiry_date : token. expired_at . unwrap ( ) . and_utc ( ) ,
64
- } ;
65
- match emails. send ( & recipient, email) {
66
- Ok ( _) => {
67
- // Update the token to prevent duplicate notifications.
68
- diesel:: update ( token)
69
- . set ( api_tokens:: expiry_notification_at. eq ( now. nullable ( ) ) )
70
- . execute ( conn) ?;
71
- }
72
- Err ( e) => {
73
- error ! ( ?e, ?recipient, "Failed to send notification" ) ;
74
- return Err ( anyhow ! ( "Failed to send notification: {}" , e) ) ;
75
- }
76
- }
77
- Ok :: < _ , anyhow:: Error > ( ( ) )
78
- } ) ?;
54
+ if let Err ( e) = handle_expiring_token ( conn, token, emails) {
55
+ error ! ( ?e, "Failed to handle expiring token" ) ;
56
+ }
79
57
}
80
58
81
59
Ok ( ( ) )
82
60
}
61
+
62
+ fn handle_expiring_token (
63
+ conn : & mut PgConnection ,
64
+ token : & ApiToken ,
65
+ emails : & Emails ,
66
+ ) -> Result < ( ) , anyhow:: Error > {
67
+ let user = User :: find ( conn, token. user_id ) ?;
68
+ let recipient = match user. email ( conn) ? {
69
+ Some ( email) => email,
70
+ None => return Err ( anyhow ! ( "No address found" ) ) ,
71
+ } ;
72
+ let email = ExpiryNotificationEmail {
73
+ name : & user. gh_login ,
74
+ token_name : & token. name ,
75
+ expiry_date : token. expired_at . unwrap ( ) . and_utc ( ) ,
76
+ } ;
77
+ emails. send ( & recipient, email) ?;
78
+ // Update the token to prevent duplicate notifications.
79
+ diesel:: update ( token)
80
+ . set ( api_tokens:: expiry_notification_at. eq ( now. nullable ( ) ) )
81
+ . execute ( conn) ?;
82
+ Ok ( ( ) )
83
+ }
84
+
83
85
/// Find all tokens that are not revoked and will expire within the specified number of days.
84
86
pub fn find_tokens_expiring_within_days (
85
87
conn : & mut PgConnection ,
0 commit comments