Class: Sidekiq::Middleware::Chain
- Inherits:
-
Object
- Object
- Sidekiq::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/middleware/chain.rb
Instance Method Summary collapse
-
#add(klass, *args) ⇒ Object
Add the given middleware to the end of the chain.
- #clear ⇒ Object
- #copy_for(capsule) ⇒ Object
-
#each(&block) ⇒ Object
Iterate through each middleware in the chain.
-
#empty? ⇒ Boolean
If the chain contains no middleware.
- #entries ⇒ Object
-
#exists?(klass) ⇒ Boolean
(also: #include?)
If the given class is already in the chain.
-
#insert_after(oldklass, newklass, *args) ⇒ Object
Inserts
newklass
afteroldklass
in the chain. -
#insert_before(oldklass, newklass, *args) ⇒ Object
Inserts
newklass
beforeoldklass
in the chain. -
#prepend(klass, *args) ⇒ Object
Identical to #add except the middleware is added to the front of the chain.
-
#remove(klass) ⇒ Object
Remove all middleware matching the given Class.
- #retrieve ⇒ Object
Instance Method Details
#add(klass, *args) ⇒ Object
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" })
119 120 121 122 |
# File 'lib/sidekiq/middleware/chain.rb', line 119 def add(klass, *args) remove(klass) entries << Entry.new(@config, klass, *args) end |
#clear ⇒ Object
163 164 165 |
# File 'lib/sidekiq/middleware/chain.rb', line 163 def clear entries.clear end |
#copy_for(capsule) ⇒ Object
99 100 101 102 103 |
# File 'lib/sidekiq/middleware/chain.rb', line 99 def copy_for(capsule) chain = Sidekiq::Middleware::Chain.new(capsule) chain.instance_variable_set(:@entries, entries.dup) chain end |
#each(&block) ⇒ Object
Iterate through each middleware in the chain
84 85 86 |
# File 'lib/sidekiq/middleware/chain.rb', line 84 def each(&block) entries.each(&block) end |
#empty? ⇒ Boolean
Returns if the chain contains no middleware.
155 156 157 |
# File 'lib/sidekiq/middleware/chain.rb', line 155 def empty? @entries.nil? || @entries.empty? end |
#entries ⇒ Object
95 96 97 |
# File 'lib/sidekiq/middleware/chain.rb', line 95 def entries @entries ||= [] end |
#exists?(klass) ⇒ Boolean Also known as: include?
Returns if the given class is already in the chain.
149 150 151 |
# File 'lib/sidekiq/middleware/chain.rb', line 149 def exists?(klass) any? { |entry| entry.klass == klass } end |
#insert_after(oldklass, newklass, *args) ⇒ Object
Inserts newklass
after oldklass
in the chain. Useful if one middleware must run after another middleware.
141 142 143 144 145 146 |
# File 'lib/sidekiq/middleware/chain.rb', line 141 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 |
#insert_before(oldklass, newklass, *args) ⇒ Object
Inserts newklass
before oldklass
in the chain. Useful if one middleware must run before another middleware.
132 133 134 135 136 137 |
# File 'lib/sidekiq/middleware/chain.rb', line 132 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 |
#prepend(klass, *args) ⇒ Object
Identical to #add except the middleware is added to the front of the chain.
125 126 127 128 |
# File 'lib/sidekiq/middleware/chain.rb', line 125 def prepend(klass, *args) remove(klass) entries.insert(0, Entry.new(@config, klass, *args)) end |
#remove(klass) ⇒ Object
Remove all middleware matching the given Class
107 108 109 |
# File 'lib/sidekiq/middleware/chain.rb', line 107 def remove(klass) entries.delete_if { |entry| entry.klass == klass } end |
#retrieve ⇒ Object
159 160 161 |
# File 'lib/sidekiq/middleware/chain.rb', line 159 def retrieve map(&:make_new) end |