Skip to content

Commit e094e5e

Browse files
committed
fix schedule
1 parent 2ec36b0 commit e094e5e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/pfp_updater.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,28 @@ pub async fn schedule_daily_updates(http: Arc<HttpClient>, state: Arc<Mutex<Stat
190190
};
191191

192192
tokio::spawn(async move {
193-
loop {
194-
// Wait 24 hours
195-
tokio::time::sleep(tokio::time::Duration::from_secs(24 * 60 * 60)).await;
193+
let now = std::time::SystemTime::now();
194+
let since_epoch = now.duration_since(std::time::UNIX_EPOCH).unwrap();
195+
196+
let seconds_today = since_epoch.as_secs() % (24 * 60 * 60);
197+
let seconds_until_midnight = (24 * 60 * 60) - seconds_today;
198+
let duration_until_midnight = std::time::Duration::from_secs(seconds_until_midnight);
199+
200+
tracing::info!(
201+
"Waiting {} seconds until midnight for first profile picture update",
202+
seconds_until_midnight
203+
);
196204

205+
// Wait until midnight
206+
tokio::time::sleep(duration_until_midnight).await;
207+
208+
loop {
197209
if let Err(e) = update_profile_picture(&http, &state, channel_id).await {
198210
tracing::error!("Failed to update profile picture: {:?}", e);
199211
}
212+
213+
// Wait 24 hours until next midnight
214+
tokio::time::sleep(tokio::time::Duration::from_secs(24 * 60 * 60)).await;
200215
}
201216
});
202217
}

0 commit comments

Comments
 (0)