mysql event not going off but event scheduler is on - mysql

I have a MySQL event and it wont go off. I have made sure that the event scheduler is on. is there something wrong with my create event code?
create event POS_Data_Import.alterSalesTransactions
on schedule
every 1 day
starts '2023-01-11 05:50:00' on completion preserve enable
do
CALL `POS_Data_Import`.`sp_Alter_CustomerExport`();
CALL `POS_Data_Import`.`sp_Alter_SalesTransactionImport`();

Use begin...end statements around the two calls like this example:
delimiter //
CREATE EVENT mydatabase.myevent
ON SCHEDULE
EVERY 1 MINUTE
starts '2023-01-12 05:50:00' on completion preserve enable
DO
BEGIN
UPDATE mydatabase.table1 SET inventory = inventory + 1;
UPDATE mydatabase.table2 SET inventory = inventory + 2;
END //
delimiter ;
After you do that, check that the event_definition has been created correctly:
select * from information_schema.events;
And that the event is enabled:
show events from mydatabase;
And that the event scheduler is running (the state should be "waiting for next activation"):
show processlist;

Related

MySQL events don't execute

I'm working in a QA server, which turns down every day at 22:00 and turns on at 06:00 and I have an event that must execute at 06:40 but it never does, except when the server stays on.
This is the EVENT conf:
CREATE EVENT test_event
ON SCHEDULE EVERY 1 DAY
STARTS '2021-12-15 06:40:00.000'
ON COMPLETION NOT PRESERVE
ENABLE
DO CALL schema.sp()
enable the event scheduler or test if on
SET GLOBAL event_scheduler=ON;
see: https://mariadb.com/docs/reference/mdb/system-variables/event_scheduler/
it equal to MySQL

How can I identify which time-driven trigger has called a function?

I have a Google App Script function in a Google Sheets spreadsheet I need to call once a day using a time-driven trigger.
This function often takes longer to run than the maximum time allowed for scripts, currently 6 minutes, so I've written it to do its work over several calls. If the function hasn't finished I would like to create a temporary time-driven trigger to run the function again in one minute and delete the temporary trigger when the function is called, but leave the daily trigger active. Pseudocode probably explains it better...
function run_job_via_trigger(trigger) {
if(trigger === temporary trigger) {
// If this is a 'temporary' trigger that was created to
// run the job after the first call then delete it.
// This must not delete the daily trigger that makes the
// first call to the function.
// If I check the UID of the trigger here I still
// would need to know which trigger is the daily trigger
// and which is a temporary trigger!
ScriptApp.deleteTrigger(trigger)
}
const job_finished = job_that_takes_several_calls_to_complete();
if(job_finished === false) {
// Create a temporary time-driven trigger to call this
// function again in 1 minute.
ScriptApp.newTrigger('run_job_via_trigger').timeBased().everyMinutes(1).create();
}
}
function job_that_takes_several_calls_to_complete() {
// This function often takes more time to complete than
// the maximum time allowed for scripts to run. It keeps
// track of its execution time and returns true if it has
// finished doing what it needs to do or false if it
// needs more time and should be called again.
return finished ? true : false;
}
How can I detect which time-driven trigger has called the run_job_via_trigger function so I can delete the temporary trigger(s) but not the daily trigger?
The spreadsheet has several other time-driven triggers so simply deleting all triggers at the end and creating a new daily trigger is not, as far as I can tell, an acceptable solution.
When your function is called with a trigger, it receives a trigger event as its parameter. You can check the trigger UID for example, like so:
function doWhatever(e) {
if(e.triggerUid === 'trigger_id') {
// do something
} else {
// do something else
}
}
UPDATE
There are a couple of ways to know which triggers are running.
The best-case scenario, when you created a trigger, you stored its ID somewhere, like user properties and then you always know when it's running. But I guess you haven't done that.
In your case you might want to do some manual work. Go to the triggers page, find your recurring trigger, click on the three dots on the right and select "Executions". You will then see the trigger ID in the filter:
Now you can use that in your code to check whether it's your recurring trigger or your temporary trigger.

mysql event scheduler displays error: 'database not selected'

I apologize if this question seems mundane, but I couldn't find a suitable answer on the web. It's my first time working with event scheduler in phpmyadmin and I think I've got the syntax down:
CREATE EVENT `zero_my_column`
ON SCHEDULE EVERY
1 MINUTE STARTS '2017-07-21 19:02:00'
DO BEGIN
UPDATE
`runners`.`run`
SET
`weekly_total` = 0
WHERE
`weekly_total` <> 0
END;
However, when I run this query in my localhost/phpmyadmin page I get the following error: #1046 - No database selected. runners is my database and run is my table. I am trying to reset the weekly_total column to zero. I'm wondering if the error arises because I need to enter validation to access the database, but I'm unsure. Any tips/hints are much appreciated.
I finally figured it out:
To create an event
Go into phpmyadmin
go to the database you will schedule the event for
click on Events tab
Then enter the script to schedule the event

Create a mysql event with condition

I currently have a mysql database where I keep data about rented books. When somebody rents a book, I save on the database an expiration date.
So, I want to create an mysql event to update the book status when it is not returned in time. How can I create this event?
Here's what I'm trying:
CREATE EVENT `EXPIRAR` ON SCHEDULE EVERY 1
HOUR STARTS '2016-07-16 00:00:00.000000' ENDS '2018-07-29
00:00:00.000000' ON COMPLETION NOT PRESERVE ENABLE DO DO IF
mtl_aluguel.dataLimite=CURRENT_DATE THEN
UPDATE mtl_aluguel SET mtl_aluguel.estado='Expirou'
END IF
But I'm getting Syntax errors on the IF.
mtl_aluguel is the name of my table.
Like #Drew said, my event doesnt make any sense. So I updated it:
CREATE EVENT `EXPIRAR` ON SCHEDULE EVERY 1
HOUR STARTS '2016-07-16 00:00:00.000000' ENDS '2018-07-29
00:00:00.000000' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE mtl_aluguel SET mtl_aluguel.estado='Expirou' WHERE mtl_aluguel.dataLimite = CURDATE() AND mtl_aluguel.estado!= 'Expirou'

How to configure an event mysql to run every day from monday to friday?

I have this code
CREATE EVENT daily_production
ON SCHEDULE EVERY 1 DAY STARTS '2015-11-08 23:45:00'
DO CALL sp_produccionDiaria();
Please help me, I want to run this code every day from monday to friday, thanks.
Have you tried from event tutorial, Use delimiters, also ON COMPLETION PRESERVE
delimiter |
CREATE EVENT daily_production
ON SCHEDULE EVERY 1 DAY
STARTS '2015-11-08 23:45:00'
ON COMPLETION PRESERVE
DO
BEGIN
CALL sp_produccionDiaria();
END |
delimiter ;
Or else, Write shell script on that MySQL event and then create a cron job
00 23 * * Mon,Tue,Wed,Thu,Fri /path/mysql_event.sh