Class: Fairway::Sidekiq::BasicFetch
- Inherits:
-
Sidekiq::BasicFetch
- Object
- Sidekiq::BasicFetch
- Fairway::Sidekiq::BasicFetch
- Defined in:
- lib/fairway/sidekiq/basic_fetch.rb
Instance Attribute Summary collapse
-
#queues ⇒ Object
readonly
Returns the value of attribute queues.
Instance Method Summary collapse
-
#initialize(options) ⇒ BasicFetch
constructor
A new instance of BasicFetch.
- #namespace ⇒ Object
- #queues_cmd ⇒ Object
- #retrieve_work(options = {}) ⇒ Object
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() @queues = [:queues].map { |q| "queue:#{q}" } end |
Instance Attribute Details
#queues ⇒ Object (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
#namespace ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 47 def namespace @namespace ||= begin namespaces = [] ::Sidekiq.redis do |conn| wrapper = conn while defined?(wrapper.redis) namespaces.unshift(wrapper.namespace) wrapper = wrapper.redis end namespace = namespaces.join(":") namespace += ":" unless namespace.blank? namespace end end end |
#queues_cmd ⇒ Object
43 44 45 |
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 43 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 41 |
# File 'lib/fairway/sidekiq/basic_fetch.rb', line 10 def retrieve_work( = {}) = { blocking: true }.merge() ::Sidekiq.logger.debug "#{self.class.name}#retrieve_work #{queues_cmd}" work = ::Sidekiq.redis do |conn| script = " -- take advantage of non-blocking scripts\n for i = 1, #KEYS do\n local work = redis.call('rpop', KEYS[i]);\n\n if work then\n return {KEYS[i], work};\n end\n end\n\n return nil;\n SCRIPT\n\n conn.eval(script, queues_cmd.map{|q| \"\#{namespace}\#{q}\"})\n end\n\n if (work)\n ::Sidekiq.logger.debug \"\#{self.class.name}#retrieve_work got work\"\n work = UnitOfWork.new(*work)\n else\n ::Sidekiq.logger.debug \"\#{self.class.name}#retrieve_work got nil\"\n sleep 1 if options[:blocking]\n end\n\n work\nend\n" |