-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry data store sync on armory boot (#354)
- Loading branch information
Samuel
authored
Jun 21, 2024
1 parent
985bfec
commit f7947f7
Showing
11 changed files
with
138 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 31 additions & 11 deletions
42
apps/policy-engine/src/engine/persistence/repository/http-data-store.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,47 @@ | ||
import { HttpSource } from '@narval/policy-engine-shared' | ||
import { HttpService } from '@nestjs/axios' | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { HttpStatus, Injectable, Logger } from '@nestjs/common' | ||
import axiosRetry from 'axios-retry' | ||
import { catchError, lastValueFrom, map } from 'rxjs' | ||
import { DataStoreException } from '../../core/exception/data-store.exception' | ||
import { DataStoreRepository } from '../../core/repository/data-store.repository' | ||
|
||
const MAX_RETRIES = 3 | ||
|
||
@Injectable() | ||
export class HttpDataStoreRepository implements DataStoreRepository { | ||
private logger = new Logger(HttpDataStoreRepository.name) | ||
|
||
constructor(private httpService: HttpService) {} | ||
|
||
fetch<Data>(source: HttpSource): Promise<Data> { | ||
return lastValueFrom( | ||
this.httpService.get<Data>(source.url, { headers: source.headers }).pipe( | ||
map((response) => response.data), | ||
catchError((error) => { | ||
throw new DataStoreException({ | ||
message: 'Unable to fetch remote data source via HTTP', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { source }, | ||
origin: error | ||
}) | ||
this.httpService | ||
.get<Data>(source.url, { | ||
headers: source.headers, | ||
'axios-retry': { | ||
retries: MAX_RETRIES, | ||
retryDelay: axiosRetry.exponentialDelay, | ||
onRetry: (retryCount) => { | ||
this.logger.log('Retry request to fetch HTTP data source', { | ||
retryCount, | ||
maxRetries: MAX_RETRIES, | ||
url: source.url.split('?')[0] | ||
}) | ||
} | ||
} | ||
}) | ||
) | ||
.pipe( | ||
map((response) => response.data), | ||
catchError((error) => { | ||
throw new DataStoreException({ | ||
message: 'Unable to fetch remote data source via HTTP', | ||
suggestedHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, | ||
context: { source }, | ||
origin: error | ||
}) | ||
}) | ||
) | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './lib/decorator' | ||
export * from './lib/dto' | ||
export * from './lib/middleware' | ||
export * from './lib/module' | ||
export * from './lib/service' | ||
export * from './lib/type' | ||
export * from './lib/util' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/nestjs-shared/src/lib/module/axios-retry.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { HttpModule, HttpService } from '@nestjs/axios' | ||
import { DynamicModule, Global, Module } from '@nestjs/common' | ||
import axios, { AxiosRequestConfig } from 'axios' | ||
import axiosRetry, { DEFAULT_OPTIONS, IAxiosRetryConfig } from 'axios-retry' | ||
|
||
interface AxiosRetryOptions { | ||
axiosConfig?: AxiosRequestConfig | ||
axiosRetryConfig?: IAxiosRetryConfig | ||
} | ||
|
||
/** | ||
* A module that provides retry functionality for Axios HTTP requests. | ||
* This module can be imported in a NestJS application to enable automatic retry of failed requests. | ||
*/ | ||
@Global() | ||
@Module({}) | ||
export class AxiosRetryModule { | ||
/** | ||
* Creates a dynamic module for the AxiosRetryModule. | ||
* @param options - Optional configuration options for the retry behavior. | ||
* @returns A dynamic module that can be imported in a NestJS application. | ||
*/ | ||
static forRoot( | ||
options: AxiosRetryOptions = { | ||
axiosRetryConfig: { | ||
...DEFAULT_OPTIONS, | ||
retries: 0 // Default never retries | ||
} | ||
} | ||
): DynamicModule { | ||
const axiosInstance = axios.create(options.axiosConfig) | ||
axiosRetry(axiosInstance, options.axiosRetryConfig) | ||
|
||
const axiosProvider = { | ||
provide: HttpService, | ||
useValue: new HttpService(axiosInstance) | ||
} | ||
|
||
return { | ||
module: AxiosRetryModule, | ||
imports: [HttpModule], | ||
providers: [axiosProvider], | ||
exports: [axiosProvider] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './axios-retry.module' |