mysql停止计划事件,创建并运行第二个事件后,mysql计划的事件将停止工作

在MySQL数据库中,作者遇到了一个问题:当创建多个事件调度(bots)来模拟用户投票时,只有一个事件能正常工作,而其他事件会受到影响。问题在于,当第二个事件被启用时,第一个事件的调度会停止。通过更改事件的调度方式,从一次性事件改为周期性事件并在事件体内部调整,作者找到了解决方法,使得多个事件可以独立并行工作,不受彼此影响。
摘要由CSDN通过智能技术生成

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:

d8055a74516ba6d35724b10bb542b244.png

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:

169d1ea62b291dcbf6b2c1d7f73cb11e.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值