Mysql Event action stopped executing automatically - mysql

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.

Related

MySQL Recurring event not executing

I have a recurring event set to execute each 1 minute in MySQL:
BEGIN
DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 1 MINUTE);
END
For some reason, my session entries, such as one with the date parameter of
2019-01-19 18:28:24
are not being deleted, despite the fact that SELECT NOW(); currently returns
2019-01-19 18:44:15
So this is more than a minute after the creation date and proves that timezones aren't the problem. Is my event simply failing to execute or is there a problem with the event itself?
EDIT: Please note that the event is set to execute each minute AND delete session entries that are more than a minute old, not just one or the other. The recurring interval isn't shown above, but I've confirmed that it is 1 minute.
I had to run
SET GLOBAL event_scheduler = on;
in order to fix the issue. I had rebooted my XAMPP local server since the last time I tested this, which is why it suddenly stopped working.

Create MySQL event and run first day of the year

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'

mysql scheduled events problems

I have a table reservation containing starttime,endtime,status. i wanted to create an event scheduler which checks whether the current time is greater than endtime . if it is greater then it sets the status to 0.
but i want that scheduler to run everyday from 6 am to 6pm at 5 minutes interval for forever. so far i have written a code
CREATE EVENT rescancel
ON SCHEDULE EVERY 5 MINUTE
STARTS 06:00:00
ENDS 18:00:00
DO
UPDATE reservation SET status = 0 WHERE CURTIME() > endtime
how can i make sure that this event runs everyday? so far from what i have read from different websites , this code runs for every 5 minutes from 6 am to 6pm only for that day when event is created. am i wrong? i am new to mysql triggers and events.
I use something similar to check days and times, so you could use:
DELIMITER ||
CREATE EVENT rescancel
ON SCHEDULE
EVERY 5 MINUTE
STARTS NOW()
ON COMPLETION PRESERVE
COMMENT ''
DO
BEGIN
IF CURRENT_TIME BETWEEN '06:00:00' AND '18:00:00' THEN
UPDATE reservation SET status = 0 WHERE CURTIME() > endtime;
END IF;
END ||
DELIMITER ;
Test the current time in the query:
CREATE EVENT rescancel
ON SCHEDULE EVERY 5 MINUTE
DO
UPDATE reservation
SET status = 0
WHERE HOUR(CURTIME()) BETWEEN 6 AND 17
AND CURTIME() > endtime

How do I generate monthly events?

I want to create a monthly event for every registered customer that fires an insert statement on billing table. If an event is specified to run from say 20th Aug 2013 0:00 hrs (a future date), on what date can I expect next event to occur?
I don't know whether to assume it as 30 days or 365/12 days (mathematically).
I am using something like this to create that event:
DELIMITER $$
CREATE EVENT event1
ON SCHEDULE EVERY '1' MONTH
STARTS '2013-08-20 00:00:00'
DO
BEGIN
END$$
DELIMITER ;
My question is when does mysql fire this event in the next month?
Rescheduled events rely on the date interval function of MySQL. Your event would reoccur on:
SELECT DATE_ADD('2013-08-20', INTERVAL 1 MONTH)
that's 2013-09-20.
Fore more details about the date functions, see:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

Mysql Events Execution Start time

I have one mysql event which will execute every 1 day and do some activity. Please find code snippets for the same.
delimiter |
CREATE EVENT createTest
ON SCHEDULE EVERY 1 DAY
DO
BEGIN
-- Do some work
END |
delimiter ;
when i created this event it executes same time for the same day. For next day it executes (last execution + 24 Hour) scenario.
is it possible to set start time in events? I want to executes this events on every day 0 hour?
You can use this:
CREATE EVENT createTest
ON SCHEDULE EVERY '1' DAY
STARTS CONCAT(DATE(NOW()+INTERVAL 1 DAY ), ' 00:00:00')
DO
BEGIN
-- do your task
END
Yes, you can do it. Use STARTS option for this purpose.
CREATE EVENT event1
ON SCHEDULE EVERY '1' DAY
STARTS '2012-10-03 00:00:00'
DO
BEGIN
-- do something
END
Note, that event start datetime must be in a future.
For #spt -
CREATE EVENT event1
ON SCHEDULE EVERY '1' YEAR
STARTS '2016-04-01 00:00:00'
DO
BEGIN
END
Also, if it is hard to create and setup event object, you can use GUI event object editor in dbForge Studio for MySQL (free Express edition allows it).