Skip to content

Commit

Permalink
fix: tidying loggers (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit authored Jan 29, 2024
1 parent 5c58245 commit cbb3aa9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions worker/src/Consumer/getConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApplicationError } from "../utils/ApplicationError";
import pino from "pino";

const URL = config.get<string>("Queue.url");
const logger = pino();
const logger = pino().child({ method: "Consumer.create" });
let consumer;

const MINUTE_IN_S = 60;
Expand All @@ -14,7 +14,7 @@ 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}`);
logger.info(`archiveFailedAfterDays: ${archiveFailedAfterDays}, deleteAfterDays: ${deleteAfterDays}`);

export async function create() {
const boss = new PgBoss({
Expand All @@ -33,7 +33,7 @@ export async function create() {
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(`Successfully started consumer at ${URL}`);
return boss;
}

Expand Down
9 changes: 5 additions & 4 deletions worker/src/queues/submission/workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as submit from "./submit";
import pino from "pino";
import PgBoss from "pg-boss";

const logger = pino();
const queue = "submission";

const logger = pino().child({
queue,
});
export async function setupSubmissionWorkers() {
const consumer: PgBoss = await getConsumer();

logger.info({ queue }, `starting queue '${queue}' workers`);
logger.info(`starting queue '${queue}' workers`);

logger.info({ queue }, `starting 'submitHandler' on ${queue} listeners`);
logger.info(`starting 'submitHandler' listener`);
await consumer.work("submission", { newJobCheckInterval: 500 }, submit.submitHandler);
}
24 changes: 13 additions & 11 deletions worker/src/queues/submission/workers/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ const worker = "submit";

const ERROR_CODE = "SUBMIT_ERROR";

const logger = pino();
export const metadata = { queue, worker };
const logger = pino().child({
queue,
worker,
});

const REQUEST_TIMEOUT = Number.parseInt(config.get<string>("Submission.requestTimeout"));
logger.info(metadata, `REQUEST_TIMEOUT set to ${REQUEST_TIMEOUT}`);
logger.info(`REQUEST_TIMEOUT set to ${REQUEST_TIMEOUT}`);

/**
* When a "submission" event is detected, this worker POSTs the data to `job.data.data.webhook_url`
* The source of this event is the runner, after a user has submitted a form.
*/
export async function submitHandler(job: Job<SubmitJob>) {
const jobLogData = { jobId: job.id, ...metadata };
logger.info(jobLogData, `received ${worker} job`);
const jobId = job.id;
logger.info({ jobId }, `received ${worker} job`);

const { data, id } = job;
const requestBody = data.data;
Expand All @@ -31,20 +34,19 @@ export async function submitHandler(job: Job<SubmitJob>) {
timeout: REQUEST_TIMEOUT,
});
// @ts-ignore
logger.info(jobLogData, `${url} took ${res.config?.meta?.responseTime}ms`);
logger.info(jobLogData, `${url} responded with ${res.status} - ${JSON.stringify(res.data)}`);
logger.info({ jobId, status: res.status, url, data: res.data }, `${url} took ${res.config?.meta?.responseTime}ms`);

const reference = res.data.reference;
if (reference) {
logger.info(jobLogData, `job: ${id} posted successfully to ${url} and responded with reference: ${reference}`);
logger.info({ jobId }, `job: ${id} posted successfully to ${url} and responded with reference: ${reference}`);
return { reference };
}
return;
} catch (e: any) {
logger.error(jobLogData, `${ERROR_CODE} to ${url} job: ${id} failed with ${e.cause ?? e.message}`);
logger.error({ jobId, err: e, errorCode: ERROR_CODE }, `post to ${url} job: ${id} failed with ${e.cause ?? e.message}`);

if (e.response) {
logger.error(jobLogData, `${ERROR_CODE} ${JSON.stringify(e.response.data)}`);
logger.error({ jobId, err: e.response.error });
const { message, name, code, response } = e;
const { status, data } = response;
throw {
Expand All @@ -57,7 +59,7 @@ export async function submitHandler(job: Job<SubmitJob>) {
}

if (e.request) {
logger.error(jobLogData, `${ERROR_CODE} to ${url} request could not be sent, see database for error`);
logger.error(jobId, `post to ${url} request could not be sent, see database for error`);
}

// @ts-ignore
Expand Down

0 comments on commit cbb3aa9

Please sign in to comment.