Skip to content

Commit

Permalink
feat: retry and retry delay
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed May 8, 2024
1 parent 1f01261 commit ebba663
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/nest-minio.options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ClientOptions } from 'minio';

export type NestMinioOptions = ClientOptions;
export type NestMinioOptions = ClientOptions & {
retries?: number
retryDelay?: number
};
27 changes: 17 additions & 10 deletions src/nest-minio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
import * as minio from 'minio';
import { MODULE_OPTIONS_TOKEN } from './nest-minio.module-definition';
import { NestMinioOptions } from './nest-minio.options';
import { from, lastValueFrom, retry } from "rxjs"

interface INestMinioService {
getMinio(): minio.Client
Expand All @@ -14,22 +15,28 @@ export class NestMinioService implements INestMinioService {
private readonly logger = new Logger(NestMinioService.name);

constructor(
@Inject(MODULE_OPTIONS_TOKEN) private _NestMinioOptions: NestMinioOptions,
) {}
@Inject(MODULE_OPTIONS_TOKEN) private nestMinioOptions: NestMinioOptions,
) { }

getMinio(): minio.Client {
if (!this._minioConnection) {
this._minioConnection = new minio.Client(this._NestMinioOptions);
const { retries, ...options } = this.nestMinioOptions;
this._minioConnection = new minio.Client(options);
}
return this._minioConnection;
}

checkConnection(){
this._minioConnection.listBuckets().then(()=>{
this.logger.log("Successfullly connected to minio.")
})
.catch(error =>{
this.logger.error(error)
})
checkConnection() {
const { retries = 5, retryDelay = 1000 } = this.nestMinioOptions

lastValueFrom(from(this._minioConnection.listBuckets()).pipe(
retry({ count: retries, delay: retryDelay })
))
.then(() => {
this.logger.log("Successfully connected to minio.")
})
.catch(error => {
this.logger.error(error)
})
}
}

0 comments on commit ebba663

Please sign in to comment.