Skip to content

Commit 5c58245

Browse files
authored
feat: allow archiveFailedInDays and deleteArchivedAfterDays to be configurable (#8)
1 parent 6df04dc commit 5c58245

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

worker/config/custom-environment-variables.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"Queue": {
3-
"url": "QUEUE_URL"
3+
"url": "QUEUE_URL",
4+
"archiveFailedAfterDays": "ARCHIVE_FAILED_AFTER_DAYS",
5+
"deleteArchivedAfterDays": "DELETE_ARCHIVED_IN_DAYS"
46
},
57
"Submission": {
68
"requestTimeout": "SUBMISSION_REQUEST_TIMEOUT"

worker/config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = {
22
Queue: {
33
url: "postgres://user:root@localhost:5432/queue",
4+
archiveFailedInDays: 30,
5+
deleteArchivedAfterDays: 7,
46
},
57
Submission: {
68
requestTimeout: 2000,

worker/config/production.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"env": "test"
3+
}

worker/src/Consumer/getConsumer.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ import PgBoss from "pg-boss";
33
import { ApplicationError } from "../utils/ApplicationError";
44
import pino from "pino";
55

6-
const DEFAULT_URL = config.get<string>("Queue.url");
6+
const URL = config.get<string>("Queue.url");
77
const logger = pino();
8-
98
let 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

Comments
 (0)