Clear resting sports orders once at scheduled kickoff
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::{assets, validation::identifier};
|
||||
use anyhow::{ensure, Result};
|
||||
use rusqlite::{params, Connection, Transaction};
|
||||
use rusqlite::{params, Connection, OptionalExtension, Transaction};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
@@ -401,6 +401,14 @@ impl Query {
|
||||
}
|
||||
Ok((id,event))
|
||||
})?;
|
||||
if detail.get("sport").is_some() {
|
||||
detail["kickoff"] = db.query_row(
|
||||
"SELECT due_ms,cleared_at_ms,cancelled_orders FROM sports_kickoffs WHERE event=?",
|
||||
[&id], |r| Ok(json!({"scheduled_at_ms":r.get::<_,i64>(0)?,
|
||||
"cleared_at_ms":r.get::<_,Option<i64>>(1)?,
|
||||
"cancelled_orders":r.get::<_,Option<i64>>(2)?})),
|
||||
).optional()?.unwrap_or(Value::Null);
|
||||
}
|
||||
let mut stmt = db.prepare(
|
||||
"SELECT s.market,COALESCE(a.question,s.question),yes_label,no_label,terms,source,resolver,rules_version,
|
||||
m.rules,m.phase,COALESCE(a.name,group_title),group_order,n.group_id,n.ordinal,
|
||||
|
||||
@@ -9,6 +9,12 @@ pub(crate) enum Command {
|
||||
micros: Option<i64>,
|
||||
},
|
||||
Event(crate::catalog::Event),
|
||||
#[serde(rename = "reschedule_sports")]
|
||||
RescheduleSports {
|
||||
event: String,
|
||||
previous_ms: i64,
|
||||
scheduled_at_ms: i64,
|
||||
},
|
||||
#[serde(rename = "tick_size_change")]
|
||||
TickSizeChange(crate::tick_changes::Change),
|
||||
List {
|
||||
|
||||
@@ -14,6 +14,7 @@ pub(crate) fn transaction(
|
||||
"SELECT EXISTS(SELECT 1 FROM order_deadlines WHERE pending=1 AND expiration<=unixepoch()+60)
|
||||
OR EXISTS(SELECT 1 FROM twap_markets WHERE pending_close=1 AND end_at<=unixepoch())
|
||||
OR EXISTS(SELECT 1 FROM order_heartbeats WHERE deadline_ms<=CAST(unixepoch('subsec')*1000 AS INTEGER))
|
||||
OR EXISTS(SELECT 1 FROM sports_kickoffs WHERE cleared_at_ms IS NULL AND due_ms<=CAST(unixepoch('subsec')*1000 AS INTEGER))
|
||||
OR EXISTS(SELECT 1 FROM quote_pending)",
|
||||
[], |row| row.get(0),
|
||||
)?;
|
||||
@@ -46,6 +47,7 @@ pub(crate) fn transaction(
|
||||
for market in markets {
|
||||
crate::market::close_market(&write, &market)?;
|
||||
}
|
||||
crate::kickoff::clear_due(&write)?;
|
||||
crate::heartbeat::expire(&write)?;
|
||||
crate::quotes::flush(&write)?;
|
||||
write.commit()?;
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
BEGIN IMMEDIATE;
|
||||
CREATE TABLE IF NOT EXISTS sports_kickoffs (
|
||||
event TEXT PRIMARY KEY REFERENCES events,
|
||||
due_ms INTEGER NOT NULL,
|
||||
cleared_at_ms INTEGER,
|
||||
cancelled_orders INTEGER
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS pending_kickoffs ON sports_kickoffs(due_ms,event)
|
||||
WHERE cleared_at_ms IS NULL;
|
||||
|
||||
-- Legacy orders have no reliable pre-match/in-play marker. Arm only future fixtures.
|
||||
INSERT OR IGNORE INTO sports_kickoffs(event,due_ms)
|
||||
SELECT id,scheduled_at_ms FROM events WHERE scheduled_at_ms>CAST(unixepoch('subsec')*1000 AS INTEGER);
|
||||
CREATE TRIGGER IF NOT EXISTS register_kickoff AFTER INSERT ON events BEGIN
|
||||
INSERT INTO sports_kickoffs(event,due_ms) VALUES(NEW.id,NEW.scheduled_at_ms);
|
||||
END;
|
||||
COMMIT;
|
||||
@@ -16,6 +16,7 @@ use validation::identifier;
|
||||
mod book;
|
||||
mod cancellation;
|
||||
mod command;
|
||||
mod kickoff;
|
||||
mod ledger;
|
||||
mod market;
|
||||
mod minimum_size;
|
||||
@@ -46,6 +47,13 @@ impl Exchange {
|
||||
db.busy_timeout(Duration::from_secs(10))?;
|
||||
db.execute_batch("PRAGMA foreign_keys=ON")?;
|
||||
db.execute_batch(include_str!("schema.sql"))?;
|
||||
let kickoff_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='sports_kickoffs')",
|
||||
[], |r| r.get(0),
|
||||
)?;
|
||||
if !kickoff_ready {
|
||||
db.execute_batch(include_str!("kickoff_schema.sql"))?;
|
||||
}
|
||||
let activity_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='wallet_migration')",
|
||||
[], |r| r.get(0),
|
||||
|
||||
@@ -45,6 +45,11 @@ pub(crate) fn apply(
|
||||
}
|
||||
Command::TickSizeChange(change) => change.apply(tx, request_id),
|
||||
Command::Event(event) => event.save(tx),
|
||||
Command::RescheduleSports {
|
||||
event,
|
||||
previous_ms,
|
||||
scheduled_at_ms,
|
||||
} => crate::kickoff::reschedule(tx, &event, previous_ms, scheduled_at_ms),
|
||||
Command::List {
|
||||
market,
|
||||
rules,
|
||||
|
||||
Reference in New Issue
Block a user