name_id(INT)(PRIMARY) name(VARCHAR(50)) add_time(DATETIME)
1 stuff 2015-10-22 10:55:41
2 stuff 2015-11-25 14:54:20
I have table main as shown. User can add new stuff into this table.I want a trigger event that:
runs 10 minutes after the new row has been added into main
gets name_id for that new name
I now i need a MySql Event rather than a trigger since i want to wait 10 minutes. However i couldn't understand how i am gonna trigger this new event for a specific row rather than a whole table. Thanks.
You can write a trigger as usual but first call the sleep function, providing the number of seconds to sleep for (600 in your case) as an argument.
Related
here is what I'd like to do:
I am working on a project where users can vote for either option A or B. For testing purposes, I need bots that automatically check for new questions in my MySQL database and vote 50/50 on either option. I've read about scheduled events, but I now I am stuck as soon as I add multiple events.
here is what I've done:
Since my bots do only work while there is just one bot (= one event) enabled but stops working as soon as a second bot (= a second event) gets enabled, I simplified the whole process. To demonstrate what the problem is, here is my first bot:
delimiter |
CREATE EVENT bot1
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 30 SECOND
ON COMPLETION PRESERVE
DO
BEGIN
## do something
## do something else
ALTER EVENT bot1
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 SECOND
ENABLE;
END |
delimiter ;
Now, this 'bot1' starts 30 seconds after it was created and then reschedules itself to every 5 seconds. As long as this bot1 is the only bot running, everything works perfectly. Within phpmyadmin, I can refresh 'show events' and can see that the 'execute at' schedule changes every 5 seconds:
what the problem is:
I need more of those bots, so I add another 'bot2':
delimiter |
CREATE EVENT bot2
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 2 MINUTE
ON COMPLETION PRESERVE
DO
BEGIN
# do something
# do something else
ALTER EVENT bot2
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 30 SECOND
ENABLE;
END |
delimiter ;
This bot is called 'bot2' and there is two differences: It starts only 2 minutes after creation, plus, it reschedules itself only every 30 seconds.
here is what I'd expect to happen:
I expect my bot1 to keep working like it did before the creation of bot2. So, bot1 shouldnt care at all that there is another bot involved, since they have different names.
here is what does happen though:
Right after creation of my bot2, bot1 stops rescheduling itself. It stops firing and waits until bot2 fired for the first time. Even then, bot1 forgot about its actual schedule of every 5 seconds and, from now on, fires only each time bot2 is executed. So, it looks like bot2 somehow is the real boss now and tells bot1 when to fire:
This behaviour is reproducable:
When I add another 'bot3' - event and tell bot3 to fire first time only 5 minutes after creation, now bot1 and bot2 wait for those full 5 minutes before they fire.
Does anyone have a clue what the problem is? Would appreciate any help :)
Oh, and yes, there is a reason why I dont use the 'every' schedule but rather reschedule it this way :)
I found a workaround:
Instead of using one-time-events and re-enable them with a new starting time (which works perfectly as long as just one event is involved), I now create recurrent events and alter those within their body. So, for example, using
ALTER EVENT `bot1`
ON SCHEDULE EVERY 52 MINUTE;
within its body, the bot now goes to sleep for 52 minutes. With this method, even multiple events work as expected and independent from each other.
Still dont know what the problem with the first attempt is, but having a working solution is most important :)
I want to create trigger in zabbix which should get executed when the value for particular item is constant for the period of 5 min.
exa:- If value of item x is 20 and it remained as 20 for 5 min, then trigger should get executed. I don't want to use avg() function since it is uncertain for some of the situations. Does anyone has any idea how to create trigger for above?
Try this: {host:item.min(5m)}={host:item.max(5m)}.
I think you can use
{Java CPU Utilization:item.min(10m)}>90
Here what will happen is when minimum value in last 10min is above 90 then it will trigger. It simply means value of item was constantly above 90 in last 10 min.
I hope it serves your purpose.
I want to initiate an action after 7 days from the day a specific field in my database table is updated.
I can use trigger to trigger an event on update of field in the database table. But how can I make it wait for 7 days. I was looking for scheduling an Event but that can only be scheduled for specific time. Is there any way to dynamically set the schedule time?
There are 2 ways to do it:
You store in a timestamp field when the record was last updated. You schedule a regular event to be run every couple of hours. The event's code scans the table in question and does whatever needs to be done for those records those timestamp is between the lust run of the even and the current time.
If you have lots of records you need to handle such events, then this is the recommended approach. The drawback is that timing of the event may not be fully accurate, depending on how often you schedule this event to run.
Every time you update the record you create an event specifically for that record in a trigger or stored procedure that triggers in 7 days time.
This approach gives you absolute accuracy over when an event is triggered, however, if you have a large number of changes, then this solution does not scale well.
An alternative solution could be to record the time when the change was done, but instead of event triggering base your code on user interaction. So, if the user, whose data has been updated logs on then you notify him about what you want regarding those updates that were done more than 7 days ago and the events were not seen by the user.
I'm wondering if there's a way to trigger an event into a database based on the age of the row entry. Based in a datetime field I will need to retrieve the entrys that match an exact age, but I don't want to stress the database. So I need the database to create an event in where if the field is in the database when the time comes, it will send a message or something like that.
Examples:
I have an entry that was created 23:59 hours ago. I want to trigger an event when the field reaches 24:00 hours.
I have a backend which should receive a command each time an entry becomes 1 day old.
When you INSERT the row (or otherwise set/change the time), create a one-time EVENT for 24 hours hence.
This would have to be in your application code, since "Events cannot be created, dropped or altered by another stored program, trigger or event."
I have the following problem to solve. I need rows inserted into a "reservations" table to, upon insertion, set a timer for themselves and then check a flag within this newly created row some minutes later to see if it has changed from "pending" to "completed" (which would be caused by user action in the intervening period) and if still "pending" to remove themselves from the table.
The idea here is that people are making reservations and the act of beginning the reservations process adds this row, however, if they fail to complete the purchase over a period of time I want to remove the rows to make the reservations (of which there is a finite amount) available to other consumers.
So, I've been looking at events and triggers and I get the concept for both, but what I'm failing to find is a way for the trigger to pass *this row's id to the event so that when the event fires it only looks at the relevant row because I don't want it to notice *all the rows that might be "pending" since there may have been newly created "pending" rows by other consumers for other reservations in the intervening period, and I obviously don't want to mess with those until their respective timers have elapsed.
So... what I am hoping for (in pseudo) is...
/*EVENT*/
CREATE EVENT IF NOT EXISTS delete_abandoned_pending_purchase
ON SCHEDULE AT CURRENT_TIMESTAMP + 5 minutes
DO
delete from tickets where state = 'PENDING' and id = [MY ROW ID]
and then...
/*trigger*/
CREATE TRIGGER remove_if_unused
AFTER INSERT ON `tickets` FOR EACH ROW
begin
[call delete_abandoned_pending_purchase with row_id MY_NEW_ROW_ID]
end
I'm guessing maybe I need to make a stored procedure that takes a parameter and then pass that row ID as the param? Or perhaps there's a more straight forward way... I'm just failing to find the syntax for this and would love some guidance. Obviously I can handle this in the business logic that wraps this data interaction, but felt that this was a more elegant approach.
[EDIT]
reading more about this
"There is no way to pass parameters directly to or from events; however, it is possible to invoke a stored routine with parameters within an event".
But the suggestion there is to call a stored procedure and pass it a param. However, my problem is that I don't see how to get *at the row.id in the event to pass to the stored proc. I feel like I must be missing something obvious... how can events not have access to specific row ids?
[EDIT EDIT]
so, based this I'm sensing that this is actually not doable in mySQL... that's a bummer and also quite surprising. Seems like a really obvious thing to want to do.
I'll leave the question open and see if anyone chimes in with a clever alternative.
I would recommend you do this via a script, less complexity and more control. Something like below:
MaxSleep=300 # In seconds SleepTime=MaxSleep
while (1) {
sleep SleepTime; delete from TheTable where reserved = 'pending' and the_timestamp >= Current_Timestamp; SleepTime='mysql
'select the_timestamp from TheTable where reserved = 'pending' order
by the_timestamp limit 1"
if SleepTime is null then SleepTime= MaxSleep
}
You could just do an event that checks against the whole table, should be fast if it is indexed correctly and then the business logic is in the DB. Perhaps use a minute as the check then max pending transaction is 6 Minutes if you have a 5 minute timeout.
CREATE EVENT delete_abandoned_pending_purchase
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 Minutes
DO
BEGIN
delete from TheTable where reserved = 'pending' and the_timestamp >= Current_Timestamp;
END