checkpoint

This commit is contained in:
AdisakKanthawilang 2024-11-18 12:02:45 +07:00
parent 0c70fe006f
commit 645b73e2e6

View file

@ -19,11 +19,11 @@ export async function init() {
const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol
console.log(connection ? "[AMQ] connection success" : "[AMQ] connection failed");
console.log(connection ? "[AMQ] Connection success" : "[AMQ] Connection failed");
const channel = await connection.createChannel(); //----> (1.4) create Channel
console.log(channel ? "[AMQ] create channel success" : "[AMQ] create channel failed");
console.log(channel ? "[AMQ] Create channel success" : "[AMQ] Create channel failed");
channel.assertQueue(queue, { durable: true }); //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ)
channel.assertQueue(queue_org, { durable: true });
@ -58,14 +58,12 @@ function createConsumer( //----> consumer
queue,
async (msg) => {
if (!msg) return;
console.log("[AMQ] on on create consumer");
if ((await handler(msg)) || retries++ >= 3) {
retries = 0;
console.log("[AMQ] process consumer success");
console.log("[AMQ] Process consumer success");
return channel.ack(msg);
}
console.log("[AMQ] process consumer fail");
console.log("[AMQ] Process consumer fail");
return await new Promise((resolve) => setTimeout(() => resolve(channel.nack(msg)), 3000));
},
{ noAck: false },
@ -73,31 +71,18 @@ function createConsumer( //----> consumer
}
async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
console.log(msg ? "[AMQ] on handler" : "[AMQ] don't handler");
console.log(msg ? "[AMQ] On handler" : "[AMQ] Not handler");
//----> condition before process consumer
const repo = AppDataSource.getRepository(Command);
const { data, token, user } = JSON.parse(msg.content.toString());
console.log(repo ? "[AMQ] have repo" : "[AMQ] don't have repo");
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
console.log(data ? "[AMQ] have data" : "[AMQ] don't have data");
const command = await repo.findOne({
where: { id: id },
relations: ["commandType", "commandRecives"],
});
console.log(command ? "[AMQ] have command" : "[AMQ] don't have command");
if (!command) return true;
const path = commandTypePath(command.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
console.log(path ? "[AMQ] have path" : "[AMQ] don't have path");
return await new CallAPI()
.PostData(
{