backend: Introduce RedisLockName enum for garbage collector lock management
This commit is contained in:
parent
c8703868d1
commit
a699bb3343
@ -1,3 +1,4 @@
|
||||
export enum RedisKeyPrefix {
|
||||
LOCK = 'ov_meet_lock:',
|
||||
}
|
||||
export const enum RedisLockName {
|
||||
GARBAGE_COLLECTOR = 'room_garbage_collector',
|
||||
RECORDING_ACTIVE = 'recording_active',
|
||||
}
|
||||
@ -1,13 +1,13 @@
|
||||
import Redlock, { Lock } from 'redlock';
|
||||
import { RedisService } from './redis.service.js';
|
||||
import { inject, injectable } from 'inversify';
|
||||
import { RedisKeyPrefix } from '../models/redis.model.js';
|
||||
|
||||
@injectable()
|
||||
export class MutexService {
|
||||
protected redlockWithoutRetry: Redlock;
|
||||
protected locks: Map<string, Lock>;
|
||||
protected readonly TTL_MS = 10_000;
|
||||
protected LOCK_KEY_PREFIX = 'ov_meet_lock:'
|
||||
|
||||
constructor(@inject(RedisService) protected redisService: RedisService) {
|
||||
this.redlockWithoutRetry = this.redisService.createRedlock(0);
|
||||
@ -21,7 +21,7 @@ export class MutexService {
|
||||
* @returns A Promise that resolves to the acquired Lock object.
|
||||
*/
|
||||
async acquire(resource: string, ttl: number = this.TTL_MS): Promise<Lock | null> {
|
||||
resource = RedisKeyPrefix.LOCK + resource;
|
||||
resource = this.LOCK_KEY_PREFIX + resource;
|
||||
|
||||
try {
|
||||
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.
|
||||
*/
|
||||
async release(resource: string): Promise<void> {
|
||||
resource = RedisKeyPrefix.LOCK + resource;
|
||||
resource = this.LOCK_KEY_PREFIX + resource;
|
||||
const lock = this.locks.get(resource);
|
||||
|
||||
if (lock) {
|
||||
|
||||
@ -3,6 +3,7 @@ import { LoggerService } from './index.js';
|
||||
import { SystemEventService } from './system-event.service.js';
|
||||
import { CronJob } from 'cron';
|
||||
import { MutexService } from './mutex.service.js';
|
||||
import { RedisLockName } from '../models/redis.model.js';
|
||||
|
||||
@injectable()
|
||||
export class TaskSchedulerService {
|
||||
@ -23,7 +24,6 @@ export class TaskSchedulerService {
|
||||
* @returns A promise that resolves when the garbage collector has been successfully started.
|
||||
*/
|
||||
async startRoomGarbageCollector(callbackFn: () => Promise<void>): Promise<void> {
|
||||
const lockName = 'room-garbage-lock';
|
||||
const lockTtl = 59 * 60 * 1000; // TTL of 59 minutes
|
||||
|
||||
if (this.roomGarbageCollectorJob) {
|
||||
@ -34,7 +34,7 @@ export class TaskSchedulerService {
|
||||
// Create a cron job to run every hour
|
||||
this.roomGarbageCollectorJob = new CronJob('0 * * * *', async () => {
|
||||
try {
|
||||
const lock = await this.mutexService.acquire(lockName, lockTtl);
|
||||
const lock = await this.mutexService.acquire(RedisLockName.GARBAGE_COLLECTOR, lockTtl);
|
||||
|
||||
if (!lock) {
|
||||
this.logger.debug('Failed to acquire lock for room garbage collection. Skipping.');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user