diff --git a/apps/api/README.md b/apps/api/README.md index a5a96613..1b15cc36 100644 --- a/apps/api/README.md +++ b/apps/api/README.md @@ -27,8 +27,8 @@ ## Riot Background Refresh -Set `RIOT_BG_REFRESH_ENABLED=true` to refresh stale Riot accounts automatically -between KST 03:00 and 10:00. The scheduler picks active users whose +Stale Riot accounts refresh automatically between KST 03:00 and 10:00. +The scheduler picks active users whose `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. diff --git a/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.spec.ts b/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.spec.ts index 01b88226..ad54fcd5 100644 --- a/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.spec.ts +++ b/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.spec.ts @@ -1,4 +1,3 @@ -import { ConfigService } from '@nestjs/config'; import mongoose from 'mongoose'; import { GarnetService } from '../global/garnet/garnet.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 = { sendCommand: jest.fn().mockResolvedValue(1), }; @@ -51,16 +50,11 @@ function makeScheduler(enabled = true) { const staleAccountRepository = { findStaleAccounts: jest.fn().mockResolvedValue([]), } as unknown as jest.Mocked; - const config = { - get: jest.fn().mockReturnValue(enabled ? 'true' : 'false'), - } as unknown as ConfigService; - const scheduler = new BackgroundRefreshScheduler( garnet, rateLimiter, enrichmentService, staleAccountRepository, - config, ) as SchedulerInternals; return { @@ -79,14 +73,15 @@ describe('BackgroundRefreshScheduler', () => { jest.restoreAllMocks(); }); - it('does not schedule ticks when disabled', () => { + it('schedules ticks without requiring RIOT_BG_REFRESH_ENABLED', () => { jest.useFakeTimers(); const setIntervalSpy = jest.spyOn(global, 'setInterval'); - const { scheduler } = makeScheduler(false); + const { scheduler } = makeScheduler(); scheduler.onModuleInit(); - expect(setIntervalSpy).not.toHaveBeenCalled(); + expect(setIntervalSpy).toHaveBeenCalledTimes(2); + scheduler.onModuleDestroy(); }); it('does not query stale accounts outside the KST window', async () => { diff --git a/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.ts b/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.ts index 730f3e1d..a95448fb 100644 --- a/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.ts +++ b/apps/api/src/modules/riot-background-refresh/background-refresh.scheduler.ts @@ -4,7 +4,6 @@ import { OnModuleDestroy, OnModuleInit, } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; import { type EnrichResult, RiotAccountEnrichmentService, @@ -34,26 +33,15 @@ export class BackgroundRefreshScheduler private leaderRenewTimer?: NodeJS.Timeout; private tickInProgress = false; private readonly consumer = `${process.pid}:${Math.random().toString(36).slice(2)}`; - private readonly enabled: boolean; constructor( private readonly garnet: GarnetService, private readonly rateLimiter: RiotRateLimiterService, private readonly enrichmentService: RiotAccountEnrichmentService, private readonly staleAccountRepository: StaleAccountRepository, - config: ConfigService, - ) { - this.enabled = config.get('RIOT_BG_REFRESH_ENABLED') === 'true'; - } + ) {} 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.leaderRenewTimer = setInterval( () => void this.renewLeaderIfHeld(),