backend: Introduce RedisLockName enum for garbage collector lock management

This commit is contained in:
Carlos Santos 2025-03-18 10:58:08 +01:00
parent c8703868d1
commit a699bb3343
3 changed files with 9 additions and 8 deletions

View File

@ -1,3 +1,4 @@
export enum RedisKeyPrefix { export const enum RedisLockName {
LOCK = 'ov_meet_lock:', GARBAGE_COLLECTOR = 'room_garbage_collector',
RECORDING_ACTIVE = 'recording_active',
} }

View File

@ -1,13 +1,13 @@
import Redlock, { Lock } from 'redlock'; import Redlock, { Lock } from 'redlock';
import { RedisService } from './redis.service.js'; import { RedisService } from './redis.service.js';
import { inject, injectable } from 'inversify'; import { inject, injectable } from 'inversify';
import { RedisKeyPrefix } from '../models/redis.model.js';
@injectable() @injectable()
export class MutexService { export class MutexService {
protected redlockWithoutRetry: Redlock; protected redlockWithoutRetry: Redlock;
protected locks: Map<string, Lock>; protected locks: Map<string, Lock>;
protected readonly TTL_MS = 10_000; protected readonly TTL_MS = 10_000;
protected LOCK_KEY_PREFIX = 'ov_meet_lock:'
constructor(@inject(RedisService) protected redisService: RedisService) { constructor(@inject(RedisService) protected redisService: RedisService) {
this.redlockWithoutRetry = this.redisService.createRedlock(0); this.redlockWithoutRetry = this.redisService.createRedlock(0);
@ -21,7 +21,7 @@ export class MutexService {
* @returns A Promise that resolves to the acquired Lock object. * @returns A Promise that resolves to the acquired Lock object.
*/ */
async acquire(resource: string, ttl: number = this.TTL_MS): Promise<Lock | null> { async acquire(resource: string, ttl: number = this.TTL_MS): Promise<Lock | null> {
resource = RedisKeyPrefix.LOCK + resource; resource = this.LOCK_KEY_PREFIX + resource;
try { try {
const lock = await this.redlockWithoutRetry.acquire([resource], ttl); const lock = await this.redlockWithoutRetry.acquire([resource], ttl);
@ -39,7 +39,7 @@ export class MutexService {
* @returns A Promise that resolves when the lock is released. * @returns A Promise that resolves when the lock is released.
*/ */
async release(resource: string): Promise<void> { async release(resource: string): Promise<void> {
resource = RedisKeyPrefix.LOCK + resource; resource = this.LOCK_KEY_PREFIX + resource;
const lock = this.locks.get(resource); const lock = this.locks.get(resource);
if (lock) { if (lock) {

View File

@ -3,6 +3,7 @@ import { LoggerService } from './index.js';
import { SystemEventService } from './system-event.service.js'; import { SystemEventService } from './system-event.service.js';
import { CronJob } from 'cron'; import { CronJob } from 'cron';
import { MutexService } from './mutex.service.js'; import { MutexService } from './mutex.service.js';
import { RedisLockName } from '../models/redis.model.js';
@injectable() @injectable()
export class TaskSchedulerService { export class TaskSchedulerService {
@ -23,7 +24,6 @@ export class TaskSchedulerService {
* @returns A promise that resolves when the garbage collector has been successfully started. * @returns A promise that resolves when the garbage collector has been successfully started.
*/ */
async startRoomGarbageCollector(callbackFn: () => Promise<void>): Promise<void> { async startRoomGarbageCollector(callbackFn: () => Promise<void>): Promise<void> {
const lockName = 'room-garbage-lock';
const lockTtl = 59 * 60 * 1000; // TTL of 59 minutes const lockTtl = 59 * 60 * 1000; // TTL of 59 minutes
if (this.roomGarbageCollectorJob) { if (this.roomGarbageCollectorJob) {
@ -34,7 +34,7 @@ export class TaskSchedulerService {
// Create a cron job to run every hour // Create a cron job to run every hour
this.roomGarbageCollectorJob = new CronJob('0 * * * *', async () => { this.roomGarbageCollectorJob = new CronJob('0 * * * *', async () => {
try { try {
const lock = await this.mutexService.acquire(lockName, lockTtl); const lock = await this.mutexService.acquire(RedisLockName.GARBAGE_COLLECTOR, lockTtl);
if (!lock) { if (!lock) {
this.logger.debug('Failed to acquire lock for room garbage collection. Skipping.'); this.logger.debug('Failed to acquire lock for room garbage collection. Skipping.');