Class: Fairway::Sidekiq::BasicFetch

Inherits:
Sidekiq::BasicFetch
  • Object
show all
Defined in:
lib/fairway/sidekiq/basic_fetch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BasicFetch

Returns a new instance of BasicFetch.



6
7
8
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 6

def initialize(options)
  @queues = options[:queues].map { |q| "queue:#{q}" }
end

Instance Attribute Details

#queuesObject (readonly)

Returns the value of attribute queues.



4
5
6
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 4

def queues
  @queues
end

Instance Method Details

#queues_cmdObject



42
43
44
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 42

def queues_cmd
  @queues.shuffle.uniq
end

#retrieve_work(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 10

def retrieve_work(options = {})
  options = { blocking: true }.merge(options)

  ::Sidekiq.logger.debug "#{self.class.name}#retrieve_work #{queues_cmd}"

  work = ::Sidekiq.redis do |conn|
    script = <<-SCRIPT
      for i = 1, #KEYS do
        local work = redis.call('rpop', KEYS[i]);

        if work then
          return {KEYS[i], work};
        end
      end

      return nil;
    SCRIPT

    conn.eval(script, queues_cmd)
  end

  if (work)
    ::Sidekiq.logger.debug "#{self.class.name}#retrieve_work got work"
    work = UnitOfWork.new(*work)
  else
    ::Sidekiq.logger.debug "#{self.class.name}#retrieve_work got nil"
    sleep 1 if options[:blocking]
  end

  work
end