Skip to content

Commit 5233c56

Browse files
authored
Merge pull request #61 from vernu/job-queue
Implement optional job queue for sending sms with delay
2 parents ecfab77 + f676393 commit 5233c56

File tree

10 files changed

+851
-161
lines changed

10 files changed

+851
-161
lines changed

api/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ MAIL_USER=
1919
MAIL_PASS=
2020
MAIL_FROM=
2121
MAIL_REPLY_TO=textbee.dev@gmail.com
22+
23+
# SMS Queue Configuration
24+
USE_SMS_QUEUE=false
25+
REDIS_URL=redis://localhost:6379 # if queue is enabled, redis url is required

api/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
},
2222
"dependencies": {
2323
"@nest-modules/mailer": "^1.3.22",
24+
"@nestjs/bull": "^11.0.2",
2425
"@nestjs/common": "^10.4.5",
26+
"@nestjs/config": "^4.0.1",
2527
"@nestjs/core": "^10.4.5",
2628
"@nestjs/jwt": "^10.2.0",
29+
"@nestjs/mapped-types": "^2.1.0",
2730
"@nestjs/mongoose": "^10.0.10",
2831
"@nestjs/passport": "^10.0.3",
2932
"@nestjs/platform-express": "^10.4.5",
@@ -33,10 +36,13 @@
3336
"@polar-sh/sdk": "^0.30.0",
3437
"axios": "^1.8.2",
3538
"bcryptjs": "^2.4.3",
39+
"bull": "^4.16.5",
40+
"class-validator": "^0.14.1",
3641
"dotenv": "^16.4.5",
3742
"express": "^4.21.2",
3843
"firebase-admin": "^12.6.0",
3944
"handlebars": "^4.7.8",
45+
"ioredis": "^5.6.0",
4046
"mongoose": "^8.12.1",
4147
"nodemailer": "^6.10.0",
4248
"passport": "^0.7.0",

api/pnpm-lock.yaml

Lines changed: 420 additions & 116 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/app.module.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Module } from '@nestjs/common'
1+
import {
2+
MiddlewareConsumer,
3+
Module,
4+
NestModule,
5+
RequestMethod,
6+
} from '@nestjs/common'
27
import { MongooseModule } from '@nestjs/mongoose'
38
import { GatewayModule } from './gateway/gateway.module'
49
import { AuthModule } from './auth/auth.module'
@@ -7,19 +12,45 @@ import { ThrottlerModule } from '@nestjs/throttler'
712
import { APP_GUARD } from '@nestjs/core/constants'
813
import { WebhookModule } from './webhook/webhook.module'
914
import { ThrottlerByIpGuard } from './auth/guards/throttle-by-ip.guard'
15+
import { Injectable, NestMiddleware } from '@nestjs/common'
16+
import { Request, Response, NextFunction } from 'express'
1017
import { ScheduleModule } from '@nestjs/schedule'
11-
import { BillingModule } from './billing/billing.module';
18+
import { BillingModule } from './billing/billing.module'
19+
import { ConfigModule, ConfigService } from '@nestjs/config'
20+
import { BullModule } from '@nestjs/bull'
21+
22+
@Injectable()
23+
export class LoggerMiddleware implements NestMiddleware {
24+
use(req: Request, res: Response, next: NextFunction) {
25+
console.log('req.originalUrl: ', req.originalUrl)
26+
if (next) {
27+
next()
28+
}
29+
}
30+
}
1231

1332
@Module({
1433
imports: [
1534
MongooseModule.forRoot(process.env.MONGO_URI),
35+
ConfigModule.forRoot({
36+
isGlobal: true,
37+
}),
1638
ThrottlerModule.forRoot([
1739
{
1840
ttl: 60000,
1941
limit: 60,
2042
},
2143
]),
2244
ScheduleModule.forRoot(),
45+
BullModule.forRootAsync({
46+
imports: [ConfigModule],
47+
inject: [ConfigService],
48+
useFactory: async (configService: ConfigService) => {
49+
return {
50+
redis: configService.get('REDIS_URL'),
51+
}
52+
},
53+
}),
2354
AuthModule,
2455
UsersModule,
2556
GatewayModule,
@@ -34,4 +65,10 @@ import { BillingModule } from './billing/billing.module';
3465
},
3566
],
3667
})
37-
export class AppModule {}
68+
export class AppModule implements NestModule {
69+
configure(consumer: MiddlewareConsumer) {
70+
consumer
71+
.apply(LoggerMiddleware)
72+
.forRoutes({ path: '*', method: RequestMethod.ALL })
73+
}
74+
}

api/src/gateway/gateway.module.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { SMS, SMSSchema } from './schemas/sms.schema'
99
import { SMSBatch, SMSBatchSchema } from './schemas/sms-batch.schema'
1010
import { WebhookModule } from 'src/webhook/webhook.module'
1111
import { BillingModule } from 'src/billing/billing.module'
12+
import { BullModule } from '@nestjs/bull'
13+
import { ConfigModule } from '@nestjs/config'
14+
import { SmsQueueService } from './queue/sms-queue.service'
15+
import { SmsQueueProcessor } from './queue/sms-queue.processor'
1216

1317
@Module({
1418
imports: [
@@ -26,13 +30,26 @@ import { BillingModule } from 'src/billing/billing.module'
2630
schema: SMSBatchSchema,
2731
},
2832
]),
33+
BullModule.registerQueue({
34+
name: 'sms',
35+
defaultJobOptions: {
36+
attempts: 2,
37+
backoff: {
38+
type: 'exponential',
39+
delay: 1000,
40+
},
41+
removeOnComplete: false,
42+
removeOnFail: false,
43+
},
44+
}),
2945
AuthModule,
3046
UsersModule,
3147
WebhookModule,
3248
forwardRef(() => BillingModule),
49+
ConfigModule,
3350
],
3451
controllers: [GatewayController],
35-
providers: [GatewayService],
36-
exports: [MongooseModule, GatewayService],
52+
providers: [GatewayService, SmsQueueService, SmsQueueProcessor],
53+
exports: [MongooseModule, GatewayService, SmsQueueService],
3754
})
3855
export class GatewayModule {}

0 commit comments

Comments
 (0)