问题描述
我正在构建一个Tinder风格的应用程序,用户可以在其中滑动事件.
I'm building a Tinder style app where users can swipe through events.
当前,我有一个后台作业,每当用户滑动事件时,都会触发该作业.随着用户每分钟浏览20个事件,我正在创建大量的后台作业:
Currently, I have a background job that is being fired any time a user swipes an event. With users swiping through 20 events per minute, I'm creating a ton of background jobs:
worker.rb
worker.rb
class Worker
include Sidekiq::Worker
def perform(user_id, newly_voted_event_id)
user = User.find(user_id)
event = Event.find(newly_voted_event_id)
events_to_rerank = event.similar.unvoted(user_id)
events_to_rerank.each do |e|
e.rank(user_id)
end
end
end
User.rb:
def recalculate_similar_events(event_id)
CalculateRelevantEventRankingsForUser.perform_async(self.id, event_id)
end
我想做的是每位用户每5分钟最多运行一次后台作业.因此,我将使用sidekiq-debounce
将我的后台作业更改为:
What I would like to do is run a maximum of one background job every 5 minutes per user. So I will use sidekiq-debounce
change my background job to:
def recalculate_similar_events(event_id)
CalculateRelevantEventRankingsForUser.perform_in(5.minutes, self.id, event_id)
end
我对宝石的作用感到困惑.它会在5分钟内自动将所有作业作为一个作业执行,还是只是在每5分钟窗口中执行第一个作业?
I'm confused about what the gem does. Does it automatically execute all the jobs as one job in 5 minutes or does it just execute the first job in each 5 minute window?
推荐答案
和以往一样,如果您不确定找出最简单的方法就是查看gem代码:
As always in questions like this one if you not sure simplest way to figure out is to look into gem code:
# Reschedule the old job to when this new job is scheduled for
# Or yield if there isn't one scheduled yet
jid = scheduled_jid ? reschedule(scheduled_jid, @msg['at']) : yield
因此,当您要求进行另一次运行时,如果一项工作已经在这里,则会将其重新安排为5分钟后.
so if one job is already here when you call for another run, it'll be rescheduled to 5 minutes later.
更新:从评论看来,您正在寻找 sidekiq-rate-limiter 不会反跳.
Update:As it seems from comments you looking for sidekiq-rate-limiter not debounce.
这篇关于轨道与Sidekiq:对反跳感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!