Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementando o Report #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions services/report/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RABBITMQ_LOGIN = guest
RABBITMQ_PASSWORD = guest
RABBITMQ_HOST = q-rabbitmq
RABBITMQ_PORT = 5672
RABBITMQ_VHOST =
RABBITMQ_QUEUE_NAME = report
15 changes: 10 additions & 5 deletions services/report/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ async function updateReport(products) {

}

async function printReport() {
for (const [key, value] of Object.entries(report)) {
console.log(`${key} = ${value} vendas`);
}
async function processMessage(msg) {
const reportData = JSON.parse(msg.content)
updateReport(reportData.products)
printReport()
}

async function consume() {
//TODO: Constuir a comunicação com a fila
console.log(
`✔ INSCRITO COM SUCESSO NA FILA: ${process.env.RABBITMQ_QUEUE_NAME}`
);

await (await RabbitMQService.getInstance()).consume(process.env.RABBITMQ_QUEUE_NAME, (msg) => {processMessage(msg);});

}

consume()
17 changes: 9 additions & 8 deletions services/report/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "report",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
"description": "Serviço responsável por reportar shipping",
"main": "app.js",
"license": "ISC",
"dependencies": {
"amqplib": "^0.7.1",
"dotenv": "^8.2.0",
"nodemailer": "^6.5.0"
}
}
55 changes: 55 additions & 0 deletions services/report/rabbitmq-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const amqlib = require('amqplib')

class RabbitMQ {
constructor() {
this.url = `amqp://${process.env.RABBITMQ_LOGIN}:${process.env.RABBITMQ_PASSWORD}@${process.env.RABBITMQ_HOST}:${process.env.RABBITMQ_PORT}/${process.env.RABBITMQ_VHOST}`
this.connection = null
this.channel = null
}

async connect() {
if (!this.connection) this.connection = await amqlib.connect(this.url)
if (!this.channel) this.channel = await this.connection.createChannel()

this.channel.prefetch(1)
}

async send(queue, message) {
try {
if (this.channel) {
this.channel.assertQueue(queue, { durable: true, queueMode: 'lazy' })
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message), 'utf-8'), { persistent: true })
}
}
catch (error) {
console.log(error.message)
}
}

async consume(queue, callback) {
try {
if (this.channel) {
this.channel.assertQueue(queue, { durable: true, queueMode: 'lazy' })
this.channel.consume(queue, callback, { noAck: true })
}
}
catch (error) {
console.log(error.message)
}
}
}

class RabbitMQService {

static async getInstance() {
if (!RabbitMQService.instance) {
let instance = new RabbitMQ()
await instance.connect()
RabbitMQService.instance = instance
}
return RabbitMQService.instance
}

}

module.exports = RabbitMQService