diff --git a/db/routines/util/events/vn_scheduler.sql b/db/routines/util/events/vn_scheduler.sql index 4c5c99b6a..e25741ac6 100644 --- a/db/routines/util/events/vn_scheduler.sql +++ b/db/routines/util/events/vn_scheduler.sql @@ -2,6 +2,7 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `VN_SCHEDULER` ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY + ENABLE DO BEGIN CALL `VN_SCHEDULER`(); END$$ diff --git a/db/routines/util/procedures/vn_scheduler.sql b/db/routines/util/procedures/vn_scheduler.sql index d34fe9092..1ab72de39 100644 --- a/db/routines/util/procedures/vn_scheduler.sql +++ b/db/routines/util/procedures/vn_scheduler.sql @@ -1,8 +1,75 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `util`.`VN_SCHEDULER`() 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 + 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$$ DELIMITER ; diff --git a/db/routines/vn/events/zone_getCollisions.sql b/db/routines/vn/events/zone_getCollisions.sql new file mode 100644 index 000000000..2437cffc2 --- /dev/null +++ b/db/routines/vn/events/zone_getCollisions.sql @@ -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 ; diff --git a/db/routines/vn/triggers/zoneIncluded_afterDelete.sql b/db/routines/vn/triggers/zoneIncluded_afterDelete.sql index 248717f51..a46ba9060 100644 --- a/db/routines/vn/triggers/zoneIncluded_afterDelete.sql +++ b/db/routines/vn/triggers/zoneIncluded_afterDelete.sql @@ -11,6 +11,9 @@ BEGIN -- Activo el evento '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' END$$ diff --git a/db/routines/vn/triggers/zoneIncluded_beforeInsert.sql b/db/routines/vn/triggers/zoneIncluded_beforeInsert.sql index 2e1f70407..810546d91 100644 --- a/db/routines/vn/triggers/zoneIncluded_beforeInsert.sql +++ b/db/routines/vn/triggers/zoneIncluded_beforeInsert.sql @@ -6,6 +6,10 @@ BEGIN -- Activo el evento '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$$ DELIMITER ; diff --git a/db/routines/vn/triggers/zoneIncluded_beforeUpdate.sql b/db/routines/vn/triggers/zoneIncluded_beforeUpdate.sql index 05c3acd41..a5e0184b4 100644 --- a/db/routines/vn/triggers/zoneIncluded_beforeUpdate.sql +++ b/db/routines/vn/triggers/zoneIncluded_beforeUpdate.sql @@ -7,5 +7,8 @@ BEGIN -- Activo el evento 'zone_getCollisions' -- Insert en vn_schedules 'zone_getCollisions' + INSERT IGNORE INTO `util`.`vn_schedules`(`schema`, `procedure`) + VALUES + (`vn`, `zone_getCollisions`); END$$ DELIMITER ; diff --git a/db/versions/10881-greenHydrangea/01-createVnSchedules.sql b/db/versions/10881-greenHydrangea/01-createVnSchedules.sql new file mode 100644 index 000000000..07dedf7d3 --- /dev/null +++ b/db/versions/10881-greenHydrangea/01-createVnSchedules.sql @@ -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; diff --git a/db/versions/10881-greenHydrangea/01-notification.vn.sql b/db/versions/10881-greenHydrangea/01-notification.vn.sql index d05902461..83e2be1d0 100644 --- a/db/versions/10881-greenHydrangea/01-notification.vn.sql +++ b/db/versions/10881-greenHydrangea/01-notification.vn.sql @@ -1,10 +1,10 @@ -INSERT INTO util.notification (id, name,description) - VALUES ( -(SELECT MAX(id)+1 from util.notification n) , 'zone-included','An email to notify zoneCollisions'); +-- INSERT INTO util.notification (id, name,description) +-- VALUES ( +-- (SELECT MAX(id)+1 from util.notification n) , 'zone-included','An email to notify zoneCollisions'); -INSERT INTO util.notificationSubscription (notificationFk,userFk) - SELECT id, account.myUser_getId() FROM util.notification WHERE name= "zone-included"; +-- INSERT INTO util.notificationSubscription (notificationFk,userFk) +-- SELECT id, account.myUser_getId() FROM util.notification WHERE name= "zone-included"; -INSERT INTO util.notificationAcl (notificationFk,roleFk) - SELECT id, (SELECT id from `account`.`role` where name = "system") FROM util.notification WHERE name= "zone-included"; +-- INSERT INTO util.notificationAcl (notificationFk,roleFk) +-- SELECT id, (SELECT id from `account`.`role` where name = "system") FROM util.notification WHERE name= "zone-included";