From 5c58245a2c5f245581d389044a2905a87f441bde Mon Sep 17 00:00:00 2001 From: Jen Duong Date: Wed, 24 Jan 2024 15:37:46 +0000 Subject: [PATCH] feat: allow archiveFailedInDays and deleteArchivedAfterDays to be configurable (#8) --- .../config/custom-environment-variables.json | 4 +++- worker/config/default.js | 2 ++ worker/config/production.json | 3 +++ worker/src/Consumer/getConsumer.ts | 23 ++++++++++++++----- 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 worker/config/production.json diff --git a/worker/config/custom-environment-variables.json b/worker/config/custom-environment-variables.json index b7b46e2..c3a67da 100644 --- a/worker/config/custom-environment-variables.json +++ b/worker/config/custom-environment-variables.json @@ -1,6 +1,8 @@ { "Queue": { - "url": "QUEUE_URL" + "url": "QUEUE_URL", + "archiveFailedAfterDays": "ARCHIVE_FAILED_AFTER_DAYS", + "deleteArchivedAfterDays": "DELETE_ARCHIVED_IN_DAYS" }, "Submission": { "requestTimeout": "SUBMISSION_REQUEST_TIMEOUT" diff --git a/worker/config/default.js b/worker/config/default.js index 3d2076f..b5b1092 100644 --- a/worker/config/default.js +++ b/worker/config/default.js @@ -1,6 +1,8 @@ module.exports = { Queue: { url: "postgres://user:root@localhost:5432/queue", + archiveFailedInDays: 30, + deleteArchivedAfterDays: 7, }, Submission: { requestTimeout: 2000, diff --git a/worker/config/production.json b/worker/config/production.json new file mode 100644 index 0000000..29b1852 --- /dev/null +++ b/worker/config/production.json @@ -0,0 +1,3 @@ +{ + "env": "test" +} \ No newline at end of file diff --git a/worker/src/Consumer/getConsumer.ts b/worker/src/Consumer/getConsumer.ts index cd5d904..7f29d36 100644 --- a/worker/src/Consumer/getConsumer.ts +++ b/worker/src/Consumer/getConsumer.ts @@ -3,14 +3,25 @@ import PgBoss from "pg-boss"; import { ApplicationError } from "../utils/ApplicationError"; import pino from "pino"; -const DEFAULT_URL = config.get("Queue.url"); +const URL = config.get("Queue.url"); const logger = pino(); - let consumer; -export async function create(url: string = DEFAULT_URL) { - logger.info({ method: "Consumer.create" }, `Starting consumer at ${url}`); - const boss = new PgBoss(url); +const MINUTE_IN_S = 60; +const HOUR_IN_S = MINUTE_IN_S * 60; +const DAY_IN_S = HOUR_IN_S * 24; + +const archiveFailedAfterDays = parseInt(config.get("Queue.archiveFailedInDays")); +const deleteAfterDays = parseInt(config.get("Queue.deleteArchivedAfterDays")); + +logger.info({ method: "Consumer.create" }, `archiveFailedAfterDays: ${archiveFailedAfterDays}, deleteAfterDays: ${deleteAfterDays}`); + +export async function create() { + const boss = new PgBoss({ + connectionString: URL, + archiveFailedAfterSeconds: archiveFailedAfterDays * DAY_IN_S, + deleteAfterDays, + }); boss.on("error", (error) => { throw error; @@ -22,7 +33,7 @@ export async function create(url: string = DEFAULT_URL) { throw new ApplicationError("CONSUMER", "START_FAILED", `Failed to start listener ${e.message}. Exiting`); } - logger.info({ method: "Consumer.create" }, `Successfully started consumer at ${url}`); + logger.info({ method: "Consumer.create" }, `Successfully started consumer at ${URL}`); return boss; }