Skip to content

Commit

Permalink
fix bug where background tasks only ran on startup (facepalm)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemis21 committed Apr 19, 2024
1 parent 191ba8d commit c5b00e8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static DB_POOL: OnceLock<database::Main> = OnceLock::new();

/// Spawn a task to take care of running periodic background tasks.
pub async fn spawn(rocket: rocket::Rocket<rocket::Build>) -> rocket::fairing::Result {
println!("here");
let Some(db) = database::Main::fetch(&rocket) else {
eprintln!("couldn't retrieve db pool to set up background tasks");
return Err(rocket);
Expand All @@ -23,15 +24,17 @@ pub async fn spawn(rocket: rocket::Rocket<rocket::Build>) -> rocket::fairing::Re
scheduler
.every(1.day())
.at("00:00")
.every(1.minute())
.run(|| run_background_task("ensure daily chosen at midnight", ensure_daily_chosen));
scheduler
.every(1.minute())
.run(|| run_background_task("end timed-out games", end_timed_out_games));
rocket::tokio::task::spawn(async move {
// Check for new tasks once a minute.
// FIXME: these tasks don't appear to be actually running
scheduler.run_pending().await;
rocket::tokio::time::sleep(std::time::Duration::from_secs(60)).await;
loop {
scheduler.run_pending().await;
rocket::tokio::time::sleep(std::time::Duration::from_secs(60)).await;
}
});
Ok(rocket)
}
Expand Down Expand Up @@ -70,9 +73,11 @@ async fn db_conn() -> Result<sqlx::pool::PoolConnection<sqlx::Postgres>> {
/// and also ensures that the database is populated with tracks and related data for
/// other tasks.
async fn ensure_daily_chosen() -> Result<()> {
println!("here....");
track::pick::daily(&mut *db_conn().await?)
.await
.wrap_err("error picking a daily track as a background task")?;
println!("done!");
Ok(())
}

Expand Down

0 comments on commit c5b00e8

Please sign in to comment.