I am trying to get MySQL to carry out some routine tasks using the event_scheduler facility, which shows up in the result set returned by the SHOW PROCESSLIST; query.
I want to create 3 scheduled tasks: daily (at 00:00:00 AM), weekly ( on Sunday at 00:00:00 AM) and monthly (on every 1st day at 00:00:00 AM).
I know I could do that by designating a start time. Like this:
DELIMITER $$
DROP EVENT IF EXISTS do_daily $$
CREATE DEFINER = 'root'#'localhost'
EVENT do_daily
ON SCHEDULE
EVERY 1 DAY
**STARTS '2018-10-29 00:00:00'**
DO
BEGIN
-- Do daily chore...
END $$
DELIMITER $$
DROP EVENT IF EXISTS do_weekly $$
CREATE DEFINER = 'root'#'localhost'
EVENT do_weekly
ON SCHEDULE
EVERY 1 WEEK
**STARTS '2018-11-04 00:00:00'**
DO
BEGIN
-- Do weekly chore...
END $$
DELIMITER $$
DROP EVENT IF EXISTS do_monthly $$
CREATE DEFINER = 'root'#'localhost'
EVENT do_monthly
ON SCHEDULE
EVERY 1 WEEK
**STARTS '2018-11-01 00:00:00'**
DO
BEGIN
-- Do monthly chore...
END $$
The problem with this is that the date-times are hard-coded in the script and I'd like to make them generic so that I can run the queries later on without a worry.
In other words, when I run the query for these events, no matter what the time at the time of running may be, the STARTS expressions must always refer to the next "00:00:00 AM" for the daily, 'Sunday 00:00:00 AM' for the weekly and '1st day 00:00:00 AM' for the monthly routines.
Any idea how?
Related
I created an event which automatically gets the system current date and matches it with the start and end date of a school year. If the system current date is within the start and end date of the school year, it updates the isCurrentSchoolYear column with 1.
It worked perfect the first time I did this. However, when I changed the date on my PC to test if it will work with year 2020, nothing happens anymore. The event is set to run every 30 seconds.
schoolyear_mt table
(had to manually call the setCurrentSchoolYear() stored procedure to update isCurrentSchoolYear of 2020 to 1 which is supposed to be updated automatically)
======================================================================
enrollmentdb.setSchoolYear event definition:
CREATE EVENT enrollmentdb.e_setCurrentSchoolYear
ON SCHEDULE EVERY 30 second
DO CALL setCurrentSchoolYear();
======================================================================
SELECT ##event_scheduler;
======================================================================
show processlist;
show events from enrollmentdb;
As you can see, Interval Value column shows 30 but the Time column shows 63163099 and continues to increment.
So the problem is it doesn't automatically update the isCurrentSchoolYear column every after 30 seconds. I don't want to manually call the stored proc named setCurrentSchoolYear() to make it work.
CREATE DEFINER=`root`#`localhost` PROCEDURE `setCurrentSchoolYear`()
BEGIN
DECLARE aSyId INT;
DECLARE EXIT HANDLER FOR sqlexception
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
-- compare the currentdate
SELECT schoolyear_id INTO aSyId FROM schoolyear_mt
WHERE curdate() >= start_date AND curdate() <= end_date;
SELECT aSyId;
IF aSyId IS NOT NULL
THEN
UPDATE schoolyear_mt SET isCurrentSchoolYear = 0
WHERE schoolyear_id >= 0;
UPDATE schoolyear_mt SET isCurrentSchoolYear = 1
WHERE schoolyear_id = aSyId;
END IF;
COMMIT;
END
The problem with automatic update happened after I changed my machine's system date to Feb 02, 2020 and also tried Feb 02, 2015 to test if it's effective.
What could possible be hindering it from executing manually every 30 seconds?
Any thoughts?
Thank you.
I want to create an event which will start at 09:00 and Stops at 22:00.
and executes on every five minutes.
Is it possible to stop the event at 22:00 every day
and start 9:00 next day.
You can use MySQL Event Scheduler for this purpose but don't think you can explicitely stop the event likewise you are talking about. One example:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE table1 SET col1 = 100;
Create an event that calls a procedure called myEvent() every 5 minutes
DELIMITER $$
CREATE
EVENT `name_of_event`
ON SCHEDULE EVERY 5 MINUTE
DO BEGIN
CALL myEvent();
END $$
DELIMITER ;
In myEvent(): Check if time is between 09:00:00 and 22:00:00
DELIMITER $$
CREATE PROCEDURE myEvent()
BEGIN
IF((DATE_FORMAT(NOW(),'%H:%m:%s') BETWEEN '09:00:00' AND '22:00:00') THEN
// Do your stuff
END IF;
END $$
DELIMITER ;
I had a problem on MySQL event. I want this event run every first day of the year. Currently my event will run on every month. Here is my event script.
DELIMITER $$
CREATE EVENT create_ledger
ON SCHEDULE EVERY '1' MONTH
STARTS '2016-05-20 11:33:01'
DO
BEGIN
insert into ledger SET ledger_year= YEAR(CURDATE());
END$$
DELIMITER ;
hope someone can help. TQVM
Assuming you have MySQL rights to create events, the basic syntax is:
CREATE EVENT `event_name`
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
DO BEGIN
-- event body
END;
In your case just use:
CREATE EVENT create_ledger
ON SCHEDULE EVERY 1 YEAR
...
You may set STARTS & ENDS :
STARTS '2016-01-01 00:00:00'
ENDS '2031-01-01 00:00:00'
How can I create a trigger to call a MySQL stored procedure every hour between 12:00PM - 12:00AM every day?
This is what I have done to call the prosedure every hour every day. but I don't want it to run 24 times a day. I only want it to run between 12-12 only.
CREATE DEFINER=`root`#`%` EVENT `prod_trig`
ON SCHEDULE EVERY 1 HOUR STARTS '2014-01-17 18:15:00' ON COMPLETION NOT PRESERVE ENABLE
COMMENT 'This.....'
DO CALL `gd`.`prod_update_tr`()
You could use the IF Syntax to call this procedure in the desired timespan of the day only:
DELIMITER |
CREATE DEFINER=`root`#`%` EVENT `prod_trig`
ON SCHEDULE EVERY 1 HOUR STARTS '2014-01-17 18:15:00' ON COMPLETION NOT PRESERVE ENABLE
COMMENT 'This.....'
IF CURRENT_TIME() BETWEEN '00:00:00' AND '12:00:00' THEN
CALL `gd`.`prod_update_tr`();
END IF
|
DELIMITER ;
Event structure:
CREATE DEFINER=`root`#`localhost` EVENT `EVENT_NAME` ON SCHEDULE EVERY 5 MINUTE STARTS '2013-07-01 09:00:00' ENDS '2013-07-01 09:38:00' ON COMPLETION PRESERVE ENABLE DO update ... Event Query ...
this is going to allow the event query to run every five minutes only from 9:00 till 9:38
but after it hits 9:38 the event ends and it doesn't work again automatically by it self the next day because its disabled.
any solution i might be missing for this ?
Sadly I don't think that the SCHEDULE specification of a MySQL event is as flexible as the INTERVAL.
You could roll your own using something like this though:
delimiter $$
CREATE DEFINER=`root`#`localhost`
EVENT `EVENT_NAME`
ON SCHEDULE EVERY 5 MINUTE
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
IF (hour(now()) = 9 AND minute(now()) <= 38) THEN
-- Do your stuff here
END IF;
END$$