murlock 3.0.2 → 3.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,6 +35,7 @@ import { MurLockModule } from 'murlock';
35
35
  wait: 1000,
36
36
  maxAttempts: 3,
37
37
  logLevel: 'log',
38
+ ignoreUnlockFail: false,
38
39
  }),
39
40
  ],
40
41
  })
@@ -102,7 +103,8 @@ import { MurLockModule } from 'murlock';
102
103
  redisOptions: configService.get('REDIS_OPTIONS'),
103
104
  wait: configService.get('MURLOCK_WAIT'),
104
105
  maxAttempts: configService.get('MURLOCK_MAX_ATTEMPTS'),
105
- logLevel: configService.get('LOG_LEVEL')
106
+ logLevel: configService.get('LOG_LEVEL'),
107
+ ignoreUnlockFail: configService.get('LOG_LEVEL')
106
108
  }),
107
109
  inject: [ConfigService],
108
110
  }),
@@ -115,6 +117,22 @@ In the example above, the `ConfigModule` and `ConfigService` are used to provide
115
117
 
116
118
  For more details on usage and configuration, please refer to the API documentation below.
117
119
 
120
+ ### Ignoring Unlock Failures
121
+
122
+ In some scenarios, throwing an exception when a lock cannot be released can be undesirable. For example, you might prefer to log the failure and continue without interrupting the flow of your application. To enable this behavior, set the `ignoreUnlockFail` option to `true` in your configuration:
123
+
124
+ ```typescript
125
+ import { MurLockModule } from 'murlock';
126
+
127
+ MurLockModule.forRoot({
128
+ redisOptions: { url: 'redis://localhost:6379' },
129
+ wait: 1000,
130
+ maxAttempts: 3,
131
+ logLevel: 'log',
132
+ ignoreUnlockFail: true, // Unlock failures will be logged instead of throwing exceptions.
133
+ }),
134
+ ```
135
+
118
136
  ## Using `MurLockService` Directly
119
137
 
120
138
  While the `@MurLock()` decorator provides a convenient and declarative way to handle locking within your NestJS application, there may be cases where you need more control over the lock lifecycle. For such cases, `MurLockService` offers a programmatic way to manage locks.
@@ -183,6 +201,7 @@ try {
183
201
 
184
202
  - Always release locks in a `finally` block to avoid deadlocks.
185
203
  - Use meaningful lock keys that are unique to the resources they represent.
204
+ - Even with `ignoreUnlockFail` set to true, you should implement your error handling logic. This could include logging and retry mechanisms for critical operations.
186
205
  - Keep lock durations as short as possible to prevent system blockage.
187
206
 
188
207
  Directly using `MurLockService` gives you finer control over lock management but also increases the responsibility to ensure locks are correctly managed throughout your application's lifecycle.
@@ -206,6 +225,7 @@ A method decorator to indicate that a particular method should be locked.
206
225
  - **wait:** Time (in milliseconds) to wait before retrying if a lock isn't obtained.
207
226
  - **maxAttempts:** Maximum number of attempts to obtain a lock.
208
227
  - **logLevel:** Logging level. Can be one of 'none', 'error', 'warn', 'log', or 'debug'.
228
+ - **ignoreUnlockFail (optional):** When set to `true`, the library will not throw an exception if it fails to release a lock. Defaults to `false` to maintain backward compatibility.
209
229
 
210
230
  ### MurLockService
211
231
 
@@ -0,0 +1,6 @@
1
+ export declare class AsyncStorageManager<T> {
2
+ private asyncLocalStorage;
3
+ runWithNewContext<R>(fn: () => Promise<R>): Promise<R>;
4
+ set(key: string, value: T): void;
5
+ get(key: string): T | undefined;
6
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AsyncStorageManager = void 0;
10
+ const async_hooks_1 = require("async_hooks");
11
+ const exceptions_1 = require("../exceptions");
12
+ const common_1 = require("@nestjs/common");
13
+ let AsyncStorageManager = class AsyncStorageManager {
14
+ constructor() {
15
+ this.asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
16
+ }
17
+ runWithNewContext(fn) {
18
+ return this.asyncLocalStorage.run(new Map(), fn);
19
+ }
20
+ set(key, value) {
21
+ const store = this.asyncLocalStorage.getStore();
22
+ if (!store) {
23
+ throw new exceptions_1.AsyncStorageManagerException('No active store found');
24
+ }
25
+ store[key] = value;
26
+ }
27
+ get(key) {
28
+ const store = this.asyncLocalStorage.getStore();
29
+ if (!store) {
30
+ throw new exceptions_1.AsyncStorageManagerException('No active store found');
31
+ }
32
+ return store === null || store === void 0 ? void 0 : store[key];
33
+ }
34
+ };
35
+ exports.AsyncStorageManager = AsyncStorageManager;
36
+ exports.AsyncStorageManager = AsyncStorageManager = __decorate([
37
+ (0, common_1.Injectable)()
38
+ ], AsyncStorageManager);
39
+ //# sourceMappingURL=als-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"als-manager.js","sourceRoot":"","sources":["../../lib/als/als-manager.ts"],"names":[],"mappings":";;;;;;;;;AAAA,6CAAgD;AAChD,8CAA6D;AAC7D,2CAA4C;AAGrC,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAAzB;QACG,sBAAiB,GAAG,IAAI,+BAAiB,EAAkB,CAAC;IAqBtE,CAAC;IAnBC,iBAAiB,CAAI,EAAoB;QACvC,OAAO,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,IAAI,GAAG,EAAa,EAAE,EAAE,CAAC,CAAC;IAC9D,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,CAAC;QAChD,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,yCAA4B,CAAC,uBAAuB,CAAC,CAAC;SACjE;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,GAAG,CAAC,GAAW;QACb,MAAM,KAAK,GAAG,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,CAAC;QAChD,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,yCAA4B,CAAC,uBAAuB,CAAC,CAAC;SACjE;QACD,OAAO,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAG,GAAG,CAAC,CAAC;IACtB,CAAC;CACF,CAAA;AAtBY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,mBAAU,GAAE;GACA,mBAAmB,CAsB/B"}
@@ -0,0 +1,3 @@
1
+ export declare class AsyncStorageManagerException extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncStorageManagerException = void 0;
4
+ class AsyncStorageManagerException extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "AsyncStorageManagerException";
8
+ }
9
+ }
10
+ exports.AsyncStorageManagerException = AsyncStorageManagerException;
11
+ //# sourceMappingURL=async-storage-manager.exception.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-storage-manager.exception.js","sourceRoot":"","sources":["../../lib/exceptions/async-storage-manager.exception.ts"],"names":[],"mappings":";;;AAAA,MAAa,4BAA6B,SAAQ,KAAK;IACrD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,8BAA8B,CAAC;IAC7C,CAAC;CACF;AALD,oEAKC"}
@@ -1,2 +1,3 @@
1
1
  export * from './murlock.exception';
2
2
  export * from './murlock-redis.exception';
3
+ export * from './async-storage-manager.exception';
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./murlock.exception"), exports);
18
18
  __exportStar(require("./murlock-redis.exception"), exports);
19
+ __exportStar(require("./async-storage-manager.exception"), exports);
19
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/exceptions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,4DAA0C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/exceptions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,4DAA0C;AAC1C,oEAAkD"}
@@ -0,0 +1,8 @@
1
+ import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
+ import { Observable } from 'rxjs';
3
+ import { AsyncStorageManager } from '../als/als-manager';
4
+ export declare class AsyncStorageInterceptor implements NestInterceptor {
5
+ private readonly asyncStorageManager;
6
+ constructor(asyncStorageManager: AsyncStorageManager<string>);
7
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
8
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.AsyncStorageInterceptor = void 0;
22
+ const common_1 = require("@nestjs/common");
23
+ const rxjs_1 = require("rxjs");
24
+ const operators_1 = require("rxjs/operators");
25
+ const als_manager_1 = require("../als/als-manager");
26
+ let AsyncStorageInterceptor = class AsyncStorageInterceptor {
27
+ constructor(asyncStorageManager) {
28
+ this.asyncStorageManager = asyncStorageManager;
29
+ }
30
+ intercept(context, next) {
31
+ return new rxjs_1.Observable((subscriber) => {
32
+ this.asyncStorageManager.runWithNewContext(() => __awaiter(this, void 0, void 0, function* () {
33
+ next.handle().pipe((0, operators_1.tap)({
34
+ next: (value) => subscriber.next(value),
35
+ error: (err) => subscriber.error(err),
36
+ complete: () => subscriber.complete(),
37
+ })).subscribe();
38
+ }));
39
+ });
40
+ }
41
+ };
42
+ exports.AsyncStorageInterceptor = AsyncStorageInterceptor;
43
+ exports.AsyncStorageInterceptor = AsyncStorageInterceptor = __decorate([
44
+ (0, common_1.Injectable)(),
45
+ __metadata("design:paramtypes", [als_manager_1.AsyncStorageManager])
46
+ ], AsyncStorageInterceptor);
47
+ //# sourceMappingURL=async-local-storage.interceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-local-storage.interceptor.js","sourceRoot":"","sources":["../../lib/interceptors/async-local-storage.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4F;AAC5F,+BAAkC;AAClC,8CAAqC;AACrC,oDAAyD;AAGlD,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;IAClC,YAA6B,mBAAgD;QAAhD,wBAAmB,GAAnB,mBAAmB,CAA6B;IAAI,CAAC;IAElF,SAAS,CAAC,OAAyB,EAAE,IAAiB;QACpD,OAAO,IAAI,iBAAU,CAAC,CAAC,UAAU,EAAE,EAAE;YACnC,IAAI,CAAC,mBAAmB,CAAC,iBAAiB,CAAC,GAAS,EAAE;gBACpD,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAChB,IAAA,eAAG,EAAC;oBACF,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;oBACvC,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC;oBACrC,QAAQ,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE;iBACtC,CAAC,CACH,CAAC,SAAS,EAAE,CAAC;YAChB,CAAC,CAAA,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAhBY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,mBAAU,GAAE;qCAEuC,iCAAmB;GAD1D,uBAAuB,CAgBnC"}
@@ -0,0 +1 @@
1
+ export * from './async-local-storage.interceptor';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./async-local-storage.interceptor"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/interceptors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oEAAkD"}
@@ -4,6 +4,7 @@ export interface MurLockModuleOptions {
4
4
  wait: number;
5
5
  maxAttempts: number;
6
6
  logLevel: 'none' | 'error' | 'warn' | 'log' | 'debug';
7
+ ignoreUnlockFail?: boolean;
7
8
  }
8
9
  export interface MurLockModuleAsyncOptions {
9
10
  imports?: any[];
@@ -1,5 +1,4 @@
1
1
  import { DynamicModule } from '@nestjs/common';
2
- import 'reflect-metadata';
3
2
  import { MurLockModuleAsyncOptions, MurLockModuleOptions } from './interfaces';
4
3
  export declare class MurLockModule {
5
4
  static forRoot(options: MurLockModuleOptions): DynamicModule;
@@ -10,7 +10,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.MurLockModule = void 0;
11
11
  const common_1 = require("@nestjs/common");
12
12
  const murlock_service_1 = require("./murlock.service");
13
- require("reflect-metadata");
13
+ const als_manager_1 = require("./als/als-manager");
14
+ const core_1 = require("@nestjs/core");
15
+ const interceptors_1 = require("./interceptors");
14
16
  let MurLockModule = MurLockModule_1 = class MurLockModule {
15
17
  static forRoot(options) {
16
18
  return {
@@ -20,7 +22,12 @@ let MurLockModule = MurLockModule_1 = class MurLockModule {
20
22
  provide: 'MURLOCK_OPTIONS',
21
23
  useValue: options,
22
24
  },
25
+ als_manager_1.AsyncStorageManager,
23
26
  murlock_service_1.MurLockService,
27
+ {
28
+ provide: core_1.APP_INTERCEPTOR,
29
+ useClass: interceptors_1.AsyncStorageInterceptor,
30
+ },
24
31
  ],
25
32
  exports: [murlock_service_1.MurLockService],
26
33
  };
@@ -32,6 +39,11 @@ let MurLockModule = MurLockModule_1 = class MurLockModule {
32
39
  providers: [
33
40
  this.createAsyncOptionsProvider(options),
34
41
  murlock_service_1.MurLockService,
42
+ als_manager_1.AsyncStorageManager,
43
+ {
44
+ provide: core_1.APP_INTERCEPTOR,
45
+ useClass: interceptors_1.AsyncStorageInterceptor,
46
+ },
35
47
  ],
36
48
  exports: [murlock_service_1.MurLockService],
37
49
  };
@@ -1 +1 @@
1
- {"version":3,"file":"murlock.module.js","sourceRoot":"","sources":["../lib/murlock.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,uDAAmD;AACnD,4BAA0B;AAKnB,IAAM,aAAa,qBAAnB,MAAM,aAAa;IACxB,MAAM,CAAC,OAAO,CAAC,OAA6B;QAC1C,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,iBAAiB;oBAC1B,QAAQ,EAAE,OAAO;iBAClB;gBACD,gCAAc;aACf;YACD,OAAO,EAAE,CAAC,gCAAc,CAAC;SAC1B,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAAkC;QACpD,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS,EAAE;gBACT,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC;gBACxC,gCAAc;aACf;YACD,OAAO,EAAE,CAAC,gCAAc,CAAC;SAC1B,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,0BAA0B,CAAC,OAAkC;QAC1E,OAAO;YACL,OAAO,EAAE,iBAAiB;YAC1B,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;SAC7B,CAAC;IACJ,CAAC;CACF,CAAA;AAlCY,sCAAa;wBAAb,aAAa;IAFzB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,aAAa,CAkCzB"}
1
+ {"version":3,"file":"murlock.module.js","sourceRoot":"","sources":["../lib/murlock.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,uDAAmD;AAEnD,mDAAwD;AACxD,uCAA+C;AAC/C,iDAAyD;AAIlD,IAAM,aAAa,qBAAnB,MAAM,aAAa;IACxB,MAAM,CAAC,OAAO,CAAC,OAA6B;QAC1C,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,iBAAiB;oBAC1B,QAAQ,EAAE,OAAO;iBAClB;gBACD,iCAAmB;gBACnB,gCAAc;gBACd;oBACE,OAAO,EAAE,sBAAe;oBACxB,QAAQ,EAAE,sCAAuB;iBAClC;aACF;YACD,OAAO,EAAE,CAAC,gCAAc,CAAC;SAC1B,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAAkC;QACpD,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS,EAAE;gBACT,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC;gBACxC,gCAAc;gBACd,iCAAmB;gBACnB;oBACE,OAAO,EAAE,sBAAe;oBACxB,QAAQ,EAAE,sCAAuB;iBAClC;aACF;YACD,OAAO,EAAE,CAAC,gCAAc,CAAC;SAC1B,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,0BAA0B,CAAC,OAAkC;QAC1E,OAAO;YACL,OAAO,EAAE,iBAAiB;YAC1B,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;SAC7B,CAAC;IACJ,CAAC;CACF,CAAA;AA5CY,sCAAa;wBAAb,aAAa;IAFzB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,aAAa,CA4CzB"}
@@ -1,13 +1,14 @@
1
1
  import { OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
2
2
  import { MurLockModuleOptions } from './interfaces';
3
+ import { AsyncStorageManager } from './als/als-manager';
3
4
  export declare class MurLockService implements OnModuleInit, OnApplicationShutdown {
4
5
  protected readonly options: MurLockModuleOptions;
6
+ private readonly asyncStorageManager;
5
7
  private readonly logger;
6
8
  private redisClient;
7
9
  private readonly lockScript;
8
10
  private readonly unlockScript;
9
- private clientId;
10
- constructor(options: MurLockModuleOptions);
11
+ constructor(options: MurLockModuleOptions, asyncStorageManager: AsyncStorageManager<string>);
11
12
  onModuleInit(): Promise<void>;
12
13
  onApplicationShutdown(signal?: string): Promise<void>;
13
14
  private generateUuid;
@@ -28,9 +28,11 @@ const redis_1 = require("redis");
28
28
  const fs_1 = require("fs");
29
29
  const path_1 = require("path");
30
30
  const exceptions_1 = require("./exceptions");
31
+ const als_manager_1 = require("./als/als-manager");
31
32
  let MurLockService = MurLockService_1 = class MurLockService {
32
- constructor(options) {
33
+ constructor(options, asyncStorageManager) {
33
34
  this.options = options;
35
+ this.asyncStorageManager = asyncStorageManager;
34
36
  this.logger = new common_1.Logger(MurLockService_1.name);
35
37
  this.lockScript = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, './lua/lock.lua')).toString();
36
38
  this.unlockScript = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, './lua/unlock.lua')).toString();
@@ -62,10 +64,12 @@ let MurLockService = MurLockService_1 = class MurLockService {
62
64
  });
63
65
  }
64
66
  getClientId() {
65
- if (this.clientId) {
66
- return this.clientId;
67
+ let clientId = this.asyncStorageManager.get('clientId');
68
+ if (!clientId) {
69
+ clientId = this.generateUuid();
70
+ this.asyncStorageManager.set('clientId', clientId);
67
71
  }
68
- return this.clientId = this.generateUuid();
72
+ return clientId;
69
73
  }
70
74
  sleep(ms) {
71
75
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -121,7 +125,12 @@ let MurLockService = MurLockService_1 = class MurLockService {
121
125
  clientId,
122
126
  ]);
123
127
  if (result === 0) {
124
- throw new exceptions_1.MurLockException(`Failed to release lock for key ${lockKey}`);
128
+ if (!this.options.ignoreUnlockFail) {
129
+ throw new exceptions_1.MurLockException(`Failed to release lock for key ${lockKey}`);
130
+ }
131
+ else {
132
+ this.log('warn', `Failed to release lock for key ${lockKey}, but throwing errors is disabled.`);
133
+ }
125
134
  }
126
135
  });
127
136
  }
@@ -130,6 +139,6 @@ exports.MurLockService = MurLockService;
130
139
  exports.MurLockService = MurLockService = MurLockService_1 = __decorate([
131
140
  (0, common_1.Injectable)(),
132
141
  __param(0, (0, common_1.Inject)('MURLOCK_OPTIONS')),
133
- __metadata("design:paramtypes", [Object])
142
+ __metadata("design:paramtypes", [Object, als_manager_1.AsyncStorageManager])
134
143
  ], MurLockService);
135
144
  //# sourceMappingURL=murlock.service.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"murlock.service.js","sourceRoot":"","sources":["../lib/murlock.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAiG;AACjG,iCAAsD;AACtD,2BAAkC;AAClC,+BAA4B;AAE5B,6CAAuE;AAMhE,IAAM,cAAc,sBAApB,MAAM,cAAc;IAOzB,YAC6B,OAAgD;QAA7B,YAAO,GAAP,OAAO,CAAsB;QAP5D,WAAM,GAAG,IAAI,eAAM,CAAC,gBAAc,CAAC,IAAI,CAAC,CAAC;QAEzC,eAAU,GAAG,IAAA,iBAAY,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;QACxE,iBAAY,GAAG,IAAA,iBAAY,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,kBAAkB,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;IAKzF,CAAC;IAEC,YAAY;;YAChB,IAAI,CAAC,WAAW,GAAG,IAAA,oBAAY,EAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAoB,CAAC;YAE9E,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;gBACnC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,4BAA4B,EAAE,GAAG,CAAC,CAAC;gBACrD,MAAM,IAAI,kCAAqB,CAAC,+BAA+B,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;YAChF,CAAC,CAAC,CAAC;YAEH,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC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