定期计时器进行可靠计时

定期计时器进行可靠计时

本文介绍了使用 EventMachine 定期计时器进行可靠计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是建立一个系统,每 10 分钟向 37,500 个城市播放一次广告.所有城市的数据库查询、计算和 AMQP IO 大约需要 5 分钟.

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.

代码结构大致如下:

EventMachine.add_periodic_timer(10 minutes) do
  the_task_that_takes_five_minutes
end

我发现,即使计时器设置为 10 分钟的间隔,即使任务花费不到 10 分钟,命令也会以 15 分钟的间隔触发(完成任务所需的时间 + EM 周期).)

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

如果我们假设任务永远不会超过 10 分钟,那么无论任务处理时间如何,我将如何确保计时器的周期始终与上次运行相距 10 分钟?

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 基本上似乎是在任务运行之后设置下一个计时器,而不是之前.

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

我尝试了一个简单的 EM.defer 围绕任务批处理本身.我认为这会打开设置下一个计时器的线程,但这并不能解决问题.

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

我知道我可以在 Javascript 中做那种事情,因为计时器不会在 do_stuff 调用堆栈内执行.EventMachine 是这样吗?

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?

推荐答案

这只是一个想法,但也许计时器可以触发函数只是为了广播广告,而不是进行所有计算.

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

我不确定推迟计算是否可以避免 EM 等待它.

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

这篇关于使用 EventMachine 定期计时器进行可靠计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:15