@monque/tsed 0.1.0
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/LICENSE +15 -0
- package/README.md +222 -0
- package/dist/CHANGELOG.md +19 -0
- package/dist/LICENSE +15 -0
- package/dist/README.md +222 -0
- package/dist/index.cjs +712 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +582 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +582 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +691 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +98 -0
- package/src/config/config.ts +60 -0
- package/src/config/index.ts +2 -0
- package/src/config/types.ts +114 -0
- package/src/constants/constants.ts +12 -0
- package/src/constants/index.ts +2 -0
- package/src/constants/types.ts +21 -0
- package/src/decorators/cron.ts +58 -0
- package/src/decorators/index.ts +10 -0
- package/src/decorators/types.ts +131 -0
- package/src/decorators/worker-controller.ts +55 -0
- package/src/decorators/worker.ts +66 -0
- package/src/index.ts +20 -0
- package/src/monque-module.ts +199 -0
- package/src/services/index.ts +1 -0
- package/src/services/monque-service.ts +267 -0
- package/src/utils/build-job-name.ts +17 -0
- package/src/utils/collect-worker-metadata.ts +95 -0
- package/src/utils/get-worker-token.ts +27 -0
- package/src/utils/guards.ts +62 -0
- package/src/utils/index.ts +13 -0
- package/src/utils/resolve-database.ts +119 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Maurice de Bruyn
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="../../assets/logo.svg" width="180" alt="Monque logo" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@monque/tsed</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/@monque/tsed">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/%40monque%2Ftsed?style=for-the-badge&label=%40monque%2Ftsed" alt="@monque/tsed version" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/ueberBrot/monque/actions/workflows/ci.yml">
|
|
12
|
+
<img src="https://img.shields.io/github/actions/workflow/status/ueberBrot/monque/ci.yml?branch=main&style=for-the-badge&logo=github" alt="CI Status" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://codecov.io/gh/ueberBrot/monque">
|
|
15
|
+
<img src="https://img.shields.io/codecov/c/github/ueberBrot/monque?style=for-the-badge&logo=codecov&logoColor=white" alt="Codecov" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://opensource.org/licenses/ISC">
|
|
18
|
+
<img src="https://img.shields.io/badge/License-ISC-blue.svg?style=for-the-badge" alt="License: ISC" />
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://bun.sh">
|
|
21
|
+
<img src="https://img.shields.io/badge/Built%20with-Bun-fbf0df?style=for-the-badge&logo=bun&logoColor=black" alt="Built with Bun" />
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
A **Ts.ED** integration for **Monque**, a robust, type-safe MongoDB job queue for TypeScript.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- 🎯 **Decorator-based API**: `@WorkerController`, `@Worker`, and `@Cron` for declarative job handling.
|
|
30
|
+
- 💉 **Dependency Injection**: Full support for Ts.ED DI (inject Services/Providers into your workers).
|
|
31
|
+
- 🔒 **Job Isolation**: Each job execution runs in a dedicated `DIContext` with Request Scope support.
|
|
32
|
+
- 🔍 **Type Safety**: Leverage TypeScript generics for fully typed job payloads.
|
|
33
|
+
- ⚡ **Full Monque Power**: Complete access to all `@monque/core` features (backoff, heartbeats, atomic locking).
|
|
34
|
+
- 🛠️ **Seamless Integration**: Native lifecycle hooks support (`$onInit`, `$onDestroy`) for graceful scheduler management.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bun add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or using npm/yarn/pnpm:
|
|
43
|
+
```bash
|
|
44
|
+
npm install @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
45
|
+
yarn add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
46
|
+
pnpm add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
Import `MonqueModule` in your `Server.ts` and configure the connection:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Configuration } from "@tsed/di";
|
|
55
|
+
import { MonqueModule } from "@monque/tsed";
|
|
56
|
+
import "@tsed/mongoose";
|
|
57
|
+
import "@tsed/platform-express"; // or @tsed/platform-koa
|
|
58
|
+
|
|
59
|
+
@Configuration({
|
|
60
|
+
imports: [
|
|
61
|
+
MonqueModule
|
|
62
|
+
],
|
|
63
|
+
mongoose: [
|
|
64
|
+
{
|
|
65
|
+
id: "default",
|
|
66
|
+
url: "mongodb://localhost:27017/my-app",
|
|
67
|
+
connectionOptions: {}
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
monque: {
|
|
71
|
+
enabled: true,
|
|
72
|
+
// Option 1: Reuse existing Mongoose connection (Recommended)
|
|
73
|
+
mongooseConnectionId: "default",
|
|
74
|
+
|
|
75
|
+
// Option 2: Provide existing Db instance via factory
|
|
76
|
+
// dbFactory: async () => {
|
|
77
|
+
// const client = new MongoClient(process.env.MONGO_URL);
|
|
78
|
+
// await client.connect();
|
|
79
|
+
// return client.db("my-app");
|
|
80
|
+
// },
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
export class Server {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Usage
|
|
87
|
+
|
|
88
|
+
### 1. Define a Worker Controller
|
|
89
|
+
|
|
90
|
+
Create a class decorated with `@WorkerController`. Methods decorated with `@Worker` will process jobs.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { WorkerController, Worker } from "@monque/tsed";
|
|
94
|
+
import { Job } from "@monque/core";
|
|
95
|
+
import { EmailService } from "./services/EmailService";
|
|
96
|
+
|
|
97
|
+
interface EmailPayload {
|
|
98
|
+
to: string;
|
|
99
|
+
subject: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@WorkerController("email") // Namespace prefix: "email."
|
|
103
|
+
export class EmailWorkers {
|
|
104
|
+
constructor(private emailService: EmailService) {}
|
|
105
|
+
|
|
106
|
+
@Worker("send", { concurrency: 5 }) // Job name: "email.send"
|
|
107
|
+
async sendEmail(job: Job<EmailPayload>) {
|
|
108
|
+
await this.emailService.send(
|
|
109
|
+
job.data.to,
|
|
110
|
+
job.data.subject
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 2. Schedule Jobs (Cron)
|
|
117
|
+
|
|
118
|
+
Use the `@Cron` decorator to schedule recurring tasks.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { WorkerController, Cron } from "@monque/tsed";
|
|
122
|
+
|
|
123
|
+
@WorkerController()
|
|
124
|
+
export class ReportWorkers {
|
|
125
|
+
|
|
126
|
+
@Cron("0 0 * * *", { name: "daily-report" })
|
|
127
|
+
async generateDailyReport() {
|
|
128
|
+
console.log("Generating report...");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 3. Enqueue Jobs
|
|
134
|
+
|
|
135
|
+
Inject `MonqueService` to dispatch jobs from anywhere in your app.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { Service, Inject } from "@tsed/di";
|
|
139
|
+
import { MonqueService } from "@monque/tsed";
|
|
140
|
+
|
|
141
|
+
@Service()
|
|
142
|
+
export class AuthService {
|
|
143
|
+
@Inject()
|
|
144
|
+
private monque: MonqueService;
|
|
145
|
+
|
|
146
|
+
async registerUser(user: User) {
|
|
147
|
+
// ... save user ...
|
|
148
|
+
|
|
149
|
+
// Dispatch background job
|
|
150
|
+
await this.monque.enqueue("email.send", {
|
|
151
|
+
to: user.email,
|
|
152
|
+
subject: "Welcome!"
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## API Reference
|
|
159
|
+
|
|
160
|
+
### Decorators
|
|
161
|
+
|
|
162
|
+
#### `@WorkerController(namespace?: string)`
|
|
163
|
+
Class decorator to register a worker controller.
|
|
164
|
+
- `namespace`: Optional prefix for all worker names in this class.
|
|
165
|
+
|
|
166
|
+
#### `@Worker(name: string, options?: WorkerOptions)`
|
|
167
|
+
Method decorator to register a job handler.
|
|
168
|
+
- `name`: Job name (combined with namespace).
|
|
169
|
+
- `options`: Supports all `@monque/core` [WorkerOptions](../../packages/core/README.md#new-monque-db-options) (concurrency, lockTimeout, etc.).
|
|
170
|
+
|
|
171
|
+
#### `@Cron(pattern: string, options?: ScheduleOptions)`
|
|
172
|
+
Method decorator to register a scheduled job.
|
|
173
|
+
- `pattern`: Cron expression (e.g., `* * * * *`).
|
|
174
|
+
- `options`: Supports all `@monque/core` [ScheduleOptions](../../packages/core/README.md#methods) (tz, job name override, etc.).
|
|
175
|
+
|
|
176
|
+
### Services
|
|
177
|
+
|
|
178
|
+
#### `MonqueService`
|
|
179
|
+
Injectable wrapper for the main `Monque` instance. It exposes the full Monque public API through dependency injection.
|
|
180
|
+
|
|
181
|
+
**Job Scheduling:**
|
|
182
|
+
- `enqueue(name, data, opts)` - Enqueue a job
|
|
183
|
+
- `schedule(cron, name, data, opts)` - Schedule a recurring job
|
|
184
|
+
- `now(name, data)` - Enqueue for immediate processing
|
|
185
|
+
|
|
186
|
+
**Job Management:**
|
|
187
|
+
- `getJob(id)` / `getJobs(filter)` - Query jobs
|
|
188
|
+
- `cancelJob(id)` / `cancelJobs(filter)` - Cancel jobs
|
|
189
|
+
- `retryJob(id)` / `retryJobs(filter)` - Retry failed jobs
|
|
190
|
+
- `deleteJob(id)` / `deleteJobs(filter)` - Delete jobs
|
|
191
|
+
- `rescheduleJob(id, date)` - Change execution time
|
|
192
|
+
|
|
193
|
+
**Observability:**
|
|
194
|
+
- `getQueueStats(filter?)` - Get queue statistics
|
|
195
|
+
- `isHealthy()` - Check scheduler health
|
|
196
|
+
- `getJobsWithCursor(opts)` - Paginated job list
|
|
197
|
+
|
|
198
|
+
> [!TIP]
|
|
199
|
+
> All methods on `MonqueService` delegate to the underlying `Monque` instance. For a complete list of methods and options, see the [@monque/core documentation](../../packages/core/README.md).
|
|
200
|
+
|
|
201
|
+
## Testing
|
|
202
|
+
|
|
203
|
+
Use `@tsed/platform-http/testing` and `PlatformTest` to test your workers. You can mock `MonqueService` or use a real Mongo connection with Testcontainers.
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { PlatformTest } from "@tsed/platform-http/testing";
|
|
207
|
+
import { MonqueService } from "@monque/tsed";
|
|
208
|
+
|
|
209
|
+
describe("EmailWorkers", () => {
|
|
210
|
+
beforeEach(PlatformTest.create);
|
|
211
|
+
afterEach(PlatformTest.reset);
|
|
212
|
+
|
|
213
|
+
it("should process email", async () => {
|
|
214
|
+
const service = PlatformTest.get<MonqueService>(MonqueService);
|
|
215
|
+
// ... test logic ...
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
ISC
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @monque/tsed
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#94](https://github.com/ueberBrot/monque/pull/94) [`2ff8c9c`](https://github.com/ueberBrot/monque/commit/2ff8c9c5e699d534ea7dc15229405db3688b961a) Thanks [@ueberBrot](https://github.com/ueberBrot)! - Initial release of @monque/tsed integration with @WorkerController, @Worker, and @Cron decorators.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#99](https://github.com/ueberBrot/monque/pull/99) [`2624dd0`](https://github.com/ueberBrot/monque/commit/2624dd0895846741bbacb958fa3305aed3013ae1) Thanks [@renovate](https://github.com/apps/renovate)! - chore(deps): update dependencies
|
|
12
|
+
|
|
13
|
+
- @monque/tsed: @monque/core (^1.1.0 → ^1.1.2)
|
|
14
|
+
|
|
15
|
+
- [#106](https://github.com/ueberBrot/monque/pull/106) [`aef0361`](https://github.com/ueberBrot/monque/commit/aef0361e4ac2ecb219e8edcd07c462daab116826) Thanks [@renovate](https://github.com/apps/renovate)! - chore(deps): update dependencies
|
|
16
|
+
|
|
17
|
+
- @monque/tsed: @tsed/core (^8.24.0 → ^8.24.1)
|
|
18
|
+
- @monque/tsed: @tsed/di (^8.24.0 → ^8.24.1)
|
|
19
|
+
- @monque/tsed: @tsed/mongoose (^8.24.0 → ^8.24.1)
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Maurice de Bruyn
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="../../assets/logo.svg" width="180" alt="Monque logo" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@monque/tsed</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/@monque/tsed">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/%40monque%2Ftsed?style=for-the-badge&label=%40monque%2Ftsed" alt="@monque/tsed version" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/ueberBrot/monque/actions/workflows/ci.yml">
|
|
12
|
+
<img src="https://img.shields.io/github/actions/workflow/status/ueberBrot/monque/ci.yml?branch=main&style=for-the-badge&logo=github" alt="CI Status" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://codecov.io/gh/ueberBrot/monque">
|
|
15
|
+
<img src="https://img.shields.io/codecov/c/github/ueberBrot/monque?style=for-the-badge&logo=codecov&logoColor=white" alt="Codecov" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://opensource.org/licenses/ISC">
|
|
18
|
+
<img src="https://img.shields.io/badge/License-ISC-blue.svg?style=for-the-badge" alt="License: ISC" />
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://bun.sh">
|
|
21
|
+
<img src="https://img.shields.io/badge/Built%20with-Bun-fbf0df?style=for-the-badge&logo=bun&logoColor=black" alt="Built with Bun" />
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
A **Ts.ED** integration for **Monque**, a robust, type-safe MongoDB job queue for TypeScript.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- 🎯 **Decorator-based API**: `@WorkerController`, `@Worker`, and `@Cron` for declarative job handling.
|
|
30
|
+
- 💉 **Dependency Injection**: Full support for Ts.ED DI (inject Services/Providers into your workers).
|
|
31
|
+
- 🔒 **Job Isolation**: Each job execution runs in a dedicated `DIContext` with Request Scope support.
|
|
32
|
+
- 🔍 **Type Safety**: Leverage TypeScript generics for fully typed job payloads.
|
|
33
|
+
- ⚡ **Full Monque Power**: Complete access to all `@monque/core` features (backoff, heartbeats, atomic locking).
|
|
34
|
+
- 🛠️ **Seamless Integration**: Native lifecycle hooks support (`$onInit`, `$onDestroy`) for graceful scheduler management.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bun add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or using npm/yarn/pnpm:
|
|
43
|
+
```bash
|
|
44
|
+
npm install @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
45
|
+
yarn add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
46
|
+
pnpm add @monque/tsed @monque/core @tsed/mongoose mongoose
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
Import `MonqueModule` in your `Server.ts` and configure the connection:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Configuration } from "@tsed/di";
|
|
55
|
+
import { MonqueModule } from "@monque/tsed";
|
|
56
|
+
import "@tsed/mongoose";
|
|
57
|
+
import "@tsed/platform-express"; // or @tsed/platform-koa
|
|
58
|
+
|
|
59
|
+
@Configuration({
|
|
60
|
+
imports: [
|
|
61
|
+
MonqueModule
|
|
62
|
+
],
|
|
63
|
+
mongoose: [
|
|
64
|
+
{
|
|
65
|
+
id: "default",
|
|
66
|
+
url: "mongodb://localhost:27017/my-app",
|
|
67
|
+
connectionOptions: {}
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
monque: {
|
|
71
|
+
enabled: true,
|
|
72
|
+
// Option 1: Reuse existing Mongoose connection (Recommended)
|
|
73
|
+
mongooseConnectionId: "default",
|
|
74
|
+
|
|
75
|
+
// Option 2: Provide existing Db instance via factory
|
|
76
|
+
// dbFactory: async () => {
|
|
77
|
+
// const client = new MongoClient(process.env.MONGO_URL);
|
|
78
|
+
// await client.connect();
|
|
79
|
+
// return client.db("my-app");
|
|
80
|
+
// },
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
export class Server {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Usage
|
|
87
|
+
|
|
88
|
+
### 1. Define a Worker Controller
|
|
89
|
+
|
|
90
|
+
Create a class decorated with `@WorkerController`. Methods decorated with `@Worker` will process jobs.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { WorkerController, Worker } from "@monque/tsed";
|
|
94
|
+
import { Job } from "@monque/core";
|
|
95
|
+
import { EmailService } from "./services/EmailService";
|
|
96
|
+
|
|
97
|
+
interface EmailPayload {
|
|
98
|
+
to: string;
|
|
99
|
+
subject: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@WorkerController("email") // Namespace prefix: "email."
|
|
103
|
+
export class EmailWorkers {
|
|
104
|
+
constructor(private emailService: EmailService) {}
|
|
105
|
+
|
|
106
|
+
@Worker("send", { concurrency: 5 }) // Job name: "email.send"
|
|
107
|
+
async sendEmail(job: Job<EmailPayload>) {
|
|
108
|
+
await this.emailService.send(
|
|
109
|
+
job.data.to,
|
|
110
|
+
job.data.subject
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 2. Schedule Jobs (Cron)
|
|
117
|
+
|
|
118
|
+
Use the `@Cron` decorator to schedule recurring tasks.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { WorkerController, Cron } from "@monque/tsed";
|
|
122
|
+
|
|
123
|
+
@WorkerController()
|
|
124
|
+
export class ReportWorkers {
|
|
125
|
+
|
|
126
|
+
@Cron("0 0 * * *", { name: "daily-report" })
|
|
127
|
+
async generateDailyReport() {
|
|
128
|
+
console.log("Generating report...");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 3. Enqueue Jobs
|
|
134
|
+
|
|
135
|
+
Inject `MonqueService` to dispatch jobs from anywhere in your app.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { Service, Inject } from "@tsed/di";
|
|
139
|
+
import { MonqueService } from "@monque/tsed";
|
|
140
|
+
|
|
141
|
+
@Service()
|
|
142
|
+
export class AuthService {
|
|
143
|
+
@Inject()
|
|
144
|
+
private monque: MonqueService;
|
|
145
|
+
|
|
146
|
+
async registerUser(user: User) {
|
|
147
|
+
// ... save user ...
|
|
148
|
+
|
|
149
|
+
// Dispatch background job
|
|
150
|
+
await this.monque.enqueue("email.send", {
|
|
151
|
+
to: user.email,
|
|
152
|
+
subject: "Welcome!"
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## API Reference
|
|
159
|
+
|
|
160
|
+
### Decorators
|
|
161
|
+
|
|
162
|
+
#### `@WorkerController(namespace?: string)`
|
|
163
|
+
Class decorator to register a worker controller.
|
|
164
|
+
- `namespace`: Optional prefix for all worker names in this class.
|
|
165
|
+
|
|
166
|
+
#### `@Worker(name: string, options?: WorkerOptions)`
|
|
167
|
+
Method decorator to register a job handler.
|
|
168
|
+
- `name`: Job name (combined with namespace).
|
|
169
|
+
- `options`: Supports all `@monque/core` [WorkerOptions](../../packages/core/README.md#new-monque-db-options) (concurrency, lockTimeout, etc.).
|
|
170
|
+
|
|
171
|
+
#### `@Cron(pattern: string, options?: ScheduleOptions)`
|
|
172
|
+
Method decorator to register a scheduled job.
|
|
173
|
+
- `pattern`: Cron expression (e.g., `* * * * *`).
|
|
174
|
+
- `options`: Supports all `@monque/core` [ScheduleOptions](../../packages/core/README.md#methods) (tz, job name override, etc.).
|
|
175
|
+
|
|
176
|
+
### Services
|
|
177
|
+
|
|
178
|
+
#### `MonqueService`
|
|
179
|
+
Injectable wrapper for the main `Monque` instance. It exposes the full Monque public API through dependency injection.
|
|
180
|
+
|
|
181
|
+
**Job Scheduling:**
|
|
182
|
+
- `enqueue(name, data, opts)` - Enqueue a job
|
|
183
|
+
- `schedule(cron, name, data, opts)` - Schedule a recurring job
|
|
184
|
+
- `now(name, data)` - Enqueue for immediate processing
|
|
185
|
+
|
|
186
|
+
**Job Management:**
|
|
187
|
+
- `getJob(id)` / `getJobs(filter)` - Query jobs
|
|
188
|
+
- `cancelJob(id)` / `cancelJobs(filter)` - Cancel jobs
|
|
189
|
+
- `retryJob(id)` / `retryJobs(filter)` - Retry failed jobs
|
|
190
|
+
- `deleteJob(id)` / `deleteJobs(filter)` - Delete jobs
|
|
191
|
+
- `rescheduleJob(id, date)` - Change execution time
|
|
192
|
+
|
|
193
|
+
**Observability:**
|
|
194
|
+
- `getQueueStats(filter?)` - Get queue statistics
|
|
195
|
+
- `isHealthy()` - Check scheduler health
|
|
196
|
+
- `getJobsWithCursor(opts)` - Paginated job list
|
|
197
|
+
|
|
198
|
+
> [!TIP]
|
|
199
|
+
> All methods on `MonqueService` delegate to the underlying `Monque` instance. For a complete list of methods and options, see the [@monque/core documentation](../../packages/core/README.md).
|
|
200
|
+
|
|
201
|
+
## Testing
|
|
202
|
+
|
|
203
|
+
Use `@tsed/platform-http/testing` and `PlatformTest` to test your workers. You can mock `MonqueService` or use a real Mongo connection with Testcontainers.
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { PlatformTest } from "@tsed/platform-http/testing";
|
|
207
|
+
import { MonqueService } from "@monque/tsed";
|
|
208
|
+
|
|
209
|
+
describe("EmailWorkers", () => {
|
|
210
|
+
beforeEach(PlatformTest.create);
|
|
211
|
+
afterEach(PlatformTest.reset);
|
|
212
|
+
|
|
213
|
+
it("should process email", async () => {
|
|
214
|
+
const service = PlatformTest.get<MonqueService>(MonqueService);
|
|
215
|
+
// ... test logic ...
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
ISC
|