Reuse the post-write tournament build and keep rosters cached on set-ending score reports

This commit is contained in:
Kalle
2026-11-08 10:18:11 +02:00
parent 9a747cd5d8
commit 2e4cd54dbd
3 changed files with 90 additions and 6 deletions

View File

@@ -1,14 +1,23 @@
import { addHours, addMinutes, subHours, subMinutes } from "date-fns";
import { beforeEach, describe, expect, test } from "vitest";
import * as TournamentFactory from "~/db/seed/factories/TournamentFactory";
import * as TournamentTeamFactory from "~/db/seed/factories/TournamentTeamFactory";
import * as UserFactory from "~/db/seed/factories/UserFactory";
import { dateToDatabaseTimestamp } from "~/utils/dates";
import * as Engine from "./engine";
import { RunningTournaments } from "./RunningTournaments.server";
import {
clearTournamentDataCache,
evictStaleRunningTournaments,
refreshTournamentCache,
serializeBracket,
tournamentSharedCached,
tournamentTeamsFullCached,
} from "./Tournament.server";
import { progressions, testTournament } from "./tests/test-utils";
const users = UserFactory.pool();
const SWISS_SETTINGS = { groupCount: 2, roundCount: 3 };
const swissBracket = () => {
@@ -189,3 +198,59 @@ describe("evictStaleRunningTournaments", () => {
expect(RunningTournaments.has(1)).toBe(false);
});
});
describe("refreshTournamentCache", () => {
beforeEach(async () => {
await users.create(1);
});
test("stores the rebuild for the next viewer", async () => {
const { id: tournamentId } = await TournamentFactory.create({
authorId: users.id(1),
});
clearTournamentDataCache(tournamentId);
const refreshed = await refreshTournamentCache(tournamentId);
expect(await tournamentSharedCached(tournamentId)).toBe(refreshed);
});
});
describe("clearTournamentDataCache", () => {
beforeEach(async () => {
await users.create(3);
});
const setupWithRosterCached = async () => {
const { id: tournamentId } = await TournamentFactory.create({
authorId: users.id(1),
});
await TournamentTeamFactory.create({
tournamentId,
memberUserIds: [users.id(2)],
});
await tournamentTeamsFullCached({ tournamentId });
await TournamentTeamFactory.create({
tournamentId,
memberUserIds: [users.id(3)],
});
return tournamentId;
};
test("keeps the rosters cached with keepRosters", async () => {
const tournamentId = await setupWithRosterCached();
clearTournamentDataCache(tournamentId, { keepRosters: true });
expect(await tournamentTeamsFullCached({ tournamentId })).toHaveLength(1);
});
test("drops the rosters by default", async () => {
const tournamentId = await setupWithRosterCached();
clearTournamentDataCache(tournamentId);
expect(await tournamentTeamsFullCached({ tournamentId })).toHaveLength(2);
});
});

View File

@@ -296,10 +296,21 @@ export async function notifyTournamentStatusChanged(
) {
if (userIds.length === 0) return;
syncTournamentToRegistry(await tournamentSharedCached(tournamentId));
await refreshTournamentCache(tournamentId);
ChatSystemMessage.notifyStatusChanged(userIds);
}
/**
* Rebuilds the tournament after a write into the (just cleared) cache and syncs the registry from it,
* so the revalidation that follows the action finds a warm cache instead of building it again.
*/
export async function refreshTournamentCache(tournamentId: number) {
const tournament = await tournamentSharedCached(tournamentId);
syncTournamentToRegistry(tournament);
return tournament;
}
const TOURNAMENT_DATA_CACHE_MAX_ENTRIES = 250;
const TOURNAMENT_DATA_CACHE_TTL_MS = minutesToMilliseconds(30);
@@ -525,9 +536,15 @@ function censoredTeams({
});
}
export function clearTournamentDataCache(tournamentId: number) {
/** `keepRosters` for writes that leave every team's members, check-ins, active roster and map pool as they were e.g. score reports. */
export function clearTournamentDataCache(
tournamentId: number,
{ keepRosters = false }: { keepRosters?: boolean } = {},
) {
tournamentDataCache.delete(tournamentId);
tournamentTeamsCache.delete(tournamentId);
if (!keepRosters) {
tournamentTeamsCache.delete(tournamentId);
}
}
export function clearAllTournamentDataCache() {

View File

@@ -17,8 +17,8 @@ import * as PickBan from "~/features/tournament-bracket/core/PickBan";
import type { Tournament } from "~/features/tournament-bracket/core/Tournament";
import {
clearTournamentDataCache,
refreshTournamentCache,
requireTournamentOrganizer,
tournamentFromDB,
tournamentFromParams,
} from "~/features/tournament-bracket/core/Tournament.server";
import {
@@ -103,6 +103,7 @@ export const action = defineAction(
let onlyMatchResultsChanged = false;
let setIsOver = false;
let emitStatusUpdate = false;
let rostersChanged = false;
let endedDroppedMatchIds: number[] = [];
let followingMatchIds: number[] = [];
@@ -164,6 +165,7 @@ export const action = defineAction(
});
emitMatchUpdate = true;
rostersChanged = true;
break;
}
@@ -881,11 +883,11 @@ export const action = defineAction(
}
}
clearTournamentDataCache(tournamentId);
clearTournamentDataCache(tournamentId, { keepRosters: !rostersChanged });
// refresh RunningTournaments so the sidebar doesn't show stale matches while the TO delays finalizing
if (setIsOver || emitStatusUpdate) {
const refreshedTournament = await tournamentFromDB(tournamentId);
const refreshedTournament = await refreshTournamentCache(tournamentId);
const followingMatches = refreshedTournament.followingMatches(match.id);
if (setIsOver) {