This commit is contained in:
Javier Segarra 2024-02-13 09:11:39 +01:00
parent 24984e8aca
commit 37dd3c00c5
8 changed files with 116 additions and 7 deletions

View File

@ -2,6 +2,7 @@ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `VN_SCHEDULER` CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `VN_SCHEDULER`
ON SCHEDULE EVERY 1 DAY ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENABLE
DO BEGIN DO BEGIN
CALL `VN_SCHEDULER`(); CALL `VN_SCHEDULER`();
END$$ END$$

View File

@ -1,8 +1,75 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`VN_SCHEDULER`() CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`VN_SCHEDULER`()
BEGIN BEGIN
DECLARE vDone BOOL;
DECLARE vError VARCHAR(255) DEFAULT NULL;
DECLARE vErrorCode VARCHAR(255) DEFAULT NULL;
DECLARE vSchema VARCHAR(255);
DECLARE vStatus VARCHAR(255);
DECLARE vProcedure VARCHAR(255);
-- DECLARE vLogMail VARCHAR(255);
DECLARE vScheduleFk INT;
-- Obtener datos de la tabla vn_schedules -- Obtener datos de la tabla vn_schedules
DECLARE vQueue CURSOR FOR
SELECT id, `schema`, `procedure`, `status`
FROM vn_schedules
WHERE status = 'scheduled';
OPEN vQueue;
l: LOOP
SET vDone = FALSE;
FETCH vQueue INTO vScheduleFk, vSchema, vProcedure, vStatus;
IF vDone THEN
LEAVE l;
END IF;
UPDATE vn_schedules
SET `started` = util.VN_NOW(),
`finished` = NULL,
`error` = NULL,
`errorCode` = NULL
WHERE id = vScheduleFk;
SET vError = NULL;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
GET DIAGNOSTICS CONDITION 1
vError = MESSAGE_TEXT,
vErrorCode = RETURNED_SQLSTATE;
CALL util.exec(CONCAT('CALL `', vSchema ,'`.`', vProcedure ,'`'));
IF vError IS NOT NULL THEN
-- IF vLogMail IS NOT NULL THEN
-- CALL vn.mail_insert(
-- vLogMail,
-- NULL,
-- CONCAT('Nightly task failed (', vSchema, '.', vProcedure, ')'),
-- CONCAT(
-- '[', vErrorCode, '] ', vError, CHAR(13, 10), -- Line break
-- 'See ', SCHEMA(), '.nightTask table for more info.'
-- )
-- );
-- END IF;
UPDATE vn_schedules
SET `error` = vError,
finished = util.VN_NOW(),
lastFinished = util.VN_NOW()
`errorCode` = vErrorCode
`status` = 'error'
WHERE id = vScheduleFk;
ELSE
UPDATE vn_schedules
SET `error` = vError,
`errorCode` = vErrorCode
finished = util.VN_NOW(),
lastFinished = util.VN_NOW(),
`status` = 'exec'
WHERE id = vScheduleFk;
END IF;
END LOOP;
CLOSE vQueue;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -0,0 +1,17 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`zoneCollisions`
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
DECLARE vEXEC BOOLEAN DEFAULT FALSE;
SELECT COUNT(*)>0 INTO vEXEC from util.vn_schedules;
IF vEXEC THEN
CALL `zone_getCollisions`();
END IF;
END$$
DELIMITER ;

View File

@ -11,6 +11,9 @@ BEGIN
-- Activo el evento 'zone_getCollisions' -- Activo el evento 'zone_getCollisions'
-- UPDATE `mysql`.`event` SET status = 1 WHERE name = 'zone_getCollisions'; -- UPDATE `mysql`.`event` SET status = 1 WHERE name = 'zone_getCollisions';
INSERT IGNORE INTO `util`.`vn_schedules`(`schema`, `procedure`)
VALUES
(`vn`, `zone_getCollisions`);
-- Insert en vn_schedules 'zone_getCollisions' -- Insert en vn_schedules 'zone_getCollisions'
END$$ END$$

View File

@ -6,6 +6,10 @@ BEGIN
-- Activo el evento 'zone_getCollisions' -- Activo el evento 'zone_getCollisions'
-- Insert en vn_schedulesla key 'zone_getCollisions' -- Insert en vn_schedulesla key 'zone_getCollisions'
-- ALTER EVENT zoneCollisions ENABLE;
INSERT IGNORE INTO `util`.`vn_schedules`(`schema`, `procedure`)
VALUES
(`vn`, `zone_getCollisions`);
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -7,5 +7,8 @@ BEGIN
-- Activo el evento 'zone_getCollisions' -- Insert en vn_schedules 'zone_getCollisions' -- Activo el evento 'zone_getCollisions' -- Insert en vn_schedules 'zone_getCollisions'
INSERT IGNORE INTO `util`.`vn_schedules`(`schema`, `procedure`)
VALUES
(`vn`, `zone_getCollisions`);
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS `util`.`vn_schedules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`started` datetime DEFAULT NULL,
`finished` datetime DEFAULT NULL,
`lastFinished` datetime DEFAULT NULL,
`order` int(11) DEFAULT NULL,
`schema` varchar(45) NOT NULL,
`procedure` varchar(100) NOT NULL,
`error` varchar(255) DEFAULT NULL,
`errorCode` varchar(10) DEFAULT NULL,
`status` enum('scheduled','exec','error') NOT NULL DEFAULT 'scheduled',
PRIMARY KEY (`id`),
UNIQUE KEY unique_schedule (`schema`, `procedure`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

View File

@ -1,10 +1,10 @@
INSERT INTO util.notification (id, name,description) -- INSERT INTO util.notification (id, name,description)
VALUES ( -- VALUES (
(SELECT MAX(id)+1 from util.notification n) , 'zone-included','An email to notify zoneCollisions'); -- (SELECT MAX(id)+1 from util.notification n) , 'zone-included','An email to notify zoneCollisions');
INSERT INTO util.notificationSubscription (notificationFk,userFk) -- INSERT INTO util.notificationSubscription (notificationFk,userFk)
SELECT id, account.myUser_getId() FROM util.notification WHERE name= "zone-included"; -- SELECT id, account.myUser_getId() FROM util.notification WHERE name= "zone-included";
INSERT INTO util.notificationAcl (notificationFk,roleFk) -- INSERT INTO util.notificationAcl (notificationFk,roleFk)
SELECT id, (SELECT id from `account`.`role` where name = "system") FROM util.notification WHERE name= "zone-included"; -- SELECT id, (SELECT id from `account`.`role` where name = "system") FROM util.notification WHERE name= "zone-included";