Clear resting sports orders once at scheduled kickoff
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
use crate::{
|
||||
book::{self, CancelReason},
|
||||
validation::identifier,
|
||||
};
|
||||
use anyhow::{ensure, Result};
|
||||
use rusqlite::{params, Transaction};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub(crate) fn reschedule(
|
||||
tx: &Transaction,
|
||||
event: &str,
|
||||
previous_ms: i64,
|
||||
scheduled_at_ms: i64,
|
||||
) -> Result<Value> {
|
||||
identifier(event)?;
|
||||
ensure!(scheduled_at_ms >= 0, "invalid sports start time");
|
||||
let changed = tx.execute(
|
||||
"UPDATE sports_kickoffs SET due_ms=?1 WHERE event=?2 AND due_ms=?3 AND cleared_at_ms IS NULL",
|
||||
params![scheduled_at_ms, event, previous_ms],
|
||||
)?;
|
||||
ensure!(changed == 1, "no pending kickoff at the specified time");
|
||||
tx.execute(
|
||||
"UPDATE events SET scheduled_at_ms=? WHERE id=?",
|
||||
params![scheduled_at_ms, event],
|
||||
)?;
|
||||
clear_due(tx)?;
|
||||
Ok(json!({"event":event,"scheduled_at_ms":scheduled_at_ms}))
|
||||
}
|
||||
|
||||
pub(crate) fn clear_due(tx: &Transaction) -> Result<()> {
|
||||
let now: i64 = tx.query_row(
|
||||
"SELECT CAST(unixepoch('subsec')*1000 AS INTEGER)",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let mut pending = tx.prepare(
|
||||
"SELECT event FROM sports_kickoffs WHERE cleared_at_ms IS NULL AND due_ms<=? ORDER BY due_ms,event",
|
||||
)?;
|
||||
let events = pending
|
||||
.query_map([now], |r| r.get::<_, String>(0))?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
for event in events {
|
||||
let mut active = tx.prepare(
|
||||
"SELECT o.seq,o.account FROM sports_markets m JOIN orders o ON o.market=m.market
|
||||
WHERE m.event=? AND o.status='live' ORDER BY o.seq",
|
||||
)?;
|
||||
let orders = active
|
||||
.query_map([&event], |r| {
|
||||
Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?))
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
for (seq, owner) in &orders {
|
||||
book::release(tx, *seq, owner, CancelReason::Cancelled)?;
|
||||
}
|
||||
tx.execute(
|
||||
"UPDATE sports_kickoffs SET cleared_at_ms=?,cancelled_orders=? WHERE event=?",
|
||||
params![now, orders.len() as i64, event],
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user