Mensajes de test de rabbit

This commit is contained in:
2026-01-08 17:10:09 +01:00
parent 68631ff18f
commit 8a1368d42a
4 changed files with 86 additions and 27 deletions

29
.env
View File

@@ -1,9 +1,22 @@
POSTGRES_PASSWORD='1234'
POSTGRES_USER=postgres
POSTGRES_DB=postgres
POSTGRES_PORT='5432'
DEV_POSTGRES_PORT='5432'
PORT=3000
RABBITMQ_USER='guest'
RABBITMQ_PASSWORD='guest'
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
ENVIORMENT=development
RABBITMQ_HOST=rabbitmq-sim-broker
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_SECURE=false
RABBITMQ_VHOST=sim-vhost
# Hay cosas que unificar de varios servicios
POSTGRES_DB=postgres
POSTGRES_DATABASE=postres
POSTGRES_HOST=postgresql-sim-1
POSTGRES_PORT=5432
DEV_POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=1234

View File

@@ -1,10 +1,12 @@
name: sim-eventos
networks:
default:
driver: bridge
name: network-test # Tiene que coincidir con el compose objetivo
services:
rabbitmq:
container_name: rabbitmq-broker
rabbitmq-sim-broker:
container_name: rabbitmq-sim-broker
image: "rabbitmq:4.2.2-management"
ports:
- "5672:5672"
@@ -14,8 +16,8 @@ services:
restart: unless-stopped
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "check_port_connectivity"]
interval: 30s
timeout: 10s
interval: 10s
timeout: 5s
retries: 5
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER}
@@ -37,7 +39,7 @@ services:
PORT: "${PORT:-3000}"
develop:
watch:
- path: ./src
- path: ./packages
action: sync
target: /usr/local/app/src
- path: ./package.json
@@ -47,6 +49,9 @@ services:
env_file:
- .env
restart: unless-stopped
depends_on:
rabbitmq-sim-broker:
condition: service_healthy
postgresql-sim:
image: postgres:16.1
@@ -59,7 +64,7 @@ services:
- ./deployment/database/test.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
interval: 5s
retries: 5
start_period: 30s
timeout: 10s
start_period: 5s
timeout: 5s

View File

@@ -14,4 +14,5 @@ export const env = {
RABBITMQ_TTL: process.env.RABBITMQ_TTL,
RABBITMQ_SECURE: process.env.RABBITMQ_SECURE,
RABBITMQ_RETRY_INTERVAL: process.env.RABBITMQ_INTERVAL,
RABBITMQ_VHOST: process.env.RABBITMQ_VHOST,
};

View File

@@ -1,43 +1,83 @@
import client, { ChannelModel, connect, Connection } from "amqplib"
import client, { ChannelModel, ConfirmChannel, connect, Connection } from "amqplib"
import { env } from "config/env"
import { Channel } from "node:diagnostics_channel"
const rmqUser = env.RABBITMQ_USER
const rmqPass = env.RABBITMQ_PASSWORD
const rmqHost = env.RABBITMQ_HOST
const rmqPort = Number(env.RABBITMQ_PORT)
const rmqSecure = env.RABBITMQ_SECURE
const rmqSecure = false
const rmqVhost = env.RABBITMQ_VHOST
const PREFETCH_LIMIT = 1
class RabbitConnection {
connection?: Connection
channel?: Channel
connection?: ChannelModel
channel?: ConfirmChannel
connected: Boolean = false
public async connect() {
this.connection = await this.createConnection();
if (this.connection == undefined) throw new Error("[RMQ] Error crecreando la conexion")
this.channel = await this.createConfirmChannel()
}
protected async createConnection(): Promise<ChannelModel> {
protected async createConnection() {
const { hostname, port, secure } = { hostname: rmqHost, port: rmqPort, secure: rmqSecure }
const { username, password } = { username: rmqUser, password: rmqPass };
const protocol = secure ? 'amqps' : 'amqp';
const vhost = rmqVhost
const connection = await connect({
const connection = await client.connect({
protocol,
hostname,
port,
username,
password
password,
vhost
});
connection.on('error', (error: unknown) => {
console.error(`[EVENT BUS] Rabbitmq connection error :: ${error}`);
console.error(`[RMQ] Rabbitmq connection error :: ${error}`);
Promise.reject(error);
});
return connection;
}
protected async createConfirmChannel() {
const channel = await this.connection?.createConfirmChannel()
await channel?.prefetch(PREFETCH_LIMIT)
if (channel == undefined) throw new Error("[RMQ] Error crecreando el canal")
channel.on('error', (error: unknown) => {
console.error(`[RMQ] Rabbitmq channel error :: ${error}`);
Promise.reject(error);
});
return channel;
}
}
while (true) {
console.log("test")
async function test() {
const rbmq = new RabbitConnection()
await rbmq.connect()
console.log("enviando")
rbmq.channel?.sendToQueue("sim.queue", Buffer.from("test"), {},
function (err, ok) {
if (err !== null)
console.warn('Message nacked!');
else
console.log('Message acked');
}
)
}
test()
export default {}