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
6 changed files
with
225 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const EventEmitter = require('events'); | ||
const amqp = require('amqplib'); | ||
|
||
const RABBITMQ_HOST = process.env.RABBITMQ_HOST || 'localhost'; | ||
const RABBITMQ_PORT = process.env.RABBITMQ_PORT || 5672; | ||
const RABBITMQ_USER = process.env.RABBITMQ_USER || 'devuser'; | ||
const RABBITMQ_PASS = process.env.RABBITMQ_PASS || 'devpass'; | ||
const RABBITMQ_VHOST = process.env.RABBITMQ_VHOST || '/'; | ||
|
||
/** | ||
* A BrokkerConnector for RabbitMQ | ||
* | ||
* {@see BrokerConnector} | ||
*/ | ||
class RabbitConnector extends EventEmitter { | ||
constructor() { | ||
super(); | ||
this.conn = null; | ||
this.onClose = this.onClose.bind(this); | ||
this.onError = this.onError.bind(this); | ||
} | ||
|
||
get connection() { | ||
return this.conn ? Promise.resolve(this.conn) : this.connect(); | ||
} | ||
|
||
async connect() { | ||
const conn = await amqp.connect({ | ||
protocol: 'amqp', | ||
hostname: RABBITMQ_HOST, | ||
port: Number(RABBITMQ_PORT), | ||
username: RABBITMQ_USER, | ||
password: RABBITMQ_PASS, | ||
vhost: RABBITMQ_VHOST | ||
}); | ||
|
||
conn.on('error', this.onError); | ||
conn.on('close', this.onClose); | ||
|
||
this.conn = conn; | ||
this.emit('connect', conn); | ||
|
||
return conn; | ||
} | ||
|
||
disconnect() { | ||
return new Promise(resolve => { | ||
if (!this.conn) { | ||
resolve(); | ||
} | ||
this.conn.off('close', this.onClose); | ||
this.conn.close().then(() => { | ||
this.emit('close', this.conn); | ||
this.conn = null; | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
reconnect() { | ||
this.emit('reconnect'); | ||
setTimeout(() => this.connect(), 500); | ||
} | ||
|
||
onClose() { | ||
this.emit('close', this.conn); | ||
this.conn = null; | ||
this.reconnect(); | ||
} | ||
|
||
onError(err) { | ||
this.emit('error', err); | ||
this.conn = null; | ||
if (err.message !== 'Connection closing') { | ||
this.reconnect(); | ||
} | ||
} | ||
} | ||
|
||
module.exports = new RabbitConnector(); | ||
|
||
/** | ||
* A BrokerConnector provides a persistent connection | ||
* to a message broker with reconnect on failure guarantees. | ||
* | ||
* @typedef {Object} BrokerConnector | ||
* @property {Promise<object>} connection - the connection as an object, will connect first if not already connected | ||
* @property {function():Promise<object>} connect - create a new connection | ||
*/ |
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,57 @@ | ||
/* eslint-disable global-require */ | ||
import logger from 'logger'; | ||
import CreateOrderCommand from 'domain/orders/create/command'; | ||
|
||
const inputQueue = 'hexagonal.input'; | ||
|
||
class Broker { | ||
constructor(rabbit, commandBus) { | ||
this.rabbit = rabbit; | ||
this.commandBus = commandBus; | ||
} | ||
|
||
async start() { | ||
this.conn = await this.rabbit.connection; | ||
this.channel = await this.conn.createChannel(); | ||
this.channel.assertQueue(inputQueue, {durable: true}); | ||
this.channel.consume(inputQueue, this.consumeMessage.bind(this)); | ||
} | ||
|
||
async consumeMessage(msg) { | ||
let response; | ||
let command; | ||
|
||
try { | ||
const payload = JSON.parse(msg.content.toString()); | ||
const {commandType, commandBody} = payload; | ||
switch (commandType) { | ||
case 'CreateOrder': | ||
command = CreateOrderCommand.buildFromJSON(commandBody); | ||
break; | ||
default: | ||
throw new Error('Unknown commandType'); | ||
} | ||
response = this.commandBus.execute(command); | ||
} catch (error) { | ||
logger.error(error.message); | ||
response = error.message; | ||
this.channel.nack(msg); | ||
return; | ||
} | ||
|
||
if (msg.properties.replyTo) { | ||
const replyOpts = { | ||
correlationId: msg.properties.correlationId | ||
}; | ||
this.channel.sendToQueue( | ||
msg.properties.replyTo, | ||
Buffer.from(JSON.stringify(response)), | ||
replyOpts | ||
); | ||
} | ||
|
||
this.channel.ack(msg); | ||
} | ||
} | ||
|
||
module.exports = Broker; |
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