111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import type {
|
|
AuctionMatchSideEffectStatus,
|
|
AuctionSideEffectApplicationState,
|
|
} from '../global/database/schema';
|
|
import { buildAuctionRedisKeys } from './redis/auction-redis.keys';
|
|
import { AuctionRedisService } from './redis/auction-redis.service';
|
|
|
|
export interface SideEffectApplicationIdentity {
|
|
auctionId: unknown;
|
|
auctionStateTransitionId: string;
|
|
sideEffectType: string;
|
|
}
|
|
|
|
export interface SideEffectLeaseState {
|
|
processingLeaseExpiresAtTimestampMS?: number;
|
|
state: AuctionSideEffectApplicationState;
|
|
}
|
|
|
|
export interface BuildSideEffectLeaseAcquireUpdateParams {
|
|
currentRedisTimestampMS: number;
|
|
leaseDurationMS: number;
|
|
ownerToken: string;
|
|
}
|
|
|
|
export interface MatchSideEffectDelta {
|
|
auctionLifecycleId: string;
|
|
currentServerTimestampMS: number;
|
|
matchSideEffectRevision: number;
|
|
matchSideEffectStatus: AuctionMatchSideEffectStatus;
|
|
}
|
|
|
|
@Injectable()
|
|
export class AuctionSideEffectService {
|
|
constructor(private readonly auctionRedisService: AuctionRedisService) {}
|
|
|
|
public async mirrorMatchSideEffectStatus(params: {
|
|
auctionLifecycleId: string;
|
|
matchId: string;
|
|
matchSideEffectRevision: number;
|
|
matchSideEffectStatus: AuctionMatchSideEffectStatus;
|
|
}): Promise<boolean> {
|
|
const keys = buildAuctionRedisKeys(params.matchId);
|
|
const result = await this.auctionRedisService.raw().sendCommand([
|
|
'EVAL',
|
|
[
|
|
"if redis.call('HGET', KEYS[1], 'auctionLifecycleId') ~= ARGV[1] then return 0 end",
|
|
"local currentRevision = tonumber(redis.call('HGET', KEYS[1], 'matchSideEffectRevision') or '-1')",
|
|
'local nextRevision = tonumber(ARGV[2])',
|
|
'if currentRevision >= nextRevision then return 0 end',
|
|
"redis.call('HSET', KEYS[1], 'matchSideEffectRevision', ARGV[2], 'matchSideEffectStatus', ARGV[3])",
|
|
'return 1',
|
|
].join('\n'),
|
|
'1',
|
|
keys.liveStateKey,
|
|
params.auctionLifecycleId,
|
|
String(params.matchSideEffectRevision),
|
|
params.matchSideEffectStatus,
|
|
]);
|
|
return Number(result) === 1;
|
|
}
|
|
}
|
|
|
|
export function buildSideEffectApplicationIdentity(
|
|
params: SideEffectApplicationIdentity,
|
|
): SideEffectApplicationIdentity {
|
|
return {
|
|
auctionId: params.auctionId,
|
|
auctionStateTransitionId: params.auctionStateTransitionId,
|
|
sideEffectType: params.sideEffectType,
|
|
};
|
|
}
|
|
|
|
export function canAcquireSideEffectLease(
|
|
state: SideEffectLeaseState,
|
|
currentRedisTimestampMS: number,
|
|
): boolean {
|
|
if (state.state === 'pending' || state.state === 'failed') return true;
|
|
if (state.state !== 'processing') return false;
|
|
return (
|
|
(state.processingLeaseExpiresAtTimestampMS ?? 0) <=
|
|
currentRedisTimestampMS
|
|
);
|
|
}
|
|
|
|
export function buildSideEffectLeaseAcquireUpdate(
|
|
params: BuildSideEffectLeaseAcquireUpdateParams,
|
|
) {
|
|
return {
|
|
$inc: { attempts: 1 },
|
|
$set: {
|
|
processingLeaseExpiresAtTimestampMS:
|
|
params.currentRedisTimestampMS + params.leaseDurationMS,
|
|
processingOwnerToken: params.ownerToken,
|
|
processingStartedAtTimestampMS: params.currentRedisTimestampMS,
|
|
state: 'processing',
|
|
},
|
|
};
|
|
}
|
|
|
|
export function buildMatchSideEffectDelta(
|
|
params: MatchSideEffectDelta,
|
|
): MatchSideEffectDelta {
|
|
return {
|
|
auctionLifecycleId: params.auctionLifecycleId,
|
|
currentServerTimestampMS: params.currentServerTimestampMS,
|
|
matchSideEffectRevision: params.matchSideEffectRevision,
|
|
matchSideEffectStatus: params.matchSideEffectStatus,
|
|
};
|
|
}
|