/* RESCHEDULE FAILED NOTIFICATION IN QUEUE */
/* =============================================================================================================== */
DROP PROCEDURE IF EXISTS rescheduleFailedNotification;
COMMIT;

DELIMITER //

CREATE PROCEDURE `rescheduleFailedNotification`(`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: notifFail', NOW(), errno; 

	RESIGNAL;
END;

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

END
//

COMMIT //

DELIMITER ;

