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 ac35b64
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 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 @@ -29,9 +30,10 @@ pub async fn spawn(rocket: rocket::Rocket<rocket::Build>) -> rocket::fairing::Re
.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 +72,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 ac35b64

Please sign in to comment.