feat: adjust errors in bulljs queue to remote ticket creation

feat-scaling-ticket-remote-creation
gustavo-gsp 2024-05-24 09:14:44 -03:00
parent 4f4c2af13a
commit 47d45cbc13
4 changed files with 35 additions and 42 deletions

View File

@ -134,15 +134,14 @@ export const remoteTicketCreation = async (
} }
//Create a job to remote ticket creation //Create a job to remote ticket creation
remoteTicketCreationQueue.add(`${contact_from}-${contact_to}`, { // `${contact_from}-${contact_to}`,
input: { await remoteTicketCreationQueue.add({
queueId, queueId,
contact_from, contact_from,
cc, cc,
contact_to, contact_to,
msg, msg,
contact_name, contact_name,
}
}) })
return res.status(HttpStatusCode.Processing).json({ return res.status(HttpStatusCode.Processing).json({

View File

@ -20,12 +20,16 @@ import controllByNumber from "../../helpers/controllByNumber";
import _botIsOnQueue from "../../helpers/BotIsOnQueue"; import _botIsOnQueue from "../../helpers/BotIsOnQueue";
import { getSettingValue } from "../../helpers/WhaticketSettings"; import { getSettingValue } from "../../helpers/WhaticketSettings";
import { set } from "../../helpers/RedisClient"; import { set } from "../../helpers/RedisClient";
import { logger } from "../../utils/logger";
export const handleAddRemoteTicketCreationQueue = async (job: Job) => { export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
try { try {
const { id, data } = job const { id, data } = job
console.log(`handleAddRemoteTicketCreationQueue Job started with ID ${id}`)
logger.info(`JOB REMOTE TICKET CREATION STARTED WITH ID ${id}`)
let { queueId, contact_from, cc, contact_to, msg, contact_name }: any = data; let { queueId, contact_from, cc, contact_to, msg, contact_name }: any = data;
let whatsappId: any; let whatsappId: any;
@ -96,6 +100,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
const validNumber = contact_to; const validNumber = contact_to;
if (validNumber) { if (validNumber) {
let contact = await Contact.findOne({ where: { number: validNumber } }); let contact = await Contact.findOne({ where: { number: validNumber } });
if (!contact) { if (!contact) {
@ -155,7 +160,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
}); });
if (ticket) { if (ticket) {
console.log( logger.error(
`THE CAMPAIGN TICKET WAS NOT CREATED BECAUSE THE TICKET IS PENDING OR OPEN` `THE CAMPAIGN TICKET WAS NOT CREATED BECAUSE THE TICKET IS PENDING OR OPEN`
); );
return Promise.reject( return Promise.reject(
@ -207,13 +212,13 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
}); });
} }
console.log( logger.info(
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 200 | MSG: success` `REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 200 | MSG: success`
); );
return Promise.resolve(); // return res.status(200).json({ msg: "success" }); return Promise.resolve(); // return res.status(200).json({ msg: "success" });
} }
console.log( logger.error(
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: The number ${contact_to} does not exist on WhatsApp` `REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: The number ${contact_to} does not exist on WhatsApp`
); );
@ -224,7 +229,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
// .status(500) // .status(500)
// .json({ msg: `The number ${contact_to} does not exist on WhatsApp` }); // .json({ msg: `The number ${contact_to} does not exist on WhatsApp` });
} catch (error) { } catch (error) {
console.log(`Error trying to execute handleAddRemoteTicketCreation. \nReason:`, error) logger.error(`Error trying to execute handleAddRemoteTicketCreation. \nReason:`, error)
return Promise.reject(new Error("Error trying to create a remote ticket in queue")) return Promise.reject(new Error("Error trying to create a remote ticket in queue"))
} }
} }

View File

@ -1,6 +1,6 @@
import Queue from "bull"; import Queue from "bull";
import cluster from "cluster";
import { handleAddRemoteTicketCreationQueue } from "./remote-ticket-creation-handle"; import { handleAddRemoteTicketCreationQueue } from "./remote-ticket-creation-handle";
import { logger } from "../../utils/logger";
export let remoteTicketCreationQueue: Queue.Queue<any>; export let remoteTicketCreationQueue: Queue.Queue<any>;
@ -17,37 +17,26 @@ export function initRemoteTicketCreationQueue() {
}, },
}); });
const numOfWorkers = 4; logger.info("Main Worker of remote ticket creation initiated")
if (cluster.isMaster) { remoteTicketCreationQueue.on("completed", (job, result) => {
console.log("Main Worker of remote ticket creation initiated") logger.info("===========================")
for (let i = 0; i < numOfWorkers; i++) { logger.info(`Job with id ${job.id} completed, result: ${result}`)
cluster.fork() });
}
cluster.on('exit', function (worker, code, signal) { remoteTicketCreationQueue.on("error", (error) => {
console.log(`Worker ${worker.id} of remote ticket creation with code ${code}, signal: ${signal}...`) logger.error("===========================")
}); logger.error(error, `Error trying to execute Remote Ticket Creation JOB`)
})
remoteTicketCreationQueue.on("completed", (job, result) => { remoteTicketCreationQueue.on("failed", (job) => {
console.log(`Job completed ${result}`) logger.error("===========================")
}); logger.error(`Failed trying to execute Remote Ticket Creation JOB ${job.id} with data ${JSON.stringify(job.data)} and stack trace: ${job.stacktrace}, attemp: ${job.attemptsMade}`)
})
remoteTicketCreationQueue.on("error", (error) => { remoteTicketCreationQueue.process(handleAddRemoteTicketCreationQueue)
console.log(`Error trying to execute Remote Ticket Creation JOB ${error}`)
})
remoteTicketCreationQueue.on("failed", (job) => {
console.log(`Failed trying to execute Remote Ticket Creation JOB ${job.id} with data ${job.data}`)
})
} else {
console.log(`Worker of remote ticket creation initiated`)
remoteTicketCreationQueue.process(numOfWorkers, handleAddRemoteTicketCreationQueue)
}
} else { } else {
console.log(`Warning: Trying to init remote ticket creation queue process, but it is already running.`) logger.info(`Warning: Trying to init remote ticket creation queue process, but it is already running.`)
} }
} }

View File

@ -43,7 +43,7 @@
"yup": "^0.32.8" "yup": "^0.32.8"
}, },
"scripts": { "scripts": {
"start": "react-scripts start", "start": "PORT=3333 react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject"