@clipboard-health/mongo-jobs 0.1.3 → 0.1.4
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 +86 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,6 +21,92 @@
|
|
|
21
21
|
npm install @clipboard-health/mongo-jobs
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Define a job handler
|
|
27
|
+
|
|
28
|
+
Job handlers implement the `HandlerInterface` and define how your jobs are processed:
|
|
29
|
+
|
|
30
|
+
<embedex source="packages/mongo-jobs/examples/quickstart/welcomeEmailJob.ts">
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import type { HandlerInterface } from "@clipboard-health/mongo-jobs";
|
|
34
|
+
|
|
35
|
+
export interface WelcomeEmailData {
|
|
36
|
+
userId: string;
|
|
37
|
+
email: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class WelcomeEmailJob implements HandlerInterface<WelcomeEmailData> {
|
|
41
|
+
public name = "WelcomeEmailJob";
|
|
42
|
+
public maxAttempts = 3;
|
|
43
|
+
|
|
44
|
+
async perform({ userId, email }: WelcomeEmailData) {
|
|
45
|
+
await this.sendEmail(email, `Welcome, user ${userId}!`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private async sendEmail(_to: string, _message: string) {
|
|
49
|
+
// Email sending logic
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
</embedex>
|
|
55
|
+
|
|
56
|
+
### 2. Create a service and register handlers
|
|
57
|
+
|
|
58
|
+
Create a `BackgroundJobsService` instance and register your handlers to groups:
|
|
59
|
+
|
|
60
|
+
<embedex source="packages/mongo-jobs/examples/quickstart/jobsRegistry.ts">
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { BackgroundJobsService } from "@clipboard-health/mongo-jobs";
|
|
64
|
+
|
|
65
|
+
import { WelcomeEmailJob } from "./welcomeEmailJob";
|
|
66
|
+
|
|
67
|
+
const backgroundJobs = new BackgroundJobsService();
|
|
68
|
+
|
|
69
|
+
backgroundJobs.register(WelcomeEmailJob, "emails");
|
|
70
|
+
|
|
71
|
+
export { backgroundJobs };
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
</embedex>
|
|
75
|
+
|
|
76
|
+
### 3. Enqueue jobs
|
|
77
|
+
|
|
78
|
+
Add jobs to the queue to be processed:
|
|
79
|
+
|
|
80
|
+
<embedex source="packages/mongo-jobs/examples/quickstart/enqueueJob.ts">
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { backgroundJobs } from "./jobsRegistry";
|
|
84
|
+
import { WelcomeEmailJob } from "./welcomeEmailJob";
|
|
85
|
+
|
|
86
|
+
await backgroundJobs.enqueue(WelcomeEmailJob, {
|
|
87
|
+
userId: "123",
|
|
88
|
+
email: "user@example.com",
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
</embedex>
|
|
93
|
+
|
|
94
|
+
### 4. Start the worker
|
|
95
|
+
|
|
96
|
+
Start processing jobs from the queue:
|
|
97
|
+
|
|
98
|
+
<embedex source="packages/mongo-jobs/examples/quickstart/startWorker.ts">
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { backgroundJobs } from "./jobsRegistry";
|
|
102
|
+
|
|
103
|
+
await backgroundJobs.start(["emails"], {
|
|
104
|
+
maxConcurrency: 10,
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
</embedex>
|
|
109
|
+
|
|
24
110
|
## License
|
|
25
111
|
|
|
26
112
|
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/mongo-jobs",
|
|
3
3
|
"description": "MongoDB-powered background jobs.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"cron-parser": "5.4.0",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"tslib": "2.8.1"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@clipboard-health/util-ts": "3.17.
|
|
12
|
+
"@clipboard-health/util-ts": "3.17.4",
|
|
13
13
|
"@opentelemetry/api": "1.9.0",
|
|
14
14
|
"mongodb": "6.18.0",
|
|
15
15
|
"mongoose": "8.18.0"
|