fix: harden multi-instance scheduler and worker execution
This commit is contained in:
1 parent
df7dcd3a9c
commit
60a006008a
23 files changed
+1442
-299
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { DistributedLockService } from '../global/redis/distributed-lock.service';
|
||||
import { RedisService } from '../global/redis/redis.service';
|
||||
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
||||
import { RiotRateLimiterService } from '../riot/riot-rate-limiter.service';
|
||||
@@ -35,11 +36,18 @@ function makeScheduler() {
|
||||
};
|
||||
const redis = {
|
||||
getString: jest.fn().mockResolvedValue(null),
|
||||
setString: jest.fn().mockResolvedValue(undefined),
|
||||
setStringIfAbsent: jest.fn().mockResolvedValue(true),
|
||||
delete: jest.fn().mockResolvedValue(1),
|
||||
raw: jest.fn().mockReturnValue(rawClient),
|
||||
} as unknown as jest.Mocked<RedisService> & { raw: jest.Mock };
|
||||
const distributedLockService = {
|
||||
acquireLock: jest.fn().mockResolvedValue({
|
||||
lockKey: 'distributed-lock:riot-background-refresh:leader',
|
||||
lockOwnershipToken: 'lock-owner',
|
||||
lockExpirationDurationMilliseconds: 90_000,
|
||||
}),
|
||||
extendLockIfOwned: jest.fn().mockResolvedValue(true),
|
||||
releaseLockIfOwned: jest.fn().mockResolvedValue(true),
|
||||
} as unknown as jest.Mocked<DistributedLockService>;
|
||||
const rateLimiter = {
|
||||
isRefreshWindowOpen: jest.fn().mockResolvedValue(true),
|
||||
} as unknown as jest.Mocked<RiotRateLimiterService>;
|
||||
@@ -52,6 +60,7 @@ function makeScheduler() {
|
||||
} as unknown as jest.Mocked<StaleAccountRepository>;
|
||||
const scheduler = new BackgroundRefreshScheduler(
|
||||
redis,
|
||||
distributedLockService,
|
||||
rateLimiter,
|
||||
enrichmentService,
|
||||
staleAccountRepository,
|
||||
@@ -60,6 +69,7 @@ function makeScheduler() {
|
||||
return {
|
||||
scheduler,
|
||||
redis,
|
||||
distributedLockService,
|
||||
rawClient,
|
||||
rateLimiter,
|
||||
enrichmentService,
|
||||
@@ -115,8 +125,9 @@ describe('BackgroundRefreshScheduler', () => {
|
||||
|
||||
it('does not query stale accounts when another leader holds the lock', async () => {
|
||||
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
|
||||
const { scheduler, redis, staleAccountRepository } = makeScheduler();
|
||||
redis.getString.mockResolvedValue('another-consumer');
|
||||
const { scheduler, distributedLockService, staleAccountRepository } =
|
||||
makeScheduler();
|
||||
distributedLockService.acquireLock.mockResolvedValueOnce(null);
|
||||
|
||||
await scheduler.tick();
|
||||
|
||||
|
||||
Reference in new issue
Block a user