Reliable timing with EventMachine periodic timers

半世苍凉 提交于 2019-12-08 07:51:29

问题


My objective is to have a system that broadcasts an ad every 10 minutes for 37,500 cities. It takes around 5 minutes do the DB queries, calculations, and AMQP IO for all cities.

The code is roughly structured like:

EventMachine.add_periodic_timer(10 minutes) do
  the_task_that_takes_five_minutes
end

What I'm finding is that even though the timer is set for 10 minute intervals and even though the task takes less than ten minutes the command fires in 15 minute intervals (the time it takes to complete the task + the EM period.)

If we make the assumption that the task will never take longer than 10 minutes, how would I go about ensuring that the period of the timer is always exactly 10 minutes from the previous run regardless of the task processing time?

EM basically seems to set the next timer after the task is run, not before.

I've tried a simple EM.defer around the task batch itself. I assumed this would open up the thread for setting the next timer but this doesn't solve the issue.

Can I away with the following?

def do_stuff
  EventMachine.add_timer(10 minutes) do
    do_stuff
  end

  the_task_that_takes_five_minutes
end

do_stuff

I know I can do that sort of thing in Javascript because the timer wouldn't execute inside the do_stuff call stack. Is this true for EventMachine?


回答1:


This is just an idea, but maybe the timer could fire the function just to broadcast the ad, not to make all the calculations.

EventMachine.add_periodic_timer(10 minutes) do
  ad_broadcasting calculations
  EM.defer
    calculations = Calc.new
  end
end

I'm not sure if deferring the calculations there would avoid EM from waiting for it.



来源:https://stackoverflow.com/questions/14975140/reliable-timing-with-eventmachine-periodic-timers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!