Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add configuration for retries
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 10, 2020
1 parent ef80fc2 commit 8e89692
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Configuration {
email: {
name: string;
from: string;
retries: number;
ses?: {
accessKeyId: string;
secretAccessKey: string;
Expand All @@ -45,7 +46,12 @@ export interface Configuration {
};
};

webhooks: {
retries: number;
};

sms: {
retries: number;
smsServiceName: string;
twilioAccountSid: string;
twilioAuthToken: string;
Expand Down
5 changes: 5 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const configuration: Configuration = {
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
from: process.env.EMAIL_FROM ?? '',
retries: int(process.env.EMAIL_FAIL_RETRIES, 3),
ses: {
accessKeyId: process.env.EMAIL_SES_ACCESS_KEY_ID ?? '',
secretAccessKey: process.env.EMAIL_SES_SECRET_ACCESS_KEY ?? '',
Expand All @@ -48,7 +49,11 @@ const configuration: Configuration = {
},
},
},
webhooks: {
retries: int(process.env.WEBHOOK_FAIL_RETRIES, 3),
},
sms: {
retries: int(process.env.SMS_FAIL_RETRIES, 3),
smsServiceName: process.env.SMS_SERVICE_NAME ?? '',
twilioAccountSid: process.env.TWILIO_ACCOUNT_SID ?? '',
twilioAuthToken: process.env.TWILIO_AUTH_TOKEN ?? '',
Expand Down
3 changes: 2 additions & 1 deletion src/modules/webhooks/webhooks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { StripeModule } from '../stripe/stripe.module';
import { TokensModule } from '../../providers/tokens/tokens.module';
import { WebhookController } from './webhooks.controller';
import { WebhooksService } from './webhooks.service';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [PrismaModule, TokensModule, StripeModule],
imports: [PrismaModule, TokensModule, StripeModule, ConfigModule],
controllers: [WebhookController],
providers: [WebhooksService],
exports: [WebhooksService],
Expand Down
9 changes: 7 additions & 2 deletions src/modules/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
webhooks,
webhooksCreateInput,
Expand All @@ -27,7 +28,10 @@ export class WebhooksService {
private readonly logger = new Logger(WebhooksService.name);
private queue = new PQueue({ concurrency: 1 });

constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private configService: ConfigService,
) {}

async createWebhook(
groupId: number,
Expand Down Expand Up @@ -156,7 +160,8 @@ export class WebhooksService {
this.queue
.add(() =>
pRetry(() => this.callWebhook(webhook, event), {
retries: 3,
retries:
this.configService.get<number>('webhooks.retries') ?? 3,
onFailedAttempt: (error) => {
this.logger.error(
`Triggering webhoook failed, retrying (${error.retriesLeft} attempts left)`,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mail/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MailService {
options.from ?? `"${this.config.name}" <${this.config.from}>`,
}),
{
retries: 3,
retries: this.configService.get<number>('email.retries') ?? 3,
onFailedAttempt: (error) => {
this.logger.error(
`Mail to ${options.to} failed, retrying (${error.retriesLeft} attempts left)`,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/twilio/twilio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TwilioService {
this.queue
.add(() =>
pRetry(() => this.sendSms(options), {
retries: 3,
retries: this.configService.get<number>('sms.retries') ?? 3,
onFailedAttempt: (error) => {
this.logger.error(
`SMS to ${options.to} failed, retrying (${error.retriesLeft} attempts left)`,
Expand Down

0 comments on commit 8e89692

Please sign in to comment.