-
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.
feat: add errorLogsService to error-handle middleware to send errors …
…to NOC app
- Loading branch information
Showing
7 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
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
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,53 @@ | ||
import { Channel } from 'amqplib'; | ||
import amqp, { ChannelWrapper } from 'amqp-connection-manager'; | ||
|
||
/** | ||
* ProducerService class for sending messages to RabbitMQ queues. | ||
*/ | ||
export class QueueService { | ||
private channelWrapper: ChannelWrapper; | ||
|
||
/** | ||
* Constructs an instance of ProducerService. | ||
* @param rabbitmqUrl - URL of the RabbitMQ server. | ||
* @param exchange - Name of the exchange to publish messages. | ||
*/ | ||
constructor( | ||
private readonly rabbitmqUrl: string, | ||
private readonly exchange: string | ||
) { | ||
// Establishes connection to RabbitMQ server and creates a channel wrapper. | ||
const connection = amqp.connect(this.rabbitmqUrl); | ||
this.channelWrapper = connection.createChannel({ | ||
// Ensures the exchange is declared upon channel creation. | ||
setup: (channel: Channel) => { | ||
return channel.assertExchange(exchange, 'direct', { | ||
durable: true, | ||
}); | ||
}, | ||
}); | ||
console.log(`${this.exchange} exchange created`); | ||
} | ||
|
||
/** | ||
* Adds a message to the specified queue in the exchange. | ||
* @param payload - Data to be sent in the message. | ||
* @param queue - Name of the queue to send the message. | ||
*/ | ||
async addMessageToQueue(payload: any, queue: string) { | ||
try { | ||
console.log({ payload, queue }); | ||
// Publishes the message to the specified queue in the exchange. | ||
await this.channelWrapper.publish( | ||
this.exchange, | ||
queue, | ||
Buffer.from(JSON.stringify(payload)), | ||
{ persistent: true } | ||
); | ||
|
||
console.log('Message sent to queue'); | ||
} catch (error) { | ||
console.log('Error sending message to queue', error); | ||
} | ||
} | ||
} |