Rails & Sidekiq: Confused about Debounce

£可爱£侵袭症+ 提交于 2019-12-25 08:57:10

问题


I'm building a Tinder style app where users can swipe through events.

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

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

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

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?


回答1:


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

so if one job is already here when you call for another run, it'll be rescheduled to 5 minutes later.

Update: As it seems from comments you looking for sidekiq-rate-limiter not debounce.



来源:https://stackoverflow.com/questions/40119004/rails-sidekiq-confused-about-debounce

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