Skip to content

Commit

Permalink
fix: pg-boss startup errors hidden issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samaratungajs committed Nov 4, 2024
1 parent 2d25724 commit 3caeddf
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/utils/handleRetry.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import { of, throwError } from "rxjs";
import { mergeMap, retryWhen, take, delay } from "rxjs/operators";
import { mergeMap, retryWhen, delay } from "rxjs/operators";
import { LOGGER } from "./consts";
import { Logger } from "@nestjs/common";

export function handleRetry(
retryAttempts = 9,
retryDelay = 3000,
verbose = false,
toRetry: (err: any) => boolean = (_err: any) => true,
) {
const logger = new Logger(LOGGER);

return <T>(source: import("rxjs").Observable<T>) =>
source.pipe(
retryWhen((attempts) =>
attempts.pipe(
take(retryAttempts),
mergeMap((error, index) => {
const includeError = toRetry(error);
if (verbose && includeError) {
console.warn(
`Attempt ${index + 1}: Retrying in ${
retryDelay / 1000
} seconds...`,
);

if (includeError) {
if (verbose) {
logger.warn(
`Attempt ${index + 1}: Retrying in ${
retryDelay / 1000
} seconds...`,
);
}

if (index + 1 >= retryAttempts) {
return throwError(() => new Error(error.message));
}

return of(error).pipe(delay(retryDelay));
}
return includeError ? of(error) : throwError(error);

return throwError(() => error);
}),
delay(retryDelay),
),
),
);
Expand Down

0 comments on commit 3caeddf

Please sign in to comment.