redis-smq-common
Advanced tools
Comparing version 1.0.1 to 1.0.2
# CHANGELOG | ||
## 1.0.2 (2022-08-10) | ||
* Update LockManager tests (321c8c4) | ||
* Fix "LockManagerExtendError: Acquired lock could not be extended" (fa3a8e5) | ||
## 1.0.1 (2022-07-07) | ||
@@ -4,0 +9,0 @@ |
/// <reference types="node" /> | ||
import { ICallback } from '../../types'; | ||
import { RedisClient } from '../redis-client/redis-client'; | ||
declare enum ELockStatus { | ||
export declare enum ELockStatus { | ||
unlocked = 0, | ||
@@ -9,3 +9,4 @@ locking = 1, | ||
releasing = 3, | ||
extending = 4 | ||
extending = 4, | ||
extended = 5 | ||
} | ||
@@ -26,6 +27,8 @@ export declare enum ELuaScript { | ||
protected autoExtendTimer: NodeJS.Timeout | null; | ||
constructor(redisClient: RedisClient, lockKey: string, ttl: number, retryOnFail?: boolean, autoExtend?: boolean); | ||
protected throwExceptions: boolean; | ||
constructor(redisClient: RedisClient, lockKey: string, ttl: number, retryOnFail?: boolean, autoExtend?: boolean, throwExceptions?: boolean); | ||
protected resetTimers(): void; | ||
protected setUnlocked(): void; | ||
protected setLocked(): void; | ||
protected setExtended(): void; | ||
protected extend(cb: ICallback<void>): void; | ||
@@ -36,5 +39,6 @@ protected runAutoExtendTimer(): void; | ||
releaseLock(cb: ICallback<void>): void; | ||
acquireOrExtend(cb: ICallback<ELockStatus>): void; | ||
isLocked(): boolean; | ||
isReleased(): boolean; | ||
getId(): string; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LockManager = exports.ELuaScript = void 0; | ||
exports.LockManager = exports.ELuaScript = exports.ELockStatus = void 0; | ||
const redis_client_1 = require("../redis-client/redis-client"); | ||
const uuid_1 = require("uuid"); | ||
const lock_manager_error_1 = require("./errors/lock-manager.error"); | ||
const lock_manager_abort_error_1 = require("./errors/lock-manager-abort.error"); | ||
@@ -11,2 +10,5 @@ const lock_manager_extend_error_1 = require("./errors/lock-manager-extend.error"); | ||
const fs = require("fs"); | ||
const lock_manager_method_not_allowed_error_1 = require("./errors/lock-manager-method-not-allowed.error"); | ||
const lock_manager_not_acquired_error_1 = require("./errors/lock-manager-not-acquired.error"); | ||
const lock_manager_not_released_error_1 = require("./errors/lock-manager-not-released.error"); | ||
var ELockStatus; | ||
@@ -19,3 +21,4 @@ (function (ELockStatus) { | ||
ELockStatus[ELockStatus["extending"] = 4] = "extending"; | ||
})(ELockStatus || (ELockStatus = {})); | ||
ELockStatus[ELockStatus["extended"] = 5] = "extended"; | ||
})(ELockStatus = exports.ELockStatus || (exports.ELockStatus = {})); | ||
var ELuaScript; | ||
@@ -29,6 +32,7 @@ (function (ELuaScript) { | ||
class LockManager { | ||
constructor(redisClient, lockKey, ttl, retryOnFail = false, autoExtend = false) { | ||
constructor(redisClient, lockKey, ttl, retryOnFail = false, autoExtend = false, throwExceptions = true) { | ||
this.status = ELockStatus.unlocked; | ||
this.lockingTimer = null; | ||
this.autoExtendTimer = null; | ||
this.throwExceptions = true; | ||
this.lockKey = lockKey; | ||
@@ -40,2 +44,3 @@ this.ttl = ttl; | ||
this.autoExtend = autoExtend; | ||
this.throwExceptions = throwExceptions; | ||
} | ||
@@ -58,6 +63,8 @@ resetTimers() { | ||
} | ||
setExtended() { | ||
this.status = ELockStatus.extended; | ||
} | ||
extend(cb) { | ||
if (this.status !== ELockStatus.locked) { | ||
cb(new lock_manager_error_1.LockManagerError('The lock is currently not acquired or a pending operation is in progress')); | ||
} | ||
if (!this.isLocked()) | ||
cb(new lock_manager_not_acquired_error_1.LockManagerNotAcquiredError()); | ||
else { | ||
@@ -75,3 +82,3 @@ this.status = ELockStatus.extending; | ||
else { | ||
this.setLocked(); | ||
this.setExtended(); | ||
cb(); | ||
@@ -92,3 +99,4 @@ } | ||
this.runAutoExtendTimer(); | ||
else if (!(err instanceof lock_manager_abort_error_1.LockManagerAbortError)) | ||
else if (this.throwExceptions && | ||
!(err instanceof lock_manager_abort_error_1.LockManagerAbortError)) | ||
throw err; | ||
@@ -98,5 +106,4 @@ }), ms); | ||
acquireLock(cb) { | ||
if (this.status !== ELockStatus.unlocked) { | ||
cb(new lock_manager_error_1.LockManagerError(`The lock is currently not released or a pending operation is in progress`)); | ||
} | ||
if (!this.isReleased()) | ||
cb(new lock_manager_not_released_error_1.LockManagerNotReleasedError()); | ||
else { | ||
@@ -145,5 +152,4 @@ this.status = ELockStatus.locking; | ||
extendLock(cb) { | ||
if (this.autoExtend) { | ||
cb(new lock_manager_error_1.LockManagerError(`Can not extend a lock when autoExtend is enabled`)); | ||
} | ||
if (this.autoExtend) | ||
cb(new lock_manager_method_not_allowed_error_1.LockManagerMethodNotAllowedError()); | ||
else | ||
@@ -154,6 +160,6 @@ this.extend(cb); | ||
const status = this.status; | ||
if (status === ELockStatus.releasing) | ||
cb(new lock_manager_error_1.LockManagerError('A pending releaseLock() call is in progress')); | ||
else if (status === ELockStatus.unlocked) | ||
if (status === ELockStatus.unlocked) | ||
cb(); | ||
else if (!this.isLocked()) | ||
cb(new lock_manager_not_acquired_error_1.LockManagerNotAcquiredError()); | ||
else { | ||
@@ -172,6 +178,35 @@ this.resetTimers(); | ||
} | ||
acquireOrExtend(cb) { | ||
if (this.autoExtend) | ||
cb(new lock_manager_method_not_allowed_error_1.LockManagerMethodNotAllowedError()); | ||
else { | ||
const lock = () => { | ||
this.acquireLock((err) => { | ||
if (err) | ||
cb(err); | ||
else | ||
cb(null, ELockStatus.locked); | ||
}); | ||
}; | ||
if (this.isLocked()) | ||
this.extend((err) => { | ||
if (err) { | ||
if (err instanceof lock_manager_extend_error_1.LockManagerExtendError) | ||
lock(); | ||
else | ||
cb(err); | ||
} | ||
else | ||
cb(null, ELockStatus.extended); | ||
}); | ||
else | ||
lock(); | ||
} | ||
} | ||
isLocked() { | ||
return (this.status === ELockStatus.locked || | ||
this.status === ELockStatus.extending); | ||
return (this.status === ELockStatus.locked || this.status === ELockStatus.extended); | ||
} | ||
isReleased() { | ||
return this.status === ELockStatus.unlocked; | ||
} | ||
getId() { | ||
@@ -178,0 +213,0 @@ return this.lockId; |
@@ -17,12 +17,8 @@ "use strict"; | ||
(cb) => { | ||
if (!this.lockManager.isLocked()) { | ||
this.lockManager.acquireLock((err) => { | ||
if (!err) { | ||
this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lockManager.getId()}).`); | ||
} | ||
cb(err); | ||
}); | ||
} | ||
else | ||
cb(); | ||
this.lockManager.acquireOrExtend((err, status) => { | ||
if (status === lock_manager_1.ELockStatus.locked) { | ||
this.logger.info(`Workers are exclusively running from this instance (Lock ID ${this.lockManager.getId()}).`); | ||
} | ||
cb(err); | ||
}); | ||
}, | ||
@@ -62,3 +58,3 @@ (cb) => { | ||
this.logger = logger; | ||
this.lockManager = new lock_manager_1.LockManager(redisClient, keyLock, 10000, false, true); | ||
this.lockManager = new lock_manager_1.LockManager(redisClient, keyLock, 60000); | ||
this.ticker = new ticker_1.Ticker(this.onTick); | ||
@@ -65,0 +61,0 @@ this.workerPool = workerPool; |
{ | ||
"name": "redis-smq-common", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "RedisSMQ shared components that may be used by integrated applications and extensions.", | ||
@@ -5,0 +5,0 @@ "author": "Weyoss <weyoss@protonmail.com>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111821
90
2738