fix: fully cancel redis auctions
This commit is contained in:
1 parent
e8bb20d988
commit
7c372f0392
3 files changed
+221
-107
No files matched your search
@@ -15,6 +15,7 @@ import {
|
||||
AuctionPlayerSnapshot,
|
||||
AuctionTeam,
|
||||
AuctionTopTierThreshold,
|
||||
MatchDocument,
|
||||
MatchParticipant,
|
||||
MatchTeamMember,
|
||||
Position,
|
||||
@@ -1128,16 +1129,18 @@ export class AuctionService {
|
||||
const AuctionModel = await this.connectionService.getModel('auction');
|
||||
const AuctionHistoryModel =
|
||||
await this.connectionService.getModel('auctionHistory');
|
||||
const AuctionSideEffectApplicationModel =
|
||||
await this.connectionService.getModel('auctionSideEffectApplication');
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new InternalServerErrorException('Failed to start DB session.');
|
||||
}
|
||||
let redisResetContext:
|
||||
let redisRuntimeToClear:
|
||||
| {
|
||||
actionRequestParams?: AuctionStructureActionRequestParams;
|
||||
auction: AuctionDocument;
|
||||
auctionLifecycleId?: string;
|
||||
matchId: string;
|
||||
}
|
||||
| null = null;
|
||||
try {
|
||||
@@ -1154,10 +1157,18 @@ export class AuctionService {
|
||||
}
|
||||
|
||||
if (this.isRedisV1Auction(auction)) {
|
||||
redisResetContext = {
|
||||
auction,
|
||||
actionRequestParams: params,
|
||||
redisRuntimeToClear = {
|
||||
auctionLifecycleId: auction.auctionLifecycleId,
|
||||
matchId: params.matchId.toString(),
|
||||
};
|
||||
await this.resetMatchAfterAuctionCancellation(match, session);
|
||||
await AuctionSideEffectApplicationModel.deleteMany({
|
||||
auctionId: auction._id,
|
||||
}).session(session);
|
||||
await AuctionHistoryModel.deleteMany({ auctionId: auction._id }).session(
|
||||
session,
|
||||
);
|
||||
await auction.deleteOne({ session });
|
||||
} else {
|
||||
await auction.deleteOne({ session });
|
||||
await AuctionHistoryModel.deleteMany({ auctionId: auction._id }).session(
|
||||
@@ -1186,8 +1197,11 @@ export class AuctionService {
|
||||
await session.endSession();
|
||||
}
|
||||
|
||||
if (redisResetContext) {
|
||||
await this.resetRedisAuction(redisResetContext);
|
||||
if (redisRuntimeToClear) {
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
redisRuntimeToClear.matchId,
|
||||
redisRuntimeToClear.auctionLifecycleId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2223,6 +2237,25 @@ export class AuctionService {
|
||||
);
|
||||
}
|
||||
|
||||
private async resetMatchAfterAuctionCancellation(
|
||||
match: MatchDocument,
|
||||
session: mongoose.ClientSession,
|
||||
): Promise<void> {
|
||||
await this.tournamentCodeService.releaseFromMatch(match);
|
||||
await this.tournamentResultService.cancelPolling(match);
|
||||
match.status = 'recruiting';
|
||||
match.teams = [];
|
||||
match.tournamentCodeIds = [];
|
||||
match.gameSchedule = [];
|
||||
match.participants.forEach((participant) => {
|
||||
participant.excluded = false;
|
||||
});
|
||||
match.teamBuiltAt = undefined;
|
||||
match.result = undefined;
|
||||
match.resultPollState = undefined;
|
||||
await match.save({ session });
|
||||
}
|
||||
|
||||
private assertLiveAuctionOperationSupported(auction: AuctionDocument): void {
|
||||
if (auction.auctionEngineVersion && !this.isRedisV1Auction(auction)) {
|
||||
throw new BadRequestException(
|
||||
@@ -2385,13 +2418,13 @@ export class AuctionService {
|
||||
expectedAuctionLifecycleId?: string,
|
||||
): Promise<void> {
|
||||
const keys = buildAuctionRedisKeys(matchId);
|
||||
await this.auctionRedisService.raw().sendCommand([
|
||||
const cleared = await this.auctionRedisService.raw().sendCommand([
|
||||
'EVAL',
|
||||
[
|
||||
"local expectedAuctionLifecycleId = ARGV[2]",
|
||||
"if expectedAuctionLifecycleId ~= '' then",
|
||||
" local currentAuctionLifecycleId = redis.call('HGET', KEYS[1], 'auctionLifecycleId')",
|
||||
" if currentAuctionLifecycleId ~= expectedAuctionLifecycleId then",
|
||||
" if currentAuctionLifecycleId and currentAuctionLifecycleId ~= expectedAuctionLifecycleId then",
|
||||
" return 0",
|
||||
' end',
|
||||
'end',
|
||||
@@ -2414,6 +2447,75 @@ export class AuctionService {
|
||||
matchId,
|
||||
expectedAuctionLifecycleId ?? '',
|
||||
]);
|
||||
if (Number(cleared) !== 1) return;
|
||||
await this.clearRedisDurabilityPayloadsForLifecycle(
|
||||
keys,
|
||||
expectedAuctionLifecycleId,
|
||||
);
|
||||
}
|
||||
|
||||
private async clearRedisDurabilityPayloadsForLifecycle(
|
||||
keys: ReturnType<typeof buildAuctionRedisKeys>,
|
||||
expectedAuctionLifecycleId?: string,
|
||||
): Promise<void> {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const reply = await this.auctionRedisService.raw().sendCommand([
|
||||
'SCAN',
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${keys.durabilityPayloadKeyPrefix}*`,
|
||||
'COUNT',
|
||||
'100',
|
||||
]);
|
||||
if (!Array.isArray(reply) || reply.length < 2) return;
|
||||
cursor = String(reply[0]);
|
||||
const payloadKeys = Array.isArray(reply[1]) ? reply[1].map(String) : [];
|
||||
if (payloadKeys.length === 0) continue;
|
||||
|
||||
const values = await this.auctionRedisService
|
||||
.raw()
|
||||
.sendCommand(['MGET', ...payloadKeys]);
|
||||
const payloadKeysToDelete = payloadKeys.filter((payloadKey, index) =>
|
||||
this.shouldDeleteRedisDurabilityPayload(
|
||||
Array.isArray(values) ? values[index] : undefined,
|
||||
expectedAuctionLifecycleId,
|
||||
payloadKey,
|
||||
),
|
||||
);
|
||||
if (payloadKeysToDelete.length === 0) continue;
|
||||
|
||||
await this.auctionRedisService.raw().sendCommand([
|
||||
'ZREM',
|
||||
keys.durabilityRetryQueueKey,
|
||||
...payloadKeysToDelete,
|
||||
]);
|
||||
await this.auctionRedisService.raw().sendCommand([
|
||||
'ZREM',
|
||||
keys.durabilityDeadLetterQueueKey,
|
||||
...payloadKeysToDelete,
|
||||
]);
|
||||
await this.auctionRedisService.raw().sendCommand([
|
||||
'DEL',
|
||||
...payloadKeysToDelete,
|
||||
]);
|
||||
} while (cursor !== '0');
|
||||
}
|
||||
|
||||
private shouldDeleteRedisDurabilityPayload(
|
||||
payloadValue: unknown,
|
||||
expectedAuctionLifecycleId: string | undefined,
|
||||
payloadKey: string,
|
||||
): boolean {
|
||||
if (!expectedAuctionLifecycleId) return true;
|
||||
if (typeof payloadValue !== 'string') return false;
|
||||
try {
|
||||
const payload = JSON.parse(payloadValue) as Record<string, unknown>;
|
||||
return payload.auctionLifecycleId === expectedAuctionLifecycleId;
|
||||
} catch {
|
||||
this.logger.warn(`Failed to parse auction durability payload ${payloadKey}.`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private enqueueBackgroundOrderEnrichment(params: {
|
||||
|
||||
Reference in new issue
Block a user