refactor: refs #4452 Changes
This commit is contained in:
parent
20fca12316
commit
6978e324b1
|
@ -29,7 +29,6 @@ async function getConnectionWithRetries(retries = 3, delay = 5000) {
|
|||
return await pool.getConnection();
|
||||
} catch (error) {
|
||||
attempt++;
|
||||
logger.error(`Error al obtener conexión (intento ${attempt}):`, error);
|
||||
if (attempt >= retries)
|
||||
throw new Error('No se pudo obtener conexión después de múltiples intentos.');
|
||||
|
||||
|
@ -49,6 +48,7 @@ async function sendZPL(zplContent, ipAddress, retries = 3, delay = 2000) {
|
|||
|
||||
client.connect(port, ipAddress, () => {
|
||||
client.write(zplContent, () => {
|
||||
client.destroy();
|
||||
resolve('success');
|
||||
});
|
||||
});
|
||||
|
@ -73,9 +73,8 @@ async function sendZPL(zplContent, ipAddress, retries = 3, delay = 2000) {
|
|||
}
|
||||
|
||||
} catch (error) {
|
||||
logger.error(`Error al enviar ZPL a la impresora (intento ${attempt + 1}):`, error.message);
|
||||
if (attempt >= retries - 1)
|
||||
return 'error';
|
||||
// if (attempt >= retries - 1)
|
||||
return 'error';
|
||||
await new Promise(resolve => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
|
@ -83,9 +82,7 @@ async function sendZPL(zplContent, ipAddress, retries = 3, delay = 2000) {
|
|||
}
|
||||
|
||||
// Función para obtener la dirección IP de la impresora, realizando una llamada a la base de datos a la tabla de printer
|
||||
async function getPrinterIpAddress(printerFk) {
|
||||
|
||||
const connection = await getConnectionWithRetries();
|
||||
async function getPrinterIpAddress(printerFk, connection) {
|
||||
try {
|
||||
dotenv.config();
|
||||
const [rows] = await connection.query(`
|
||||
|
@ -97,13 +94,11 @@ async function getPrinterIpAddress(printerFk) {
|
|||
} catch (error) {
|
||||
logger.error('Error al obtener la dirección IP de la impresora:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
// Función para obtener un único registro para procesar
|
||||
async function getRecordForProcessing(retries = 5, delay = 5000) {
|
||||
async function getRecordForProcessing(retries = 5, delay = 4000) {
|
||||
for (let attempt = 0; attempt < retries; attempt++) {
|
||||
const connection = await getConnectionWithRetries();
|
||||
try {
|
||||
|
@ -156,7 +151,7 @@ async function processRecord(record) {
|
|||
|
||||
const zplContent = await fs.readFile(filePath, 'utf8');
|
||||
|
||||
const ipAddress = await getPrinterIpAddress(record.printerFk);
|
||||
const ipAddress = await getPrinterIpAddress(record.printerFk, connection);
|
||||
|
||||
const sendResult = await sendZPL(zplContent, ipAddress);
|
||||
|
||||
|
@ -178,7 +173,8 @@ async function processRecord(record) {
|
|||
} catch (error) {
|
||||
logger.error('Error al procesar el registro:', error);
|
||||
parentPort.postMessage('error');
|
||||
await connection.rollback();
|
||||
if (!error.message === `Can't add new command when connection is in closed state`)
|
||||
await connection.rollback();
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue