DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` EVENT `vn`.`printQueue_check`
ON SCHEDULE EVERY 10 MINUTE
STARTS '2022-01-28 09:52:46.000'
ON COMPLETION PRESERVE
ENABLE
DO BEGIN
DECLARE vCurrentCount INT;
DECLARE vCheckSum INT;
DECLARE vIsAlreadyNotified BOOLEAN;
DECLARE vTableQueue TEXT;
DECLARE vLineQueue TEXT;
DECLARE vDone BOOL DEFAULT FALSE;
DECLARE vPrintQueueLimit INT;
DECLARE vCur CURSOR FOR
SELECT CONCAT('
\n\t\t\t\t\t\t\t', IFNULL(pq.id, ''), ' | \n\t\t\t\t\t\t\t', IFNULL(p.path, ''),' | \n\t\t\t\t\t\t\t', IFNULL(r.name, ''),' | \n\t\t\t\t\t\t\t', IFNULL(pq.statusCode, ''),' | \n\t\t\t\t\t\t\t', IFNULL(w.firstname, ''), " ", IFNULL(w.lastName, ''),' | \n\t\t\t\t\t\t\t', IFNULL(pq.`error`, ''),' | \n\t\t\t\t\t\t
')
FROM printQueue pq
LEFT JOIN worker w ON w.id = pq.workerFk
LEFT JOIN printer p ON p.id = pq.printerFk
LEFT JOIN report r ON r.id = pq.reportFk
JOIN printQueueConfig
LIMIT vPrintQueueLimit;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET vDone = TRUE;
SELECT printQueueLimit INTO vPrintQueueLimit
FROM printQueueConfig;
SELECT COUNT(*), IFNULL(SUM(id),0) INTO vCurrentCount, vCheckSum
FROM printQueue WHERE statusCode = 'queued';
SELECT isAlreadyNotified INTO vIsAlreadyNotified
FROM printingQueueCheck;
IF (SELECT lastCount FROM printingQueueCheck) = vCurrentCount AND
(SELECT lastCheckSum FROM printingQueueCheck) = vCheckSum AND
vIsAlreadyNotified = FALSE AND vCurrentCount > 0
THEN
SELECT '\n\t\t\t\t\t\n\t\t\t\t\t\tId Cola | \n\t\t\t\t\t\tRuta Impresora | \n\t\t\t\t\t\tInforme | \n\t\t\t\t\t\tEstado | \n\t\t\t\t\t\tTrabajador | \n\t\t\t\t\t\tError | \n\t\t\t\t\t
' INTO vTableQueue;
OPEN vCur;
l: LOOP
SET vDone = FALSE;
FETCH vCur INTO vLineQueue;
IF vDone THEN
LEAVE l;
END IF;
SELECT CONCAT(vTableQueue, vLineQueue) INTO vTableQueue;
END LOOP;
CLOSE vCur;
CALL mail_insert(
'cau@verdnatura.es',
NULL,
'Servidor de impresion parado',
CONCAT('Hay ', vCurrentCount, ' lineas bloqueadas', vTableQueue, '
')
);
UPDATE printingQueueCheck SET isAlreadyNotified = TRUE;
END IF;
IF (SELECT lastCount FROM printingQueueCheck) > vCurrentCount AND
vIsAlreadyNotified = TRUE
THEN
UPDATE printingQueueCheck SET isAlreadyNotified = FALSE;
END IF;
UPDATE printingQueueCheck
SET lastCount = vCurrentCount,
lastCheckSum = vCheckSum;
END$$
DELIMITER ;