Skip to content

Commit

Permalink
fix: better logs (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenbutongit authored Jan 24, 2024
1 parent 90cac1d commit 6df04dc
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions worker/src/queues/submission/workers/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,13 @@ import config from "config";
const queue = "submission";
const worker = "submit";

const ERROR_CODE = "SUBMIT_ERROR";

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

axios.interceptors.request.use((intercepted) => {
// @ts-ignore
intercepted.meta = intercepted.meta ?? {};
// @ts-ignore
intercepted.meta.requestStartedAt = new Date().getTime();
return intercepted;
});

axios.interceptors.response.use((intercepted) => {
// @ts-ignore
intercepted.config.meta.requestFinishedAt = new Date().getTime();
// @ts-ignore
intercepted.config.meta.responseTime = intercepted.config.meta.requestFinishedAt - intercepted.config.meta.requestStartedAt;
return intercepted;
});

/**
* 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.
Expand All @@ -39,6 +25,7 @@ export async function submitHandler(job: Job<SubmitJob>) {
const { data, id } = job;
const requestBody = data.data;
const url = data.webhook_url;

try {
const res = await axios.post(url, requestBody, {
timeout: REQUEST_TIMEOUT,
Expand All @@ -54,11 +41,45 @@ export async function submitHandler(job: Job<SubmitJob>) {
}
return;
} catch (e: any) {
logger.error(jobLogData, `job: ${id} failed with ${e.cause ?? e.message}`);
logger.error(jobLogData, `${ERROR_CODE} to ${url} job: ${id} failed with ${e.cause ?? e.message}`);

if (e.response) {
logger.error(jobLogData, `${ERROR_CODE} ${JSON.stringify(e.response.data)}`);
const { message, name, code, response } = e;
const { status, data } = response;
throw {
message,
name,
code,
status,
data,
};
}

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

// @ts-ignore
if (e.cause instanceof AggregateError) {
throw { errors: e.cause.errors };
}
throw e;
}
}

axios.interceptors.request.use((intercepted) => {
// @ts-ignore
intercepted.meta = intercepted.meta ?? {};
// @ts-ignore
intercepted.meta.requestStartedAt = new Date().getTime();
return intercepted;
});

axios.interceptors.response.use((intercepted) => {
// @ts-ignore
intercepted.config.meta.requestFinishedAt = new Date().getTime();
// @ts-ignore
intercepted.config.meta.responseTime = intercepted.config.meta.requestFinishedAt - intercepted.config.meta.requestStartedAt;
return intercepted;
});

0 comments on commit 6df04dc

Please sign in to comment.