Skip to content

Commit

Permalink
update reconcile order
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Feb 24, 2025
1 parent 1ea3ba5 commit 9b5920f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,10 @@ CREATE TABLE IF NOT EXISTS player_match_history(
PRIMARY KEY (match_id, account_id),
account_id bigint,
match_id bigint,
player_slot integer
player_slot integer,
retries integer
);
CREATE INDEX IF NOT EXISTS player_match_history_retries_idx ON player_match_history(retries) NULLS FIRST;

DO $$
BEGIN
Expand Down
4 changes: 2 additions & 2 deletions svc/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export async function reconcileMatch(rows: HistoryType[]) {
async function doReconcile() {
while (true) {
// Fetch rows for a single match (could be multiple players to fill)
const { rows }: { rows: HistoryType[] } = await db.raw('SELECT * from player_match_history WHERE match_id = (SELECT match_id FROM player_match_history TABLESAMPLE SYSTEM_ROWS(1))');
const { rows }: { rows: HistoryType[] } = await db.raw('UPDATE player_match_history SET retries = coalesce(retries, 0) + 1 WHERE match_id = (SELECT match_id FROM player_match_history ORDER BY retries ASC NULLS FIRST LIMIT 1) RETURNING *');
console.log(rows[0].match_id);
if (rows[0].match_id < 6000000000) {
// Old match so we probably don't have data (until backfilled)
// We still might have data, so process it with some probability
// If not processed, retry with short interval
if (Math.random() < 0.9) {
console.log('skip', rows[0].match_id);
await new Promise(resolve => setTimeout(resolve, 50));
continue;
}
Expand Down

0 comments on commit 9b5920f

Please sign in to comment.