Skip to content

Commit

Permalink
Fix edge cases with daily_results poll
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldickison committed Aug 22, 2023
1 parent 49f5368 commit 54ebc29
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/poll/daily_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::external::sumo_api;
use crate::AppState;
use chrono::Utc;
use chrono::{DateTime, FixedOffset, NaiveTime};
use rusqlite::OptionalExtension;
use tokio::time::sleep;

const POLL_INTERVAL_FAST: i64 = 300; // 5 min
Expand Down Expand Up @@ -78,8 +79,9 @@ async fn do_tick(app_state: &AppState) -> anyhow::Result<DateTime<Utc>> {
{
// Find the last completed day of the currently active basho. The current basho should be the only one in the db with picks but without any associated awards.
let db = app_state.db.lock().unwrap();
(basho_id, basho_start_date, last_day) = db.query_row(
"
(basho_id, basho_start_date, last_day) = match db
.query_row(
"
SELECT
basho.id,
basho.start_date,
Expand All @@ -89,9 +91,17 @@ async fn do_tick(app_state: &AppState) -> anyhow::Result<DateTime<Utc>> {
NOT EXISTS (SELECT 1 FROM award WHERE award.basho_id = basho.id)
AND EXISTS (SELECT 1 FROM pick WHERE pick.basho_id = basho.id)
", // This will error if multiple rows are returned
(),
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)?;
(),
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)
.optional()?
{
None => {
info!("No current basho");
return Ok(Utc::now() + chrono::Duration::days(1));
}
Some(row) => row,
}
}

let day = last_day + 1;
Expand Down

0 comments on commit 54ebc29

Please sign in to comment.