/* RESCHEDULE FAILED EMAIL IN QUEUE */
/* =============================================================================================================== */
DROP PROCEDURE IF EXISTS rescheduleFailedEmail;
COMMIT;

DELIMITER //

CREATE PROCEDURE `rescheduleFailedEmail`(`p_queueId` INT, `p_error` TEXT)
BEGIN

DECLARE errno INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
	GET CURRENT DIAGNOSTICS CONDITION 1 errno = MYSQL_ERRNO;

	INSERT INTO debug_log(activity, ts_when, log) 
  SELECT 'PROC ERR: emailFail', NOW(), errno; 

	RESIGNAL;
END;

UPDATE `EmailQueue`
SET `status` = 'ready',
  `attempts` = `attempts` + 1,
  `error` = p_error,
  `due` = DATE_ADD(NOW(), INTERVAL 1 MINUTE),
  `updatedAt` = NOW()
WHERE `_id` = p_queueId;

END
//

COMMIT //

DELIMITER ;

