import { OnModuleInit } from '@nestjs/common'; import { Connection } from 'mongoose'; /** * LockService * * This service manages distributed locks using MongoDB. Each lock is uniquely identified by a `lockKey` * and associated with a service via `serviceId`. Locks ensure that only one service instance can perform * operations on a given resource at any time. Locks are automatically released after a specified timeout * to prevent deadlocks. */ export declare class LockService implements OnModuleInit { private readonly databaseConnection; private readonly logger; private readonly lockModel; private readonly serviceId; private readonly lockDuration; /** * Initializes the LockService with an injected MongoDB connection. * Sets up the lock model based on the Lock schema. * * @param databaseConnection Injected Mongoose connection instance. */ constructor(databaseConnection: Connection); /** * Ensures the lock model indexes and schema are initialized after module setup. */ onModuleInit(): Promise; /** * Attempts to acquire a distributed lock for the given key. * * The lock is acquired only if: * - No current lock exists for the key (i.e., serviceId field does not exist) * - Or the existing lock has expired (expiresAt less than current time) * * If successful, the lock document is created or updated with the current serviceId * and a new expiration timestamp. * * This atomic operation avoids race conditions where multiple services * attempt to acquire the same lock simultaneously. * * @param lockKey Unique identifier for the resource to lock. * @returns True if the lock was acquired successfully; false otherwise. */ acquireLock(lockKey: string): Promise; /** * Releases the lock for the specified key, but only if currently owned by this service instance. * * This prevents accidentally releasing locks held by other services. * * @param lockKey Unique identifier for the lock to release. */ releaseLock(lockKey: string): Promise; }