class Sidekiq::Middleware::Chain
Public Instance Methods
Add the given middleware to the end of the chain. Sidekiq
will call `klass.new(*args)` to create a clean copy of your middleware for every job executed.
chain.add(Statsd::Metrics, { collector: "localhost:8125" })
@param klass [Class] Your middleware class @param *args [Array<Object>] Set of arguments to pass to every instance of your middleware
# File lib/sidekiq/middleware/chain.rb, line 122 def add(klass, *args) remove(klass) entries << Entry.new(@config, klass, *args) end
# File lib/sidekiq/middleware/chain.rb, line 165 def clear entries.clear end
Iterate through each middleware in the chain
# File lib/sidekiq/middleware/chain.rb, line 93 def each(&block) entries.each(&block) end
@return [Boolean] if the chain contains no middleware
# File lib/sidekiq/middleware/chain.rb, line 157 def empty? @entries.nil? || @entries.empty? end
# File lib/sidekiq/middleware/chain.rb, line 104 def entries @entries ||= [] end
@return [Boolean] if the given class is already in the chain
# File lib/sidekiq/middleware/chain.rb, line 152 def exists?(klass) any? { |entry| entry.klass == klass } end
A unique instance of the middleware chain is created for each job executed in order to be thread-safe. @param copy [Sidekiq::Middleware::Chain] New instance of Chain
@returns nil
# File lib/sidekiq/middleware/chain.rb, line 87 def initialize_copy(copy) copy.instance_variable_set(:@entries, entries.dup) nil end
Inserts newklass
after oldklass
in the chain. Useful if one middleware must run after another middleware.
# File lib/sidekiq/middleware/chain.rb, line 144 def insert_after(oldklass, newklass, *args) i = entries.index { |entry| entry.klass == newklass } new_entry = i.nil? ? Entry.new(@config, newklass, *args) : entries.delete_at(i) i = entries.index { |entry| entry.klass == oldklass } || entries.count - 1 entries.insert(i + 1, new_entry) end
Inserts newklass
before oldklass
in the chain. Useful if one middleware must run before another middleware.
# File lib/sidekiq/middleware/chain.rb, line 135 def insert_before(oldklass, newklass, *args) i = entries.index { |entry| entry.klass == newklass } new_entry = i.nil? ? Entry.new(@config, newklass, *args) : entries.delete_at(i) i = entries.index { |entry| entry.klass == oldklass } || 0 entries.insert(i, new_entry) end
Used by Sidekiq
to execute the middleware at runtime @api private
# File lib/sidekiq/middleware/chain.rb, line 171 def invoke(*args) return yield if empty? chain = retrieve traverse_chain = proc do if chain.empty? yield else chain.shift.call(*args, &traverse_chain) end end traverse_chain.call end
Identical to {#add} except the middleware is added to the front of the chain.
# File lib/sidekiq/middleware/chain.rb, line 128 def prepend(klass, *args) remove(klass) entries.insert(0, Entry.new(@config, klass, *args)) end
Remove all middleware matching the given Class @param klass [Class]
# File lib/sidekiq/middleware/chain.rb, line 110 def remove(klass) entries.delete_if { |entry| entry.klass == klass } end
# File lib/sidekiq/middleware/chain.rb, line 161 def retrieve map(&:make_new) end