@@ -3,14 +3,25 @@ import PgBoss from "pg-boss";
33import { ApplicationError } from "../utils/ApplicationError" ;
44import pino from "pino" ;
55
6- const DEFAULT_URL = config . get < string > ( "Queue.url" ) ;
6+ const URL = config . get < string > ( "Queue.url" ) ;
77const logger = pino ( ) ;
8-
98let consumer ;
109
11- export async function create ( url : string = DEFAULT_URL ) {
12- logger . info ( { method : "Consumer.create" } , `Starting consumer at ${ url } ` ) ;
13- const boss = new PgBoss ( url ) ;
10+ const MINUTE_IN_S = 60 ;
11+ const HOUR_IN_S = MINUTE_IN_S * 60 ;
12+ const DAY_IN_S = HOUR_IN_S * 24 ;
13+
14+ const archiveFailedAfterDays = parseInt ( config . get < string > ( "Queue.archiveFailedInDays" ) ) ;
15+ const deleteAfterDays = parseInt ( config . get < string > ( "Queue.deleteArchivedAfterDays" ) ) ;
16+
17+ logger . info ( { method : "Consumer.create" } , `archiveFailedAfterDays: ${ archiveFailedAfterDays } , deleteAfterDays: ${ deleteAfterDays } ` ) ;
18+
19+ export async function create ( ) {
20+ const boss = new PgBoss ( {
21+ connectionString : URL ,
22+ archiveFailedAfterSeconds : archiveFailedAfterDays * DAY_IN_S ,
23+ deleteAfterDays,
24+ } ) ;
1425
1526 boss . on ( "error" , ( error ) => {
1627 throw error ;
@@ -22,7 +33,7 @@ export async function create(url: string = DEFAULT_URL) {
2233 throw new ApplicationError ( "CONSUMER" , "START_FAILED" , `Failed to start listener ${ e . message } . Exiting` ) ;
2334 }
2435
25- logger . info ( { method : "Consumer.create" } , `Successfully started consumer at ${ url } ` ) ;
36+ logger . info ( { method : "Consumer.create" } , `Successfully started consumer at ${ URL } ` ) ;
2637 return boss ;
2738}
2839
0 commit comments