fix: harden skipped ledger resolution paths
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
b88b21b772
commit
48dfc3e76f
@@ -40,63 +40,72 @@ class PostgreSQLLedgerSkipStore:
|
||||
)
|
||||
now = datetime.now(tz=UTC)
|
||||
notify = False
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn, conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT status FROM ledger_skip WHERE skip_key = %s FOR UPDATE",
|
||||
(skip_key,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
notify = True
|
||||
cur.execute(
|
||||
"INSERT INTO ledger_skip "
|
||||
"(skip_key, instance_key, slot_id, portfolio_id, symbol, order_id, "
|
||||
" execution_id, side, qty, price, executed_at, order_venue, "
|
||||
" execution_venue, skip_reason, status, created_at, updated_at) "
|
||||
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
|
||||
" 'unresolved', %s, %s)",
|
||||
(
|
||||
skip_key,
|
||||
instance_key,
|
||||
slot_id,
|
||||
portfolio_id,
|
||||
symbol,
|
||||
order_id,
|
||||
execution_id,
|
||||
side,
|
||||
qty,
|
||||
price,
|
||||
executed_at,
|
||||
order_venue or "unknown",
|
||||
execution_venue or "unknown",
|
||||
skip_reason,
|
||||
now,
|
||||
now,
|
||||
),
|
||||
)
|
||||
else:
|
||||
notify = row[0] == "resolved"
|
||||
cur.execute(
|
||||
"UPDATE ledger_skip SET portfolio_id=%s, symbol=%s, order_id=%s, "
|
||||
"execution_id=%s, qty=%s, price=%s, executed_at=%s, "
|
||||
"order_venue=%s, execution_venue=%s, status='unresolved', "
|
||||
"resolution_reason=NULL, resolved_at=NULL, updated_at=%s "
|
||||
"WHERE skip_key=%s",
|
||||
(
|
||||
portfolio_id,
|
||||
symbol,
|
||||
order_id,
|
||||
execution_id,
|
||||
qty,
|
||||
price,
|
||||
executed_at,
|
||||
order_venue or "unknown",
|
||||
execution_venue or "unknown",
|
||||
now,
|
||||
skip_key,
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
for _ in range(2):
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn, conn.cursor() as cur:
|
||||
try:
|
||||
cur.execute(
|
||||
"INSERT INTO ledger_skip "
|
||||
"(skip_key, instance_key, slot_id, portfolio_id, symbol, order_id, "
|
||||
" execution_id, side, qty, price, executed_at, order_venue, "
|
||||
" execution_venue, skip_reason, status, created_at, updated_at) "
|
||||
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
|
||||
" 'unresolved', %s, %s)",
|
||||
(
|
||||
skip_key,
|
||||
instance_key,
|
||||
slot_id,
|
||||
portfolio_id,
|
||||
symbol,
|
||||
order_id,
|
||||
execution_id,
|
||||
side,
|
||||
qty,
|
||||
price,
|
||||
executed_at,
|
||||
order_venue or "unknown",
|
||||
execution_venue or "unknown",
|
||||
skip_reason,
|
||||
now,
|
||||
now,
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
notify = True
|
||||
break
|
||||
except self._psycopg.errors.UniqueViolation:
|
||||
conn.rollback()
|
||||
cur.execute(
|
||||
"SELECT status FROM ledger_skip WHERE skip_key = %s FOR UPDATE",
|
||||
(skip_key,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
continue
|
||||
notify = row[0] == "resolved"
|
||||
cur.execute(
|
||||
"UPDATE ledger_skip SET portfolio_id=%s, symbol=%s, order_id=%s, "
|
||||
"execution_id=%s, qty=%s, price=%s, executed_at=%s, "
|
||||
"order_venue=%s, execution_venue=%s, status='unresolved', "
|
||||
"resolution_reason=NULL, resolved_at=NULL, updated_at=%s "
|
||||
"WHERE skip_key=%s",
|
||||
(
|
||||
portfolio_id,
|
||||
symbol,
|
||||
order_id,
|
||||
execution_id,
|
||||
qty,
|
||||
price,
|
||||
executed_at,
|
||||
order_venue or "unknown",
|
||||
execution_venue or "unknown",
|
||||
now,
|
||||
skip_key,
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(f"failed to persist ledger_skip row for key {skip_key!r}")
|
||||
record = self.get(skip_key)
|
||||
if record is None:
|
||||
raise RuntimeError(f"ledger_skip row missing after upsert for key {skip_key!r}")
|
||||
@@ -166,21 +175,24 @@ class PostgreSQLLedgerSkipStore:
|
||||
if execution_id:
|
||||
cur.execute(
|
||||
"UPDATE ledger_skip SET status='resolved', resolution_reason=%s, "
|
||||
"execution_id=COALESCE(execution_id, %s), "
|
||||
"resolved_at=%s, updated_at=%s "
|
||||
"WHERE status='unresolved' AND instance_key=%s AND slot_id=%s "
|
||||
"AND side=%s AND execution_id=%s "
|
||||
"AND side=%s AND (execution_id=%s OR (execution_id IS NULL AND order_id=%s)) "
|
||||
"RETURNING skip_key, instance_key, slot_id, portfolio_id, symbol, "
|
||||
"order_id, execution_id, side, qty, price, executed_at, order_venue, "
|
||||
"execution_venue, skip_reason, status, resolution_reason, resolved_at, "
|
||||
"created_at, updated_at",
|
||||
(
|
||||
resolution_reason,
|
||||
execution_id,
|
||||
now,
|
||||
now,
|
||||
instance_key,
|
||||
slot_id,
|
||||
side,
|
||||
execution_id,
|
||||
order_id,
|
||||
),
|
||||
)
|
||||
else:
|
||||
@@ -207,17 +219,60 @@ class PostgreSQLLedgerSkipStore:
|
||||
conn.commit()
|
||||
return [self._row_to_record(row) for row in rows]
|
||||
|
||||
def resolve_by_key(self, skip_key: str, *, resolution_reason: str) -> LedgerSkipRecord | None:
|
||||
def resolve_for_delivery(
|
||||
self,
|
||||
*,
|
||||
instance_key: str,
|
||||
portfolio_id: str,
|
||||
side: str,
|
||||
order_id: str,
|
||||
execution_id: str,
|
||||
resolution_reason: str,
|
||||
) -> list[LedgerSkipRecord]:
|
||||
now = datetime.now(tz=UTC)
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn, conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE ledger_skip SET status='resolved', resolution_reason=%s, "
|
||||
"execution_id=COALESCE(execution_id, %s), "
|
||||
"resolved_at=%s, updated_at=%s "
|
||||
"WHERE status='unresolved' AND instance_key=%s AND portfolio_id=%s AND side=%s "
|
||||
"AND (execution_id=%s OR (execution_id IS NULL AND order_id=%s)) "
|
||||
"RETURNING skip_key, instance_key, slot_id, portfolio_id, symbol, order_id, "
|
||||
"execution_id, side, qty, price, executed_at, order_venue, execution_venue, "
|
||||
"skip_reason, status, resolution_reason, resolved_at, created_at, updated_at",
|
||||
(
|
||||
resolution_reason,
|
||||
execution_id,
|
||||
now,
|
||||
now,
|
||||
instance_key,
|
||||
portfolio_id,
|
||||
side,
|
||||
execution_id,
|
||||
order_id,
|
||||
),
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
conn.commit()
|
||||
return [self._row_to_record(row) for row in rows]
|
||||
|
||||
def resolve_by_key(
|
||||
self,
|
||||
skip_key: str,
|
||||
*,
|
||||
instance_key: str,
|
||||
resolution_reason: str,
|
||||
) -> LedgerSkipRecord | None:
|
||||
now = datetime.now(tz=UTC)
|
||||
with self._psycopg.connect(self._dsn, autocommit=False) as conn, conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE ledger_skip SET status='resolved', resolution_reason=%s, "
|
||||
"resolved_at=%s, updated_at=%s "
|
||||
"WHERE skip_key=%s AND status='unresolved' "
|
||||
"WHERE skip_key=%s AND instance_key=%s AND status='unresolved' "
|
||||
"RETURNING skip_key, instance_key, slot_id, portfolio_id, symbol, order_id, "
|
||||
"execution_id, side, qty, price, executed_at, order_venue, execution_venue, "
|
||||
"skip_reason, status, resolution_reason, resolved_at, created_at, updated_at",
|
||||
(resolution_reason, now, now, skip_key),
|
||||
(resolution_reason, now, now, skip_key, instance_key),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
conn.commit()
|
||||
|
||||
Reference in New Issue
Block a user