Skip to content

Commit

Permalink
feat: allow archiveFailedInDays and deleteArchivedAfterDays to be con…
Browse files Browse the repository at this point in the history
…figurable (#8)
  • Loading branch information
jenbutongit authored Jan 24, 2024
1 parent 6df04dc commit 5c58245
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion worker/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions worker/config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
Queue: {
url: "postgres://user:root@localhost:5432/queue",
archiveFailedInDays: 30,
deleteArchivedAfterDays: 7,
},
Submission: {
requestTimeout: 2000,
Expand Down
3 changes: 3 additions & 0 deletions worker/config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"env": "test"
}
23 changes: 17 additions & 6 deletions worker/src/Consumer/getConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import PgBoss from "pg-boss";
import { ApplicationError } from "../utils/ApplicationError";
import pino from "pino";

const DEFAULT_URL = config.get<string>("Queue.url");
const URL = config.get<string>("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<string>("Queue.archiveFailedInDays"));
const deleteAfterDays = parseInt(config.get<string>("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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 5c58245

Please sign in to comment.