-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Praashh/feat-event
[feat]: pub sub integration using websocket and redis, refactored queue and worker using redis package, removed bullmq
- Loading branch information
Showing
22 changed files
with
1,056 additions
and
579 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { RedisClientType, createClient } from "redis"; | ||
import { DbMessage, MessageToApi, WsMessage } from "@opinix/types"; | ||
|
||
export class SubscriberManager { | ||
private client: RedisClientType; | ||
private static instance: SubscriberManager; | ||
|
||
constructor() { | ||
this.client = createClient(); | ||
this.client.connect(); | ||
} | ||
|
||
public static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new SubscriberManager(); | ||
} | ||
return this.instance; | ||
} | ||
|
||
public subscribeToChannel( | ||
channel: string, | ||
callback: (event: string, message: string) => void | ||
) { | ||
this.client.subscribe(channel, (message: string, channel: string) => { | ||
callback(channel, message); | ||
}); | ||
} | ||
|
||
public unsubscribeFromChannel(channel: string) { | ||
this.client.unsubscribe(channel); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { addToOrderQueue } from "./queues/orderQueue"; | ||
import { createClient } from "redis"; | ||
import orderWorker from "./queues/orderProcessor"; | ||
import { processOrderQueue } from "./queues/orderQueue"; | ||
import { logger } from "@opinix/logger"; | ||
import { RedisManager } from "./classes/RedisManager"; | ||
import { SubscriberManager } from "./classes/SubscriberManager"; | ||
const startWorker = async () => { | ||
logger.info("WORKER | Starting order worker"); | ||
orderWorker; | ||
processOrderQueue; | ||
}; | ||
|
||
startWorker(); | ||
export { addToOrderQueue, RedisManager, createClient }; | ||
export { addToOrderQueue, RedisManager, createClient, SubscriberManager }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
import { Queue } from "bullmq"; | ||
import getRedisClient from "../config/redisClient"; | ||
let redisClient = getRedisClient(); | ||
export const orderQueue = new Queue("orderQueue", { | ||
connection: redisClient, | ||
}); | ||
import { logger } from "@opinix/logger"; | ||
import { RedisClientType, createClient } from "redis"; | ||
import { RedisManager } from "../classes/RedisManager"; | ||
|
||
let redisClient = RedisManager.getInstance().getClient(); | ||
|
||
const QUEUE_NAME = "ORDER_QUEUE"; | ||
|
||
export const addToOrderQueue = async (order: object) => { | ||
await orderQueue.add("order", order, { | ||
attempts: 3, | ||
backoff: { | ||
type: "exponential", | ||
delay: 5000, | ||
}, | ||
}); | ||
try { | ||
await redisClient.lPush(QUEUE_NAME, JSON.stringify(order)); | ||
logger.info(`Order added to queue: ${JSON.stringify(order)}`); | ||
} catch (err) { | ||
if (err instanceof Error) | ||
logger.error(`Error adding order to queue: ${err.message}`); | ||
} | ||
}; | ||
|
||
export const processOrderQueue = async () => { | ||
while (true) { | ||
try { | ||
const order = await redisClient.lPop(QUEUE_NAME); | ||
if (order) { | ||
logger.info(`Processing order: ${JSON.stringify(order)}`); | ||
} | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
logger.error(`Error processing order: ${err.message}`); | ||
} | ||
} | ||
} | ||
}; |
Oops, something went wrong.