Skip to content

Commit cd9806a

Browse files
authored
worker/jobs/expiry_notification: Add more logging statements (#8657)
1 parent 90e157c commit cd9806a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/worker/jobs/expiry_notification.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,33 @@ impl BackgroundJob for SendTokenExpiryNotifications {
3737

3838
/// Find tokens that are about to expire and send notifications to their owners.
3939
fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
40-
info!("Checking if tokens are about to expire");
4140
let before = chrono::Utc::now() + EXPIRY_THRESHOLD;
41+
info!("Searching for tokens that will expire before {before}…");
42+
4243
let expired_tokens = find_expiring_tokens(conn, before)?;
43-
if expired_tokens.len() == MAX_ROWS as usize {
44+
let num_tokens = expired_tokens.len();
45+
if num_tokens == 0 {
46+
info!("Found no tokens that will expire before {before}. Skipping expiry notifications.");
47+
return Ok(());
48+
}
49+
50+
info!("Found {num_tokens} tokens that will expire before {before}. Sending out expiry notifications…");
51+
52+
if num_tokens == MAX_ROWS as usize {
4453
warn!("The maximum number of API tokens per query has been reached. More API tokens might be processed on the next run.");
4554
}
55+
56+
let mut success = 0;
4657
for token in &expired_tokens {
4758
if let Err(e) = handle_expiring_token(conn, token, emails) {
4859
error!(?e, "Failed to handle expiring token");
60+
} else {
61+
success += 1;
4962
}
5063
}
5164

65+
info!("Sent expiry notifications for {success} of {num_tokens} expiring tokens.");
66+
5267
Ok(())
5368
}
5469

@@ -58,20 +73,28 @@ fn handle_expiring_token(
5873
token: &ApiToken,
5974
emails: &Emails,
6075
) -> Result<(), anyhow::Error> {
76+
debug!("Looking up user {} for token {}…", token.user_id, token.id);
6177
let user = User::find(conn, token.user_id)?;
78+
79+
debug!("Looking up email address for user {}…", user.id);
6280
let recipient = user
6381
.email(conn)?
6482
.ok_or_else(|| anyhow!("No address found"))?;
83+
84+
debug!("Sending expiry notification to {}…", recipient);
6585
let email = ExpiryNotificationEmail {
6686
name: &user.gh_login,
6787
token_name: &token.name,
6888
expiry_date: token.expired_at.unwrap().and_utc(),
6989
};
7090
emails.send(&recipient, email)?;
91+
7192
// Update the token to prevent duplicate notifications.
93+
debug!("Marking token {} as notified…", token.id);
7294
diesel::update(token)
7395
.set(api_tokens::expiry_notification_at.eq(now.nullable()))
7496
.execute(conn)?;
97+
7598
Ok(())
7699
}
77100

0 commit comments

Comments
 (0)