创建并运行第二个事件后,mysql计划的事件将停止工作 [英] mysql scheduled event stops working as soon as 2nd event is created and running

查看:75
本文介绍了创建并运行第二个事件后,mysql计划的事件将停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想做的事情: 我正在做一个项目,用户可以对选项A或B进行投票.出于测试目的,我需要自动检查MySQL数据库中新问题并针对其中一个选项以50/50投票的机器人.我已经阅读了有关计划的事件的信息,但是现在添加多个事件后,我就陷入了困境.

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 ;

现在,此"bot1"在创建后30秒开始,然后将其自身重新安排为每5秒一次.只要该bot1是唯一运行的bot,一切都将完美运行.在phpmyadmin中,我可以刷新显示事件",并且可以看到执行时间"计划每5秒更改一次:

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:

问题出在哪里: 我需要更多这些机器人,因此我添加了另一个"bot2":

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 ;

此机器人称为"bot2",有两个区别:创建后仅2分钟启动,而且每30秒仅重新安排一次.

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.

这是我期望发生的事情: 我希望我的bot1能够像创建bot2之前一样继续工作.因此,bot1根本不必关心是否还有另一个bot,因为它们的名称不同.

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.

这是发生了什么 在创建bot2之后,bot1便立即停止了自身的重新计划.它停止触发并等待,直到bot2首次触发.即使这样,bot1也忘记了每5秒的实际调度,从现在开始,仅在每次执行bot2时才触发.因此,看起来bot2现在已经是真正的老板了,并告诉bot1什么时候触发:

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:

此行为是可重现的: 当我添加另一个"bot3"事件并告诉bot3在创建后仅5分钟后首次启动时,现在bot1和bot2等待这5分钟才启动.

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.

有人知道这是什么问题吗?感谢您的帮助:)

哦,是的,有一个原因我不使用每个"计划,而是以这种方式重新计划:)

推荐答案

我找到了一种解决方法:

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;

现在,该机器人在其体内进入睡眠状态达52分钟.使用此方法,甚至多个事件也可以按预期方式工作,并且彼此独立.

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 :)

这篇关于创建并运行第二个事件后,mysql计划的事件将停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆