Skip to content

Commit ff40368

Browse files
ff137cursoragent
andcommitted
fix(db): rollback rater and syncSubs transactions on error
Mirror insertMatch behavior so exceptions after db.transaction() always call trx.rollback() before propagating. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0fcb8e1 commit ff40368

2 files changed

Lines changed: 44 additions & 34 deletions

File tree

svc/rater.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,37 @@ await runInLoop(async function rate() {
7474
const ratingDiff2 = kFactor * (win2 - e2);
7575
// Start transaction
7676
const trx = await db.transaction();
77-
// Rate each player
78-
console.log("match %s, radiant_win: %s", row.match_id, row.radiant_win);
79-
for (let p of gcMatch.players) {
80-
const oldRating = ratingMap.get(p.account_id!) ?? DEFAULT_RATING;
81-
const delta = isRadiant(p) ? ratingDiff1 : ratingDiff2;
82-
// apply delta to each player (all players on a team will have the same rating change)
83-
const newRating = oldRating + delta;
84-
console.log(
85-
"account_id: %s, oldRating: %s, newRating: %s, delta: %s",
86-
p.account_id,
87-
oldRating,
88-
newRating,
89-
delta,
90-
);
91-
// Write ratings back to players
77+
try {
78+
// Rate each player
79+
console.log("match %s, radiant_win: %s", row.match_id, row.radiant_win);
80+
for (let p of gcMatch.players) {
81+
const oldRating = ratingMap.get(p.account_id!) ?? DEFAULT_RATING;
82+
const delta = isRadiant(p) ? ratingDiff1 : ratingDiff2;
83+
// apply delta to each player (all players on a team will have the same rating change)
84+
const newRating = oldRating + delta;
85+
console.log(
86+
"account_id: %s, oldRating: %s, newRating: %s, delta: %s",
87+
p.account_id,
88+
oldRating,
89+
newRating,
90+
delta,
91+
);
92+
// Write ratings back to players
93+
await trx.raw(
94+
`INSERT INTO ${tableName}(account_id, computed_mmr, delta, match_id) VALUES(?, ?, ?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr, delta = EXCLUDED.delta, match_id = EXCLUDED.match_id`,
95+
[p.account_id, newRating, delta, row.match_id],
96+
);
97+
}
98+
// Delete row
9299
await trx.raw(
93-
`INSERT INTO ${tableName}(account_id, computed_mmr, delta, match_id) VALUES(?, ?, ?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr, delta = EXCLUDED.delta, match_id = EXCLUDED.match_id`,
94-
[p.account_id, newRating, delta, row.match_id],
100+
"DELETE FROM rating_queue WHERE match_seq_num = ?",
101+
row.match_seq_num,
95102
);
103+
// Commit transaction
104+
await trx.commit();
105+
redisCount("rater");
106+
} catch (e) {
107+
await trx.rollback();
108+
throw e;
96109
}
97-
// Delete row
98-
await trx.raw(
99-
"DELETE FROM rating_queue WHERE match_seq_num = ?",
100-
row.match_seq_num,
101-
);
102-
// Commit transaction
103-
await trx.commit();
104-
redisCount("rater");
105110
}, 0);

svc/syncSubs.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ await runInLoop(async function doSyncSubs() {
1515
}
1616
console.log(result.length, "subs");
1717
const trx = await db.transaction();
18-
// Delete all status from subscribers
19-
await trx.raw("UPDATE subscriber SET status = NULL");
20-
for (let sub of result) {
21-
// Mark list of subscribers as active
22-
await trx.raw("UPDATE subscriber SET status = ? WHERE customer_id = ?", [
23-
sub.status,
24-
sub.customer,
25-
]);
18+
try {
19+
// Delete all status from subscribers
20+
await trx.raw("UPDATE subscriber SET status = NULL");
21+
for (let sub of result) {
22+
// Mark list of subscribers as active
23+
await trx.raw("UPDATE subscriber SET status = ? WHERE customer_id = ?", [
24+
sub.status,
25+
sub.customer,
26+
]);
27+
}
28+
await trx.commit();
29+
} catch (e) {
30+
await trx.rollback();
31+
throw e;
2632
}
27-
await trx.commit();
2833
}, 60 * 1000);

0 commit comments

Comments
 (0)