murlock 2.0.0 → 2.0.1
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 +78 -1
- package/dist/murlock.module.js +6 -1
- package/dist/murlock.module.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ export class AppService {
|
|
|
70
70
|
```
|
|
71
71
|
|
|
72
72
|
If there are multiple wrapped parameter, you can call it by {index of parameter}.{parameter name} as it shown
|
|
73
|
+
|
|
73
74
|
```typescript
|
|
74
75
|
import { MurLock } from 'murlock';
|
|
75
76
|
|
|
@@ -114,6 +115,82 @@ In the example above, the `ConfigModule` and `ConfigService` are used to provide
|
|
|
114
115
|
|
|
115
116
|
For more details on usage and configuration, please refer to the API documentation below.
|
|
116
117
|
|
|
118
|
+
## Using `MurLockService` Directly
|
|
119
|
+
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
#### Injecting `MurLockService`
|
|
123
|
+
|
|
124
|
+
First, inject `MurLockService` into your service:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { Injectable } from '@nestjs/common';
|
|
128
|
+
import { MurLockService } from 'murlock';
|
|
129
|
+
|
|
130
|
+
@Injectable()
|
|
131
|
+
export class YourService {
|
|
132
|
+
constructor(private murLockService: MurLockService) {}
|
|
133
|
+
|
|
134
|
+
// Your methods where you want to use the lock
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### Acquiring a Lock
|
|
139
|
+
|
|
140
|
+
You can acquire a lock by calling the `acquireLock` method with a unique `lockKey` and the desired `lockTime`:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
async performTaskWithLock() {
|
|
144
|
+
const lockKey = 'unique_lock_key';
|
|
145
|
+
const lockTime = 3000; // Duration for which the lock should be held in milliseconds
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
await this.murLockService.acquireLock(lockKey, lockTime);
|
|
149
|
+
// Proceed with the operation that requires the lock
|
|
150
|
+
} catch (error) {
|
|
151
|
+
// Handle the error if the lock could not be acquired
|
|
152
|
+
throw error;
|
|
153
|
+
} finally {
|
|
154
|
+
// Make sure to release the lock
|
|
155
|
+
await this.murLockService.releaseLock(lockKey);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Releasing a Lock
|
|
161
|
+
|
|
162
|
+
To release a lock, use the `releaseLock` method:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
await this.murLockService.releaseLock(lockKey);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Handling Errors
|
|
169
|
+
|
|
170
|
+
Make sure to handle exceptions gracefully, especially when you are unable to acquire a lock:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
try {
|
|
174
|
+
// Lock acquisition attempts
|
|
175
|
+
} catch (error) {
|
|
176
|
+
// Error handling logic
|
|
177
|
+
} finally {
|
|
178
|
+
// Always release the lock in a finally block
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### Best Practices and Considerations
|
|
183
|
+
|
|
184
|
+
- Always release locks in a `finally` block to avoid deadlocks.
|
|
185
|
+
- Use meaningful lock keys that are unique to the resources they represent.
|
|
186
|
+
- Keep lock durations as short as possible to prevent system blockage.
|
|
187
|
+
|
|
188
|
+
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.
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
This refined section is suitable for developers looking for documentation on using `MurLockService` directly in their projects and adheres to the typical conventions found in README files for open-source projects.
|
|
193
|
+
|
|
117
194
|
## API Documentation
|
|
118
195
|
|
|
119
196
|
### MurLock(releaseTime: number, ...keyParams: string[])
|
|
@@ -155,7 +232,7 @@ This project is licensed under the [MIT License](https://github.com/felanios/mur
|
|
|
155
232
|
|
|
156
233
|
## Contact
|
|
157
234
|
|
|
158
|
-
If you have any questions or feedback, feel free to contact me at ozmen.eyupfurkan@gmail.com
|
|
235
|
+
If you have any questions or feedback, feel free to contact me at <ozmen.eyupfurkan@gmail.com>.
|
|
159
236
|
|
|
160
237
|
---
|
|
161
238
|
|
package/dist/murlock.module.js
CHANGED
|
@@ -66,7 +66,12 @@ exports.MurLockModule = MurLockModule;
|
|
|
66
66
|
exports.MurLockModule = MurLockModule = MurLockModule_1 = __decorate([
|
|
67
67
|
(0, common_1.Global)(),
|
|
68
68
|
(0, common_1.Module)({
|
|
69
|
-
imports: [
|
|
69
|
+
imports: [
|
|
70
|
+
nestjs_cls_1.ClsModule.forRoot({
|
|
71
|
+
global: true,
|
|
72
|
+
middleware: { mount: true },
|
|
73
|
+
}),
|
|
74
|
+
],
|
|
70
75
|
providers: [murlock_service_1.MurLockService],
|
|
71
76
|
exports: [murlock_service_1.MurLockService],
|
|
72
77
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"murlock.module.js","sourceRoot":"","sources":["../lib/murlock.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,uDAAmD;AACnD,4BAA0B;AAE1B,2CAA2D;AAC3D,2CAAuC;
|
|
1
|
+
{"version":3,"file":"murlock.module.js","sourceRoot":"","sources":["../lib/murlock.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,uDAAmD;AACnD,4BAA0B;AAE1B,2CAA2D;AAC3D,2CAAuC;AAahC,IAAM,aAAa,qBAAnB,MAAM,aAAa;IACxB,MAAM,CAAC,YAAY,CAAC,OAA6B;QAC/C,OAAO;YACL,MAAM,EAAE,eAAa;YACrB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,iBAAiB;oBAC1B,QAAQ,EAA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