Inicio port NOS
This commit is contained in:
@@ -1,65 +1,34 @@
|
||||
import { EventBus } from "sim-shared/domain/EventBus.port.js";
|
||||
import { ConsumeMessage } from "amqplib";
|
||||
import { SimNosUsecases } from "./SimNOS.usecases";
|
||||
|
||||
export class SimNosController {
|
||||
private eventBus: EventBus;
|
||||
private activationUseCases: any;
|
||||
|
||||
private routes = new Map<string, () => void>([
|
||||
["activate", async () => { console.log("caso de uso activate") }],
|
||||
["pause", async () => { console.log("caso de uso pause") }],
|
||||
["cancel", async () => { console.log("caso de uso cancel") }],
|
||||
])
|
||||
|
||||
constructor(
|
||||
eventBus: EventBus
|
||||
uscases: SimNosUsecases
|
||||
) {
|
||||
this.eventBus = eventBus
|
||||
|
||||
// No se si hay un sistema mejor
|
||||
// convertor en const () => {} para conservar el contexto??
|
||||
this.recibeMsg = this.recibeMsg.bind(this)
|
||||
}
|
||||
|
||||
public async recibeMsg(msg: ConsumeMessage | null) {
|
||||
if (!this.validateActivationMsg(msg)) {
|
||||
throw new Error("Error consumiendo el mensaje no es valido")
|
||||
}
|
||||
|
||||
msg = msg!
|
||||
|
||||
const msgParsed = JSON.parse(String(msg.content))
|
||||
const msgKey = msg.fields.routingKey.split(".")
|
||||
const accion = msgKey[2]
|
||||
|
||||
if (accion == undefined) {
|
||||
console.error("La routingKey es incorrecta: " + accion)
|
||||
this.eventBus.nack(msg)
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.routes.get(accion) == undefined) {
|
||||
console.error("No hay una ruta definida para la accion")
|
||||
this.eventBus.nack(msg)
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.routes.get(accion)!()
|
||||
} catch (err) {
|
||||
console.log("Error procesando el mensaje")
|
||||
this.eventBus.nack(msg)
|
||||
} finally {
|
||||
this.eventBus.ack(msg)
|
||||
public activate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
console.log("Evento activate ", msg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO:
|
||||
* - Loguear motivos de la no validacion
|
||||
*/
|
||||
private validateActivationMsg(msg: ConsumeMessage | null) {
|
||||
if (msg == undefined) return false;
|
||||
return true;
|
||||
public suspend() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
console.log("Evento suspend ", msg)
|
||||
}
|
||||
}
|
||||
|
||||
public terminate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
console.log("Evento termiante ", msg)
|
||||
}
|
||||
}
|
||||
|
||||
public preActivate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
console.log("Evento preActivate ", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user