@hazeljs/queue 0.2.0-beta.24
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 +21 -0
- package/README.md +174 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/queue.decorator.d.ts +30 -0
- package/dist/queue.decorator.d.ts.map +1 -0
- package/dist/queue.decorator.js +36 -0
- package/dist/queue.module.d.ts +61 -0
- package/dist/queue.module.d.ts.map +1 -0
- package/dist/queue.module.js +87 -0
- package/dist/queue.service.d.ts +53 -0
- package/dist/queue.service.d.ts.map +1 -0
- package/dist/queue.service.js +90 -0
- package/dist/queue.types.d.ts +46 -0
- package/dist/queue.types.d.ts.map +1 -0
- package/dist/queue.types.js +5 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 HazelJS Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# @hazeljs/queue
|
|
2
|
+
|
|
3
|
+
**Redis-backed Job Queue Module for HazelJS**
|
|
4
|
+
|
|
5
|
+
Add and process background jobs using BullMQ with Redis. Ideal for distributed systems, cron-triggered workloads, and long-running agent tasks.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hazeljs/queue)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Redis-backed** - Uses BullMQ for reliable, distributed job queues
|
|
13
|
+
- **QueueService** - Injectable service for adding jobs from controllers and services
|
|
14
|
+
- **@Queue decorator** - Mark methods as job processors for Worker setup
|
|
15
|
+
- **Job options** - Delay, priority, attempts, backoff, timeout
|
|
16
|
+
- **HazelJS integration** - Works with CronModule for distributed cron jobs
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @hazeljs/queue ioredis
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Import QueueModule
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { HazelModule } from '@hazeljs/core';
|
|
30
|
+
import { QueueModule } from '@hazeljs/queue';
|
|
31
|
+
|
|
32
|
+
@HazelModule({
|
|
33
|
+
imports: [
|
|
34
|
+
QueueModule.forRoot({
|
|
35
|
+
connection: {
|
|
36
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
37
|
+
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
],
|
|
41
|
+
})
|
|
42
|
+
export class AppModule {}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. Add Jobs
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Injectable } from '@hazeljs/core';
|
|
49
|
+
import { QueueService } from '@hazeljs/queue';
|
|
50
|
+
|
|
51
|
+
@Injectable()
|
|
52
|
+
export class EmailService {
|
|
53
|
+
constructor(private queue: QueueService) {}
|
|
54
|
+
|
|
55
|
+
async sendWelcomeEmail(userId: string, email: string) {
|
|
56
|
+
await this.queue.add('emails', 'welcome', { userId, email });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async sendDelayedReminder(userId: string, delayMs: number) {
|
|
60
|
+
await this.queue.addDelayed('emails', 'reminder', { userId }, delayMs);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async processWithRetry(data: { orderId: string }) {
|
|
64
|
+
await this.queue.addWithRetry('orders', 'process', data, {
|
|
65
|
+
attempts: 3,
|
|
66
|
+
backoff: { type: 'exponential', delay: 1000 },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Process Jobs with BullMQ Worker
|
|
73
|
+
|
|
74
|
+
Create a worker process (or run alongside your app) to process jobs:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { Worker } from 'bullmq';
|
|
78
|
+
|
|
79
|
+
const worker = new Worker(
|
|
80
|
+
'emails',
|
|
81
|
+
async (job) => {
|
|
82
|
+
if (job.name === 'welcome') {
|
|
83
|
+
await sendWelcomeEmail(job.data.userId, job.data.email);
|
|
84
|
+
} else if (job.name === 'reminder') {
|
|
85
|
+
await sendReminder(job.data.userId);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
connection: {
|
|
90
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
91
|
+
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
worker.on('completed', (job) => console.log(`Job ${job.id} completed`));
|
|
97
|
+
worker.on('failed', (job, err) => console.error(`Job ${job?.id} failed:`, err));
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 4. Using @Queue Decorator for Processor Metadata
|
|
101
|
+
|
|
102
|
+
The `@Queue` decorator marks methods as job processors. Use `QueueModule.getProcessorMetadata()` to get processor info for Worker setup:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { Injectable } from '@hazeljs/core';
|
|
106
|
+
import { Queue } from '@hazeljs/queue';
|
|
107
|
+
|
|
108
|
+
@Injectable()
|
|
109
|
+
export class EmailProcessor {
|
|
110
|
+
@Queue('emails')
|
|
111
|
+
async handleWelcome(job: { data: { userId: string; email: string } }) {
|
|
112
|
+
await this.sendWelcome(job.data.userId, job.data.email);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
@Queue('emails')
|
|
116
|
+
async handleReminder(job: { data: { userId: string } }) {
|
|
117
|
+
await this.sendReminder(job.data.userId);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private async sendWelcome(userId: string, email: string) {
|
|
121
|
+
// ...
|
|
122
|
+
}
|
|
123
|
+
private async sendReminder(userId: string) {
|
|
124
|
+
// ...
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Integration with Cron
|
|
130
|
+
|
|
131
|
+
For distributed cron jobs, enqueue work from cron handlers instead of doing it inline:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { Injectable } from '@hazeljs/core';
|
|
135
|
+
import { Cron, CronExpression } from '@hazeljs/cron';
|
|
136
|
+
import { QueueService } from '@hazeljs/queue';
|
|
137
|
+
|
|
138
|
+
@Injectable()
|
|
139
|
+
export class TaskService {
|
|
140
|
+
constructor(private queue: QueueService) {}
|
|
141
|
+
|
|
142
|
+
@Cron({
|
|
143
|
+
name: 'daily-cleanup',
|
|
144
|
+
cronTime: CronExpression.EVERY_DAY_AT_MIDNIGHT,
|
|
145
|
+
})
|
|
146
|
+
async triggerCleanup() {
|
|
147
|
+
// Enqueue for distributed processing instead of running inline
|
|
148
|
+
await this.queue.add('maintenance', 'daily-cleanup', {});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## API Reference
|
|
154
|
+
|
|
155
|
+
### QueueService
|
|
156
|
+
|
|
157
|
+
- `add(queueName, jobName, data?, options?)` - Add a job
|
|
158
|
+
- `addDelayed(queueName, jobName, data, delayMs)` - Add a delayed job
|
|
159
|
+
- `addWithRetry(queueName, jobName, data, options)` - Add with retry config
|
|
160
|
+
- `getQueue(name)` - Get BullMQ Queue instance
|
|
161
|
+
- `close()` - Close all queue connections
|
|
162
|
+
|
|
163
|
+
### Job Options (JobsOptions)
|
|
164
|
+
|
|
165
|
+
- `delay` - Delay before processing (ms)
|
|
166
|
+
- `priority` - Higher = processed first
|
|
167
|
+
- `attempts` - Retry count
|
|
168
|
+
- `backoff` - `{ type: 'fixed' | 'exponential', delay: number }`
|
|
169
|
+
- `timeout` - Job timeout (ms)
|
|
170
|
+
|
|
171
|
+
## See Also
|
|
172
|
+
|
|
173
|
+
- [Cron Jobs Guide](../../docs/guides/cron-jobs.md) - Schedule jobs that enqueue to Queue
|
|
174
|
+
- [BullMQ Documentation](https://docs.bullmq.io/) - Underlying queue library
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hazeljs/queue - Redis-backed job queue module for HazelJS using BullMQ
|
|
3
|
+
*/
|
|
4
|
+
export { QueueModule, type QueueModuleOptions } from './queue.module';
|
|
5
|
+
export { QueueService } from './queue.service';
|
|
6
|
+
export { Queue, getQueueProcessorMetadata } from './queue.decorator';
|
|
7
|
+
export type { QueueDecoratorOptions } from './queue.decorator';
|
|
8
|
+
export type { RedisConnectionOptions, QueueJobOptions, QueueProcessorMetadata, } from './queue.types';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AACrE,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,YAAY,EACV,sBAAsB,EACtB,eAAe,EACf,sBAAsB,GACvB,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @hazeljs/queue - Redis-backed job queue module for HazelJS using BullMQ
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getQueueProcessorMetadata = exports.Queue = exports.QueueService = exports.QueueModule = void 0;
|
|
7
|
+
var queue_module_1 = require("./queue.module");
|
|
8
|
+
Object.defineProperty(exports, "QueueModule", { enumerable: true, get: function () { return queue_module_1.QueueModule; } });
|
|
9
|
+
var queue_service_1 = require("./queue.service");
|
|
10
|
+
Object.defineProperty(exports, "QueueService", { enumerable: true, get: function () { return queue_service_1.QueueService; } });
|
|
11
|
+
var queue_decorator_1 = require("./queue.decorator");
|
|
12
|
+
Object.defineProperty(exports, "Queue", { enumerable: true, get: function () { return queue_decorator_1.Queue; } });
|
|
13
|
+
Object.defineProperty(exports, "getQueueProcessorMetadata", { enumerable: true, get: function () { return queue_decorator_1.getQueueProcessorMetadata; } });
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Metadata key for queue processors
|
|
4
|
+
*/
|
|
5
|
+
export declare const QUEUE_PROCESSOR_METADATA_KEY: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* Options for the @Queue decorator
|
|
8
|
+
*/
|
|
9
|
+
export interface QueueDecoratorOptions {
|
|
10
|
+
/** Queue name (defaults to class.methodName) */
|
|
11
|
+
name?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Decorator to mark a method as a queue job processor
|
|
15
|
+
* The method will be invoked when jobs are processed from the named queue
|
|
16
|
+
*
|
|
17
|
+
* @param queueName - Name of the queue this processor handles
|
|
18
|
+
* @param options - Optional processor configuration
|
|
19
|
+
*/
|
|
20
|
+
export declare function Queue(queueName: string, options?: QueueDecoratorOptions): MethodDecorator;
|
|
21
|
+
/**
|
|
22
|
+
* Get queue processor metadata from a class
|
|
23
|
+
*/
|
|
24
|
+
export declare function getQueueProcessorMetadata(target: object): Array<{
|
|
25
|
+
queueName: string;
|
|
26
|
+
methodName: string;
|
|
27
|
+
target: object;
|
|
28
|
+
options: QueueDecoratorOptions;
|
|
29
|
+
}>;
|
|
30
|
+
//# sourceMappingURL=queue.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.decorator.d.ts","sourceRoot":"","sources":["../src/queue.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B;;GAEG;AACH,eAAO,MAAM,4BAA4B,eAA6B,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,eAAe,CAczF;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IAC/D,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,qBAAqB,CAAC;CAChC,CAAC,CAED"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QUEUE_PROCESSOR_METADATA_KEY = void 0;
|
|
4
|
+
exports.Queue = Queue;
|
|
5
|
+
exports.getQueueProcessorMetadata = getQueueProcessorMetadata;
|
|
6
|
+
require("reflect-metadata");
|
|
7
|
+
/**
|
|
8
|
+
* Metadata key for queue processors
|
|
9
|
+
*/
|
|
10
|
+
exports.QUEUE_PROCESSOR_METADATA_KEY = Symbol('queue:processors');
|
|
11
|
+
/**
|
|
12
|
+
* Decorator to mark a method as a queue job processor
|
|
13
|
+
* The method will be invoked when jobs are processed from the named queue
|
|
14
|
+
*
|
|
15
|
+
* @param queueName - Name of the queue this processor handles
|
|
16
|
+
* @param options - Optional processor configuration
|
|
17
|
+
*/
|
|
18
|
+
function Queue(queueName, options) {
|
|
19
|
+
return (target, propertyKey, _descriptor) => {
|
|
20
|
+
const existing = Reflect.getMetadata(exports.QUEUE_PROCESSOR_METADATA_KEY, target.constructor) || [];
|
|
21
|
+
const metadata = {
|
|
22
|
+
queueName,
|
|
23
|
+
methodName: propertyKey.toString(),
|
|
24
|
+
target,
|
|
25
|
+
options: options || {},
|
|
26
|
+
};
|
|
27
|
+
existing.push(metadata);
|
|
28
|
+
Reflect.defineMetadata(exports.QUEUE_PROCESSOR_METADATA_KEY, existing, target.constructor);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get queue processor metadata from a class
|
|
33
|
+
*/
|
|
34
|
+
function getQueueProcessorMetadata(target) {
|
|
35
|
+
return Reflect.getMetadata(exports.QUEUE_PROCESSOR_METADATA_KEY, target.constructor) || [];
|
|
36
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { QueueService } from './queue.service';
|
|
2
|
+
import type { RedisConnectionOptions } from './queue.types';
|
|
3
|
+
/**
|
|
4
|
+
* Queue module options
|
|
5
|
+
*/
|
|
6
|
+
export interface QueueModuleOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Redis connection options for BullMQ
|
|
9
|
+
* Passed to ioredis constructor
|
|
10
|
+
*/
|
|
11
|
+
connection: RedisConnectionOptions;
|
|
12
|
+
/**
|
|
13
|
+
* Whether this is a global module
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
isGlobal?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Queue module for HazelJS
|
|
20
|
+
* Provides Redis-backed job queues using BullMQ
|
|
21
|
+
*/
|
|
22
|
+
export declare class QueueModule {
|
|
23
|
+
/**
|
|
24
|
+
* Configure queue module with Redis connection
|
|
25
|
+
*/
|
|
26
|
+
static forRoot(options: QueueModuleOptions): {
|
|
27
|
+
module: typeof QueueModule;
|
|
28
|
+
providers: Array<{
|
|
29
|
+
provide: typeof QueueService;
|
|
30
|
+
useFactory: () => QueueService;
|
|
31
|
+
}>;
|
|
32
|
+
exports: Array<typeof QueueService>;
|
|
33
|
+
global: boolean;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Register queue processors from a provider instance
|
|
37
|
+
* Call this after the provider is instantiated and workers are ready
|
|
38
|
+
*
|
|
39
|
+
* Note: For BullMQ workers, you typically need to create Worker instances
|
|
40
|
+
* that call into your processor methods. This helper retrieves metadata
|
|
41
|
+
* for integration with worker setup.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const taskService = container.resolve(TaskService);
|
|
46
|
+
* const metadata = QueueModule.getProcessorMetadata(taskService);
|
|
47
|
+
* // Use metadata to set up BullMQ Workers
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
static getProcessorMetadata(provider: object): Array<{
|
|
51
|
+
queueName: string;
|
|
52
|
+
methodName: string;
|
|
53
|
+
options: Record<string, unknown>;
|
|
54
|
+
}>;
|
|
55
|
+
/**
|
|
56
|
+
* Get QueueService from the container and ensure it has connection configured
|
|
57
|
+
* Useful when using QueueModule without forRoot (e.g. manual connection setup)
|
|
58
|
+
*/
|
|
59
|
+
static getQueueService(): QueueService;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=queue.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.module.d.ts","sourceRoot":"","sources":["../src/queue.module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAI/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,UAAU,EAAE,sBAAsB,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;GAGG;AACH,qBAIa,WAAW;IACtB;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,kBAAkB,GAAG;QAC3C,MAAM,EAAE,OAAO,WAAW,CAAC;QAC3B,SAAS,EAAE,KAAK,CAAC;YAAE,OAAO,EAAE,OAAO,YAAY,CAAC;YAAC,UAAU,EAAE,MAAM,YAAY,CAAA;SAAE,CAAC,CAAC;QACnF,OAAO,EAAE,KAAK,CAAC,OAAO,YAAY,CAAC,CAAC;QACpC,MAAM,EAAE,OAAO,CAAC;KACjB;IAsBD;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;QACnD,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC,CAAC;IASF;;;OAGG;IACH,MAAM,CAAC,eAAe,IAAI,YAAY;CAQvC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var QueueModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.QueueModule = void 0;
|
|
14
|
+
const core_1 = require("@hazeljs/core");
|
|
15
|
+
const queue_service_1 = require("./queue.service");
|
|
16
|
+
const queue_decorator_1 = require("./queue.decorator");
|
|
17
|
+
const core_2 = require("@hazeljs/core");
|
|
18
|
+
const core_3 = __importDefault(require("@hazeljs/core"));
|
|
19
|
+
/**
|
|
20
|
+
* Queue module for HazelJS
|
|
21
|
+
* Provides Redis-backed job queues using BullMQ
|
|
22
|
+
*/
|
|
23
|
+
let QueueModule = QueueModule_1 = class QueueModule {
|
|
24
|
+
/**
|
|
25
|
+
* Configure queue module with Redis connection
|
|
26
|
+
*/
|
|
27
|
+
static forRoot(options) {
|
|
28
|
+
const { connection, isGlobal = true } = options;
|
|
29
|
+
core_3.default.info('Configuring queue module with BullMQ...');
|
|
30
|
+
const queueServiceProvider = {
|
|
31
|
+
provide: queue_service_1.QueueService,
|
|
32
|
+
useFactory: () => {
|
|
33
|
+
const service = new queue_service_1.QueueService();
|
|
34
|
+
service.setConnection(connection);
|
|
35
|
+
return service;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
module: QueueModule_1,
|
|
40
|
+
providers: [queueServiceProvider],
|
|
41
|
+
exports: [queue_service_1.QueueService],
|
|
42
|
+
global: isGlobal,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Register queue processors from a provider instance
|
|
47
|
+
* Call this after the provider is instantiated and workers are ready
|
|
48
|
+
*
|
|
49
|
+
* Note: For BullMQ workers, you typically need to create Worker instances
|
|
50
|
+
* that call into your processor methods. This helper retrieves metadata
|
|
51
|
+
* for integration with worker setup.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const taskService = container.resolve(TaskService);
|
|
56
|
+
* const metadata = QueueModule.getProcessorMetadata(taskService);
|
|
57
|
+
* // Use metadata to set up BullMQ Workers
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
static getProcessorMetadata(provider) {
|
|
61
|
+
const metadata = (0, queue_decorator_1.getQueueProcessorMetadata)(provider);
|
|
62
|
+
return metadata.map((m) => ({
|
|
63
|
+
queueName: m.queueName,
|
|
64
|
+
methodName: m.methodName,
|
|
65
|
+
options: m.options,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get QueueService from the container and ensure it has connection configured
|
|
70
|
+
* Useful when using QueueModule without forRoot (e.g. manual connection setup)
|
|
71
|
+
*/
|
|
72
|
+
static getQueueService() {
|
|
73
|
+
const container = core_2.Container.getInstance();
|
|
74
|
+
const service = container.resolve(queue_service_1.QueueService);
|
|
75
|
+
if (!service) {
|
|
76
|
+
throw new Error('QueueService not found. Ensure QueueModule is imported.');
|
|
77
|
+
}
|
|
78
|
+
return service;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.QueueModule = QueueModule;
|
|
82
|
+
exports.QueueModule = QueueModule = QueueModule_1 = __decorate([
|
|
83
|
+
(0, core_1.HazelModule)({
|
|
84
|
+
providers: [queue_service_1.QueueService],
|
|
85
|
+
exports: [queue_service_1.QueueService],
|
|
86
|
+
})
|
|
87
|
+
], QueueModule);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Queue as BullQueue, JobsOptions } from 'bullmq';
|
|
2
|
+
import type { RedisConnectionOptions } from './queue.types';
|
|
3
|
+
/**
|
|
4
|
+
* Queue service for adding and managing jobs
|
|
5
|
+
* Uses BullMQ for Redis-backed job queues
|
|
6
|
+
*/
|
|
7
|
+
export declare class QueueService {
|
|
8
|
+
private queues;
|
|
9
|
+
private connection;
|
|
10
|
+
/**
|
|
11
|
+
* Initialize the queue service with Redis connection options
|
|
12
|
+
* Must be called before using add() or getQueue()
|
|
13
|
+
*/
|
|
14
|
+
setConnection(connection: RedisConnectionOptions): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get or create a BullMQ Queue instance for the given name
|
|
17
|
+
*/
|
|
18
|
+
getQueue<T = unknown>(name: string): BullQueue<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Add a job to a queue
|
|
21
|
+
*
|
|
22
|
+
* @param queueName - Name of the queue
|
|
23
|
+
* @param jobName - Job name/type
|
|
24
|
+
* @param data - Job payload
|
|
25
|
+
* @param options - Job options (delay, priority, attempts, backoff, etc.)
|
|
26
|
+
*/
|
|
27
|
+
add<T = unknown>(queueName: string, jobName: string, data?: T, options?: JobsOptions): Promise<{
|
|
28
|
+
id: string | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Add a job with a delay
|
|
32
|
+
*/
|
|
33
|
+
addDelayed<T = unknown>(queueName: string, jobName: string, data: T, delayMs: number): Promise<{
|
|
34
|
+
id: string | undefined;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Add a job with retry configuration
|
|
38
|
+
*/
|
|
39
|
+
addWithRetry<T = unknown>(queueName: string, jobName: string, data: T, options: {
|
|
40
|
+
attempts?: number;
|
|
41
|
+
backoff?: {
|
|
42
|
+
type: 'fixed' | 'exponential';
|
|
43
|
+
delay: number;
|
|
44
|
+
};
|
|
45
|
+
}): Promise<{
|
|
46
|
+
id: string | undefined;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Close all queue connections
|
|
50
|
+
*/
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=queue.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.service.d.ts","sourceRoot":"","sources":["../src/queue.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAG5D;;;GAGG;AACH,qBACa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAgC;IAC9C,OAAO,CAAC,UAAU,CAAuC;IAEzD;;;OAGG;IACH,aAAa,CAAC,UAAU,EAAE,sBAAsB,GAAG,IAAI;IAKvD;;OAEG;IACH,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IAkBjD;;;;;;;OAOG;IACG,GAAG,CAAC,CAAC,GAAG,OAAO,EACnB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAYtC;;OAEG;IACG,UAAU,CAAC,CAAC,GAAG,OAAO,EAC1B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,MAAM,GACd,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAItC;;OAEG;IACG,YAAY,CAAC,CAAC,GAAG,OAAO,EAC5B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,CAAC,EACP,OAAO,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE;YAAE,IAAI,EAAE,OAAO,GAAG,aAAa,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,GACzF,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAItC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7B"}
|
|
@@ -0,0 +1,90 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QueueService = void 0;
|
|
13
|
+
const core_1 = require("@hazeljs/core");
|
|
14
|
+
const bullmq_1 = require("bullmq");
|
|
15
|
+
const core_2 = __importDefault(require("@hazeljs/core"));
|
|
16
|
+
/**
|
|
17
|
+
* Queue service for adding and managing jobs
|
|
18
|
+
* Uses BullMQ for Redis-backed job queues
|
|
19
|
+
*/
|
|
20
|
+
let QueueService = class QueueService {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.queues = new Map();
|
|
23
|
+
this.connection = null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Initialize the queue service with Redis connection options
|
|
27
|
+
* Must be called before using add() or getQueue()
|
|
28
|
+
*/
|
|
29
|
+
setConnection(connection) {
|
|
30
|
+
this.connection = connection;
|
|
31
|
+
core_2.default.info('QueueService connection configured');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get or create a BullMQ Queue instance for the given name
|
|
35
|
+
*/
|
|
36
|
+
getQueue(name) {
|
|
37
|
+
if (!this.connection) {
|
|
38
|
+
throw new Error('QueueService not configured. Call setConnection() or use QueueModule.forRoot() with connection options.');
|
|
39
|
+
}
|
|
40
|
+
let queue = this.queues.get(name);
|
|
41
|
+
if (!queue) {
|
|
42
|
+
queue = new bullmq_1.Queue(name, {
|
|
43
|
+
connection: this.connection,
|
|
44
|
+
});
|
|
45
|
+
this.queues.set(name, queue);
|
|
46
|
+
core_2.default.debug(`Queue "${name}" created`);
|
|
47
|
+
}
|
|
48
|
+
return queue;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add a job to a queue
|
|
52
|
+
*
|
|
53
|
+
* @param queueName - Name of the queue
|
|
54
|
+
* @param jobName - Job name/type
|
|
55
|
+
* @param data - Job payload
|
|
56
|
+
* @param options - Job options (delay, priority, attempts, backoff, etc.)
|
|
57
|
+
*/
|
|
58
|
+
async add(queueName, jobName, data, options) {
|
|
59
|
+
const queue = this.getQueue(queueName);
|
|
60
|
+
// BullMQ has strict generics for job names; cast for dynamic queue/job names
|
|
61
|
+
const job = await queue.add(jobName, data ?? {}, options);
|
|
62
|
+
core_2.default.debug(`Job added to queue "${queueName}": ${jobName} (id: ${job.id})`);
|
|
63
|
+
return { id: job.id };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Add a job with a delay
|
|
67
|
+
*/
|
|
68
|
+
async addDelayed(queueName, jobName, data, delayMs) {
|
|
69
|
+
return this.add(queueName, jobName, data, { delay: delayMs });
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Add a job with retry configuration
|
|
73
|
+
*/
|
|
74
|
+
async addWithRetry(queueName, jobName, data, options) {
|
|
75
|
+
return this.add(queueName, jobName, data, options);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Close all queue connections
|
|
79
|
+
*/
|
|
80
|
+
async close() {
|
|
81
|
+
const closePromises = Array.from(this.queues.values()).map((q) => q.close());
|
|
82
|
+
await Promise.all(closePromises);
|
|
83
|
+
this.queues.clear();
|
|
84
|
+
core_2.default.info('QueueService: all queues closed');
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
exports.QueueService = QueueService;
|
|
88
|
+
exports.QueueService = QueueService = __decorate([
|
|
89
|
+
(0, core_1.Injectable)()
|
|
90
|
+
], QueueService);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Queue module types
|
|
3
|
+
*/
|
|
4
|
+
import type { JobsOptions } from 'bullmq';
|
|
5
|
+
/**
|
|
6
|
+
* Redis connection options for BullMQ
|
|
7
|
+
* Passed to ioredis constructor
|
|
8
|
+
*/
|
|
9
|
+
export interface RedisConnectionOptions {
|
|
10
|
+
host?: string;
|
|
11
|
+
port?: number;
|
|
12
|
+
password?: string;
|
|
13
|
+
db?: number;
|
|
14
|
+
keyPrefix?: string;
|
|
15
|
+
maxRetriesPerRequest?: number | null;
|
|
16
|
+
enableReadyCheck?: boolean;
|
|
17
|
+
retryStrategy?: (times: number) => number | null;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for adding a job to the queue
|
|
22
|
+
*/
|
|
23
|
+
export interface QueueJobOptions extends Omit<JobsOptions, 'repeat'> {
|
|
24
|
+
/** Delay before job is processed (ms) */
|
|
25
|
+
delay?: number;
|
|
26
|
+
/** Job priority (higher = processed first) */
|
|
27
|
+
priority?: number;
|
|
28
|
+
/** Number of attempts before failing */
|
|
29
|
+
attempts?: number;
|
|
30
|
+
/** Backoff strategy: 'fixed' | 'exponential' */
|
|
31
|
+
backoff?: {
|
|
32
|
+
type: 'fixed' | 'exponential';
|
|
33
|
+
delay: number;
|
|
34
|
+
};
|
|
35
|
+
/** Job timeout (ms) */
|
|
36
|
+
timeout?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Queue processor metadata
|
|
40
|
+
*/
|
|
41
|
+
export interface QueueProcessorMetadata {
|
|
42
|
+
queueName: string;
|
|
43
|
+
methodName: string;
|
|
44
|
+
target: object;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=queue.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.types.d.ts","sourceRoot":"","sources":["../src/queue.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oBAAoB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IACjD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAAC,WAAW,EAAE,QAAQ,CAAC;IAClE,yCAAyC;IACzC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wCAAwC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,OAAO,CAAC,EAAE;QACR,IAAI,EAAE,OAAO,GAAG,aAAa,CAAC;QAC9B,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;IACF,uBAAuB;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hazeljs/queue",
|
|
3
|
+
"version": "0.2.0-beta.24",
|
|
4
|
+
"description": "Redis-backed job queue module for HazelJS framework using BullMQ",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest --coverage --passWithNoTests",
|
|
13
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
14
|
+
"lint:fix": "eslint \"src/**/*.ts\" --fix",
|
|
15
|
+
"clean": "rm -rf dist"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"bullmq": "^5.34.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^20.17.50",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^8.18.2",
|
|
23
|
+
"@typescript-eslint/parser": "^8.18.2",
|
|
24
|
+
"eslint": "^8.56.0",
|
|
25
|
+
"jest": "^29.7.0",
|
|
26
|
+
"ts-jest": "^29.1.2",
|
|
27
|
+
"typescript": "^5.3.3"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/hazel-js/hazeljs.git",
|
|
35
|
+
"directory": "packages/queue"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"hazeljs",
|
|
39
|
+
"queue",
|
|
40
|
+
"jobs",
|
|
41
|
+
"bullmq",
|
|
42
|
+
"redis",
|
|
43
|
+
"background-jobs"
|
|
44
|
+
],
|
|
45
|
+
"author": "Muhammad Arslan <muhammad.arslan@hazeljs.com>",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/hazeljs/hazel-js/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://hazeljs.com",
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@hazeljs/core": ">=0.2.0-beta.0",
|
|
53
|
+
"ioredis": ">=5.0.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"ioredis": {
|
|
57
|
+
"optional": false,
|
|
58
|
+
"description": "Required for Redis connection. BullMQ uses ioredis for Redis connectivity."
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "874f2959b320ac617212ea56b295a54627c6e24a"
|
|
62
|
+
}
|