Mysql Event scheduling history

Keywords: MySQL Database

Mysql Event scheduling history

The current mysql version does not have Event execution history information. In order to check whether the Event is executed normally and the execution result conveniently, the following two steps can be used:

 

1, Create job execution Event history table

  1. CREATE TABLE `mysql`.`t_event_history` (  
  2.   `dbname` VARCHAR(128) NOT NULL DEFAULT '',  
  3.   `eventname` VARCHAR(128) NOT NULL DEFAULT '',  
  4.   `starttime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',  
  5.   `endtime` DATETIME DEFAULT NULL,  
  6.   `issuccess` INT(11) DEFAULT NULL,  
  7.   `duration` INT(11) DEFAULT NULL,  
  8.   `errormessage` VARCHAR(512) DEFAULT NULL,  
  9.   `randno` INT(11) DEFAULT NULL,  
  10.   PRIMARY KEY (`dbname`,`eventname`,`starttime`),  
  11.   KEY `ix_endtime` (`endtime`),  
  12.   KEY `ix_starttime_randno` (`starttime`,`randno`)  
  13. ) ENGINE=INNODB DEFAULT CHARSET=utf8;  

 

2, Create a job based on the following template

#Please pay attention to modify the relevant information according to the actual situation

  1. DELIMITER $$  
  2. CREATE DEFINER=`root`@`localhost` EVENT `e_test` ON SCHEDULE   
  3. #Modify the following scheduling information
  4. EVERY 1 DAY STARTS '2014-01-03 01:00:00' ON COMPLETION PRESERVE ENABLE DO   
  5. BEGIN  
  6.     DECLARE r_code CHAR(5) DEFAULT '00000';  
  7.     DECLARE r_msg TEXT;  
  8.     DECLARE v_error INTEGER;  
  9.     DECLARE v_starttime DATETIME DEFAULT NOW();  
  10.     DECLARE v_randno INTEGER DEFAULT FLOOR(RAND()*100001);  
  11.       
  12.     INSERT INTO mysql.t_event_history (dbname,eventname,starttime,randno)   
  13.     #Modify the following job name (the name of the job)
  14.     VALUES(DATABASE(),'e_test', v_starttime,v_randno);    
  15.       
  16.     BEGIN  
  17.         #Exception handling section
  18.         DECLARE CONTINUE HANDLER FOR SQLEXCEPTION    
  19.         BEGIN  
  20.             SET  v_error = 1;  
  21.             GET DIAGNOSTICS CONDITION 1 r_code = RETURNED_SQLSTATE , r_msg = MESSAGE_TEXT;  
  22.         END;  
  23.           
  24.         #Here is the actual called user program procedure
  25.         CALL test.usp_test1();  
  26.     END;  
  27.       
  28.     UPDATE mysql.t_event_history SET endtime=NOW(),issuccess=ISNULL(v_error),duration=TIMESTAMPDIFF(SECOND,starttime,NOW()), errormessage=CONCAT('error=',r_code,', message=',r_msg),randno=NULL WHERE starttime=v_starttime AND randno=v_randno;  
  29.       
  30. END$$  
  31. DELIMITER ;  

By querying the mysql.t_event_history table, we can know when the event is executed, whether it is successful, how long it takes to execute, and the error information when there is an error. It is very convenient for us to manage our daily scheduling plan.


Posted by Danicek on Tue, 31 Mar 2020 11:51:26 -0700