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
remoteTicketCreationQueue.add(`${contact_from}-${contact_to}`, {
input: {
queueId,
contact_from,
cc,
contact_to,
msg,
contact_name,
}
// `${contact_from}-${contact_to}`,
await remoteTicketCreationQueue.add({
queueId,
contact_from,
cc,
contact_to,
msg,
contact_name,
})
return res.status(HttpStatusCode.Processing).json({

View File

@ -20,12 +20,16 @@ import controllByNumber from "../../helpers/controllByNumber";
import _botIsOnQueue from "../../helpers/BotIsOnQueue";
import { getSettingValue } from "../../helpers/WhaticketSettings";
import { set } from "../../helpers/RedisClient";
import { logger } from "../../utils/logger";
export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
try {
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 whatsappId: any;
@ -96,6 +100,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
const validNumber = contact_to;
if (validNumber) {
let contact = await Contact.findOne({ where: { number: validNumber } });
if (!contact) {
@ -155,7 +160,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
});
if (ticket) {
console.log(
logger.error(
`THE CAMPAIGN TICKET WAS NOT CREATED BECAUSE THE TICKET IS PENDING OR OPEN`
);
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`
);
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`
);
@ -224,7 +229,7 @@ export const handleAddRemoteTicketCreationQueue = async (job: Job) => {
// .status(500)
// .json({ msg: `The number ${contact_to} does not exist on WhatsApp` });
} 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"))
}
}

View File

@ -1,6 +1,6 @@
import Queue from "bull";
import cluster from "cluster";
import { handleAddRemoteTicketCreationQueue } from "./remote-ticket-creation-handle";
import { logger } from "../../utils/logger";
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) {
console.log("Main Worker of remote ticket creation initiated")
for (let i = 0; i < numOfWorkers; i++) {
cluster.fork()
}
remoteTicketCreationQueue.on("completed", (job, result) => {
logger.info("===========================")
logger.info(`Job with id ${job.id} completed, result: ${result}`)
});
cluster.on('exit', function (worker, code, signal) {
console.log(`Worker ${worker.id} of remote ticket creation with code ${code}, signal: ${signal}...`)
});
remoteTicketCreationQueue.on("error", (error) => {
logger.error("===========================")
logger.error(error, `Error trying to execute Remote Ticket Creation JOB`)
})
remoteTicketCreationQueue.on("completed", (job, result) => {
console.log(`Job completed ${result}`)
});
remoteTicketCreationQueue.on("failed", (job) => {
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) => {
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)
}
remoteTicketCreationQueue.process(handleAddRemoteTicketCreationQueue)
} 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"
},
"scripts": {
"start": "react-scripts start",
"start": "PORT=3333 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"