Skip to content

Commit

Permalink
feat(): added verbose retry log option
Browse files Browse the repository at this point in the history
  • Loading branch information
ISlavinskyi committed May 25, 2020
1 parent 6900241 commit b02e7f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/common/typeorm.utils.ts
Expand Up @@ -110,17 +110,21 @@ export function getEntityManagerToken(
export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
verboseRetryLog = false,
): <T>(source: Observable<T>) => Observable<T> {
return <T>(source: Observable<T>) =>
source.pipe(
retryWhen(e =>
retryWhen((e) =>
e.pipe(
scan((errorCount, error: Error) => {
logger.error(
`Unable to connect to the database. Message: ${error.message}. Retrying (${errorCount +
1})...`,
error.stack,
);
const message = verboseRetryLog
? `Unable to connect to the database. Message: ${
error.message
}. Retrying (${errorCount + 1})...`
: `Unable to connect to the database. Retrying (${
errorCount + 1
})...`;
logger.error(message, error.stack);
if (errorCount + 1 >= retryAttempts) {
throw error;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/interfaces/typeorm-options.interface.ts
Expand Up @@ -21,6 +21,10 @@ export type TypeOrmModuleOptions = {
* If `true`, connection will not be closed on application shutdown.
*/
keepConnectionAlive?: boolean;
/**
* If `true`, will show verbose error messages on each connection retry.
*/
verboseRetryLog?: boolean;
} & Partial<ConnectionOptions>;

export interface TypeOrmOptionsFactory {
Expand Down
8 changes: 7 additions & 1 deletion lib/typeorm-core.module.ts
Expand Up @@ -201,7 +201,13 @@ export class TypeOrmCoreModule implements OnApplicationShutdown {
entities,
} as ConnectionOptions);
})
.pipe(handleRetry(options.retryAttempts, options.retryDelay))
.pipe(
handleRetry(
options.retryAttempts,
options.retryDelay,
options.verboseRetryLog,
),
)
.toPromise();
}
}

0 comments on commit b02e7f9

Please sign in to comment.