@anchan828/nest-redlock 0.1.12 → 0.1.14

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Redlock } from "./redlock.decorator";
2
2
  export { FakeRedlockService } from "./redlock.fake-service";
3
- export { RedlockKeyFunction, RedlockModuleOptions } from "./redlock.interface";
3
+ export { LockedKeysHookArgs, PreLockedKeysHookArgs, RedlockKeyFunction, RedlockModuleOptions, UnlockedKeysHookArgs, } from "./redlock.interface";
4
4
  export { RedlockModule } from "./redlock.module";
5
5
  export { RedlockService } from "./redlock.service";
@@ -15,12 +15,28 @@ function Redlock(key, duration, settings = {}) {
15
15
  const descriptorThis = this;
16
16
  const redlockService = descriptorThis[serviceSymbol];
17
17
  const keys = getKeys(key, descriptorThis, args);
18
- return await redlockService.using(keys, duration || redlockService.options?.duration || redlock_constants_1.DEFAULT_DURATION, settings, async (signal) => {
18
+ const useDuration = duration || redlockService.options?.duration || redlock_constants_1.DEFAULT_DURATION;
19
+ await redlockService.options.decoratorHooks?.preLockKeys?.({ keys, duration: useDuration });
20
+ const startTime = Date.now();
21
+ return await redlockService
22
+ .using(keys, useDuration, settings, async (signal) => {
23
+ await redlockService.options.decoratorHooks?.lockedKeys?.({
24
+ keys,
25
+ duration: useDuration,
26
+ elapsedTime: Date.now() - startTime,
27
+ });
19
28
  const result = await originalMethod.apply(descriptorThis, args);
20
29
  if (signal.aborted) {
21
30
  throw signal.error;
22
31
  }
23
32
  return result;
33
+ })
34
+ .finally(async () => {
35
+ await redlockService.options.decoratorHooks?.unlockedKeys?.({
36
+ keys,
37
+ duration: useDuration,
38
+ elapsedTime: Date.now() - startTime,
39
+ });
24
40
  });
25
41
  };
26
42
  return descriptor;
@@ -1,5 +1,19 @@
1
1
  import Redis, { Cluster } from "ioredis";
2
2
  import { Settings } from "redlock";
3
+ export type PreLockedKeysHookArgs = {
4
+ keys: string[];
5
+ duration: number;
6
+ };
7
+ export type LockedKeysHookArgs = {
8
+ keys: string[];
9
+ duration: number;
10
+ elapsedTime: number;
11
+ };
12
+ export type UnlockedKeysHookArgs = {
13
+ keys: string[];
14
+ duration: number;
15
+ elapsedTime: number;
16
+ };
3
17
  export type RedlockModuleOptions = {
4
18
  clients: Iterable<Redis | Cluster>;
5
19
  settings?: Partial<Settings>;
@@ -8,6 +22,23 @@ export type RedlockModuleOptions = {
8
22
  readonly extendScript?: string | ((script: string) => string);
9
23
  readonly releaseScript?: string | ((script: string) => string);
10
24
  };
25
+ /**
26
+ * Hooks called when using @Redlock decorator.
27
+ */
28
+ decoratorHooks?: {
29
+ /**
30
+ * Called before redlock.using
31
+ */
32
+ readonly preLockKeys?: (args: PreLockedKeysHookArgs) => void | Promise<void>;
33
+ /**
34
+ * Called first when the redlock.using callback is invoked.
35
+ */
36
+ readonly lockedKeys?: (args: LockedKeysHookArgs) => void | Promise<void>;
37
+ /**
38
+ * Called after when the redlock.using callback is finished.
39
+ */
40
+ readonly unlockedKeys?: (args: UnlockedKeysHookArgs) => void | Promise<void>;
41
+ };
11
42
  /**
12
43
  * Default duratiuon to use with Redlock decorator
13
44
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchan828/nest-redlock",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "This is a [Nest](https://github.com/nestjs/nest) implementation of the redlock algorithm for distributed redis locks.",
5
5
  "homepage": "https://github.com/anchan828/nest-redlock#readme",
6
6
  "bugs": {
@@ -26,8 +26,8 @@
26
26
  "redlock": "^5.0.0-beta.2"
27
27
  },
28
28
  "devDependencies": {
29
- "@commitlint/cli": "^17.2.0",
30
- "@commitlint/config-conventional": "^17.2.0",
29
+ "@commitlint/cli": "^17.3.0",
30
+ "@commitlint/config-conventional": "^17.3.0",
31
31
  "@nestjs/common": "^9.2.0",
32
32
  "@nestjs/core": "^9.2.0",
33
33
  "@nestjs/platform-express": "^9.2.0",
@@ -35,16 +35,16 @@
35
35
  "@types/jest": "^29.2.3",
36
36
  "@types/node": "^18.11.9",
37
37
  "@types/supertest": "^2.0.12",
38
- "@typescript-eslint/eslint-plugin": "^5.43.0",
39
- "@typescript-eslint/parser": "^5.43.0",
40
- "eslint": "^8.27.0",
38
+ "@typescript-eslint/eslint-plugin": "^5.44.0",
39
+ "@typescript-eslint/parser": "^5.44.0",
40
+ "eslint": "^8.28.0",
41
41
  "eslint-config-prettier": "^8.5.0",
42
42
  "eslint-plugin-prettier": "^4.2.1",
43
43
  "husky": "^8.0.2",
44
44
  "ioredis": "^5.2.4",
45
45
  "jest": "^29.3.1",
46
- "lint-staged": "^13.0.3",
47
- "prettier": "^2.7.1",
46
+ "lint-staged": "^13.0.4",
47
+ "prettier": "^2.8.0",
48
48
  "reflect-metadata": "^0.1.13",
49
49
  "rxjs": "^7.5.7",
50
50
  "supertest": "^6.3.1",