-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
147 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FastifyBaseLogger } from "fastify"; | ||
import Pino, { LoggerOptions } from "pino"; | ||
import { env } from "../../core/env"; | ||
|
||
const defaultConfig: LoggerOptions = { | ||
redact: ["headers.authorization"], | ||
transport: { | ||
target: "pino-pretty", | ||
options: { | ||
translateTime: "HH:MM:ss Z", | ||
ignore: "pid,hostname,reqId", | ||
singleLine: true, | ||
}, | ||
}, | ||
level: | ||
env.NODE_ENV === "production" | ||
? "info" | ||
: env.NODE_ENV === "development" | ||
? "debug" | ||
: env.NODE_ENV === "testing" | ||
? "debug" | ||
: "trace", | ||
}; | ||
|
||
interface Logger { | ||
server: FastifyBaseLogger; | ||
worker: FastifyBaseLogger; | ||
} | ||
|
||
const createLogger = (prefix: string) => { | ||
return Pino({ ...defaultConfig, msgPrefix: prefix }); | ||
}; | ||
|
||
export const logger: Logger = { | ||
server: createLogger("[Server] "), | ||
worker: createLogger("[Worker] "), | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,49 @@ | ||
import { FastifyInstance } from "fastify"; | ||
import { connectToDatabase } from "../../core"; | ||
import { logger } from "../../src/utils/logger"; | ||
import { queue } from "../services/pQueue"; | ||
import { processTransaction } from "./processTransaction"; | ||
|
||
const beginTransactionProcessing = (server: FastifyInstance) => { | ||
const beginTransactionProcessing = () => { | ||
return async () => { | ||
server.log.info(`--- processing Q request started at ${new Date()} ---`); | ||
await processTransaction(server); | ||
server.log.info(`--- processing Q request ended at ${new Date()} ---`); | ||
logger.worker.info(`--- processing Q request started at ${new Date()} ---`); | ||
await processTransaction(); | ||
logger.worker.info(`--- processing Q request ended at ${new Date()} ---`); | ||
}; | ||
}; | ||
|
||
export const startNotificationListener = async ( | ||
server: FastifyInstance, | ||
): Promise<void> => { | ||
export const startNotificationListener = async (): Promise<void> => { | ||
try { | ||
server.log.info(`Starting notification listener`); | ||
logger.worker.info(`Starting notification listener`); | ||
// Acquire a connection | ||
const knex = await connectToDatabase(); | ||
const connection = await knex.client.acquireConnection(); | ||
connection.query("LISTEN new_transaction_data"); | ||
|
||
// Adding to Queue to Process Requests | ||
queue.add(beginTransactionProcessing(server)); | ||
queue.add(beginTransactionProcessing()); | ||
|
||
connection.on( | ||
"notification", | ||
async (msg: { channel: string; payload: string }) => { | ||
queue.add(beginTransactionProcessing(server)); | ||
queue.add(beginTransactionProcessing()); | ||
}, | ||
); | ||
|
||
connection.on("end", async () => { | ||
server.log.info(`Connection database ended`); | ||
logger.worker.info(`Connection database ended`); | ||
await knex.destroy(); | ||
knex.client.releaseConnection(connection); | ||
server.log.info(`Released sql connection : on end`); | ||
logger.worker.info(`Released sql connection : on end`); | ||
}); | ||
|
||
connection.on("error", async (err: any) => { | ||
server.log.error(err); | ||
logger.worker.error(err); | ||
knex.client.releaseConnection(connection); | ||
await knex.destroy(); | ||
server.log.info(`Released sql connection: on error`); | ||
logger.worker.info(`Released sql connection: on error`); | ||
}); | ||
} catch (error) { | ||
server.log.error(`Error in notification listener: ${error}`); | ||
logger.worker.error(`Error in notification listener: ${error}`); | ||
throw error; | ||
} | ||
}; |
Oops, something went wrong.