fix: Added bullmq
This commit is contained in:
@@ -16,6 +16,10 @@ import { AuthorizationModule } from './authorization/authorization.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import { MailModule } from './mail/mail.module';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { BullBoardModule } from '@bull-board/nestjs';
|
||||
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
|
||||
import { ExpressAdapter } from '@bull-board/express';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -23,6 +27,21 @@ import { MailModule } from './mail/mail.module';
|
||||
isGlobal: true,
|
||||
}),
|
||||
EventEmitterModule.forRoot(),
|
||||
BullModule.forRoot({
|
||||
connection: {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
},
|
||||
}),
|
||||
BullBoardModule.forRoot({
|
||||
route: '/queues', // 👈 dashboard URL
|
||||
adapter: ExpressAdapter,
|
||||
}),
|
||||
|
||||
BullBoardModule.forFeature({
|
||||
name: 'mail', // 👈 register each queue you want visible
|
||||
adapter: BullMQAdapter,
|
||||
}),
|
||||
UserModule,
|
||||
AuthModule,
|
||||
RequestContextModule,
|
||||
|
||||
@@ -6,6 +6,8 @@ import { AuthGuard } from './guards/auth.guard';
|
||||
import { UserModule } from 'src/user/user.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { RequestContextModule } from 'core/als/request-context.module';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -18,6 +20,9 @@ import { RequestContextModule } from 'core/als/request-context.module';
|
||||
],
|
||||
controllers: [AuthController],
|
||||
imports: [
|
||||
BullModule.registerQueue({
|
||||
name: "mail"
|
||||
}),
|
||||
UserModule,
|
||||
JwtModule,
|
||||
RequestContextModule,
|
||||
|
||||
@@ -5,7 +5,8 @@ import * as bcrypt from 'bcrypt';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { TokenInputType } from './types';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Queue } from 'bullmq';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
|
||||
@Injectable()
|
||||
@Public()
|
||||
@@ -13,7 +14,7 @@ export class AuthService {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly eventEmitter: EventEmitter2
|
||||
@InjectQueue('mail') private readonly mailQueue: Queue
|
||||
) { }
|
||||
|
||||
async register(dto: RegisterUserRequestDTO) {
|
||||
@@ -23,7 +24,17 @@ export class AuthService {
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
this.eventEmitter.emit('user.sign_up', dto.email)
|
||||
this.mailQueue.add('send-welcome-email', {
|
||||
email: dto.email
|
||||
}, {
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
type: "exponential",
|
||||
delay: 3000,
|
||||
},
|
||||
removeOnComplete: true, // clean up Redis after success
|
||||
removeOnFail: false,
|
||||
})
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
18
src/mail/mail.consumer.ts
Normal file
18
src/mail/mail.consumer.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Processor, WorkerHost } from "@nestjs/bullmq";
|
||||
import { Job } from "bullmq";
|
||||
import { MailService } from "./mail.service";
|
||||
|
||||
@Processor('mail')
|
||||
export class MailConsumer extends WorkerHost {
|
||||
constructor(private readonly mailService: MailService) {
|
||||
super()
|
||||
}
|
||||
|
||||
async process(job: Job<{ email: string }>) {
|
||||
switch (job.name) {
|
||||
case 'send-welcome-email':
|
||||
await this.mailService.sendWelcomeMail({ to: job.data.email })
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Injectable } from "@nestjs/common"
|
||||
import { OnEvent } from "@nestjs/event-emitter"
|
||||
import { MailService } from "./mail.service"
|
||||
|
||||
@Injectable()
|
||||
export class MailListener {
|
||||
constructor(private readonly mailService: MailService) { }
|
||||
@OnEvent('user.sign_up')
|
||||
handleUserSignUp(email: string) {
|
||||
this.mailService.sendWelcomeMail({ to: email })
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MailService } from './mail.service';
|
||||
import { MailListener } from './mail.listener';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { MailConsumer } from './mail.consumer';
|
||||
|
||||
@Module({
|
||||
providers: [MailService, MailListener],
|
||||
imports: [
|
||||
BullModule.registerQueue({
|
||||
name: "welcome_mail"
|
||||
}),
|
||||
],
|
||||
providers: [MailService, MailConsumer],
|
||||
exports: [MailService]
|
||||
})
|
||||
export class MailModule { }
|
||||
|
||||
@@ -34,13 +34,13 @@ export class MailService {
|
||||
/*
|
||||
* SIGN-UP
|
||||
* */
|
||||
sendWelcomeMail({ to }: { to: string }) {
|
||||
async sendWelcomeMail({ to }: { to: string }) {
|
||||
if (!this.mailServiceAvailable)
|
||||
throw new Error("Mail service not available")
|
||||
|
||||
const email = EmailTemplates.signup_completed;
|
||||
|
||||
this.transporter.sendMail(
|
||||
await this.transporter.sendMail(
|
||||
{
|
||||
to,
|
||||
subject: email.subject,
|
||||
|
||||
Reference in New Issue
Block a user