feat: run Riot background refresh by default
This commit is contained in:
1 parent
528744ebf6
commit
1c0af891fa
3 files changed
+8
-25
No files matched your search
+2
-2
@@ -27,8 +27,8 @@
|
|||||||
|
|
||||||
## Riot Background Refresh
|
## Riot Background Refresh
|
||||||
|
|
||||||
Set `RIOT_BG_REFRESH_ENABLED=true` to refresh stale Riot accounts automatically
|
Stale Riot accounts refresh automatically between KST 03:00 and 10:00.
|
||||||
between KST 03:00 and 10:00. The scheduler picks active users whose
|
The scheduler picks active users whose
|
||||||
`lastRefreshRequestedAt` is older than 6 hours, oldest first, and uses a
|
`lastRefreshRequestedAt` is older than 6 hours, oldest first, and uses a
|
||||||
background-only Riot token bucket capped at roughly 40% of the app rate limit.
|
background-only Riot token bucket capped at roughly 40% of the app rate limit.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import { GarnetService } from '../global/garnet/garnet.service';
|
import { GarnetService } from '../global/garnet/garnet.service';
|
||||||
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
||||||
@@ -30,7 +29,7 @@ function candidate(puuid: string): StaleAccountCandidate {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeScheduler(enabled = true) {
|
function makeScheduler() {
|
||||||
const rawClient = {
|
const rawClient = {
|
||||||
sendCommand: jest.fn().mockResolvedValue(1),
|
sendCommand: jest.fn().mockResolvedValue(1),
|
||||||
};
|
};
|
||||||
@@ -51,16 +50,11 @@ function makeScheduler(enabled = true) {
|
|||||||
const staleAccountRepository = {
|
const staleAccountRepository = {
|
||||||
findStaleAccounts: jest.fn().mockResolvedValue([]),
|
findStaleAccounts: jest.fn().mockResolvedValue([]),
|
||||||
} as unknown as jest.Mocked<StaleAccountRepository>;
|
} as unknown as jest.Mocked<StaleAccountRepository>;
|
||||||
const config = {
|
|
||||||
get: jest.fn().mockReturnValue(enabled ? 'true' : 'false'),
|
|
||||||
} as unknown as ConfigService;
|
|
||||||
|
|
||||||
const scheduler = new BackgroundRefreshScheduler(
|
const scheduler = new BackgroundRefreshScheduler(
|
||||||
garnet,
|
garnet,
|
||||||
rateLimiter,
|
rateLimiter,
|
||||||
enrichmentService,
|
enrichmentService,
|
||||||
staleAccountRepository,
|
staleAccountRepository,
|
||||||
config,
|
|
||||||
) as SchedulerInternals;
|
) as SchedulerInternals;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -79,14 +73,15 @@ describe('BackgroundRefreshScheduler', () => {
|
|||||||
jest.restoreAllMocks();
|
jest.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not schedule ticks when disabled', () => {
|
it('schedules ticks without requiring RIOT_BG_REFRESH_ENABLED', () => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
const setIntervalSpy = jest.spyOn(global, 'setInterval');
|
const setIntervalSpy = jest.spyOn(global, 'setInterval');
|
||||||
const { scheduler } = makeScheduler(false);
|
const { scheduler } = makeScheduler();
|
||||||
|
|
||||||
scheduler.onModuleInit();
|
scheduler.onModuleInit();
|
||||||
|
|
||||||
expect(setIntervalSpy).not.toHaveBeenCalled();
|
expect(setIntervalSpy).toHaveBeenCalledTimes(2);
|
||||||
|
scheduler.onModuleDestroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not query stale accounts outside the KST window', async () => {
|
it('does not query stale accounts outside the KST window', async () => {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import {
|
|||||||
OnModuleDestroy,
|
OnModuleDestroy,
|
||||||
OnModuleInit,
|
OnModuleInit,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import {
|
import {
|
||||||
type EnrichResult,
|
type EnrichResult,
|
||||||
RiotAccountEnrichmentService,
|
RiotAccountEnrichmentService,
|
||||||
@@ -34,26 +33,15 @@ export class BackgroundRefreshScheduler
|
|||||||
private leaderRenewTimer?: NodeJS.Timeout;
|
private leaderRenewTimer?: NodeJS.Timeout;
|
||||||
private tickInProgress = false;
|
private tickInProgress = false;
|
||||||
private readonly consumer = `${process.pid}:${Math.random().toString(36).slice(2)}`;
|
private readonly consumer = `${process.pid}:${Math.random().toString(36).slice(2)}`;
|
||||||
private readonly enabled: boolean;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly garnet: GarnetService,
|
private readonly garnet: GarnetService,
|
||||||
private readonly rateLimiter: RiotRateLimiterService,
|
private readonly rateLimiter: RiotRateLimiterService,
|
||||||
private readonly enrichmentService: RiotAccountEnrichmentService,
|
private readonly enrichmentService: RiotAccountEnrichmentService,
|
||||||
private readonly staleAccountRepository: StaleAccountRepository,
|
private readonly staleAccountRepository: StaleAccountRepository,
|
||||||
config: ConfigService,
|
) {}
|
||||||
) {
|
|
||||||
this.enabled = config.get<string>('RIOT_BG_REFRESH_ENABLED') === 'true';
|
|
||||||
}
|
|
||||||
|
|
||||||
public onModuleInit(): void {
|
public onModuleInit(): void {
|
||||||
if (!this.enabled) {
|
|
||||||
this.logger.log(
|
|
||||||
'Background Riot refresh is disabled (RIOT_BG_REFRESH_ENABLED).',
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.tickTimer = setInterval(() => void this.safeTick(), TICK_INTERVAL_MS);
|
this.tickTimer = setInterval(() => void this.safeTick(), TICK_INTERVAL_MS);
|
||||||
this.leaderRenewTimer = setInterval(
|
this.leaderRenewTimer = setInterval(
|
||||||
() => void this.renewLeaderIfHeld(),
|
() => void this.renewLeaderIfHeld(),
|
||||||
|
|||||||
Reference in new issue
Block a user