Class: Cloudtasker::Middleware::Chain
- Inherits:
-
Object
- Object
- Cloudtasker::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/cloudtasker/middleware/chain.rb
Overview
The class below was originally taken from Sidekiq. See: github.com/mperham/sidekiq/blob/master/lib/sidekiq/middleware/chain.rb
Middleware are callables configured to run before/after a message is processed. Middlewares can be configured to run on the client side (when jobs are pushed to Cloud Tasks) as well as on the server side (when jobs are processed by your application)
To add middleware for the client:
Cloudtasker.configure do |config|
config.client_middleware do |chain|
chain.add MyClientHook
end
end
To modify middleware for the server, just call with another block:
Cloudtasker.configure do |config|
config.server_middleware do |chain|
chain.add MyServerHook
chain.remove ActiveRecord
end
end
To insert immediately preceding another entry:
Cloudtasker.configure do |config|
config.client_middleware do |chain|
chain.insert_before ActiveRecord, MyClientHook
end
end
To insert immediately after another entry:
Cloudtasker.configure do |config|
config.client_middleware do |chain|
chain.insert_after ActiveRecord, MyClientHook
end
end
This is an example of a minimal server middleware:
class MyServerHook
def call(worker_instance, msg, queue)
puts "Before work"
yield
puts "After work"
end
end
This is an example of a minimal client middleware, note the method must return the result or the job will not push to Redis:
class MyClientHook
def call(worker_class, msg, queue, redis_pool)
puts "Before push"
result = yield
puts "After push"
result
end
end
Instance Method Summary collapse
-
#add(klass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware at the end of the list.
-
#clear ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Empty the list of middlewares.
-
#each(&block) ⇒ Object
Iterate over the list middlewares and execute the block on each item.
-
#empty? ⇒ Boolean
Checks if the middlware list is empty.
-
#entries ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Return the list of middlewares.
-
#exists?(klass) ⇒ Boolean
Checks if middleware has been added to the list.
-
#initialize {|_self| ... } ⇒ Chain
constructor
Build a new middleware chain.
-
#insert_after(oldklass, newklass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware after another middleware.
-
#insert_before(oldklass, newklass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware before another middleware.
-
#invoke(*args) ⇒ Object
Invoke the chain of middlewares.
-
#prepend(klass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware at the beginning of the list.
-
#remove(klass) ⇒ Object
Remove a middleware from the list.
-
#retrieve ⇒ Array<any>
Return a list of instantiated middlewares.
Constructor Details
#initialize {|_self| ... } ⇒ Chain
Build a new middleware chain.
76 77 78 79 |
# File 'lib/cloudtasker/middleware/chain.rb', line 76 def initialize @entries = nil yield self if block_given? end |
Instance Method Details
#add(klass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware at the end of the list.
116 117 118 119 |
# File 'lib/cloudtasker/middleware/chain.rb', line 116 def add(klass, *args) remove(klass) if exists?(klass) entries << Entry.new(klass, *args) end |
#clear ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Empty the list of middlewares.
201 202 203 |
# File 'lib/cloudtasker/middleware/chain.rb', line 201 def clear entries.clear end |
#each(&block) ⇒ Object
Iterate over the list middlewares and execute the block on each item.
86 87 88 |
# File 'lib/cloudtasker/middleware/chain.rb', line 86 def each(&block) entries.each(&block) end |
#empty? ⇒ Boolean
Checks if the middlware list is empty
182 183 184 |
# File 'lib/cloudtasker/middleware/chain.rb', line 182 def empty? @entries.nil? || @entries.empty? end |
#entries ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Return the list of middlewares.
95 96 97 |
# File 'lib/cloudtasker/middleware/chain.rb', line 95 def entries @entries ||= [] end |
#exists?(klass) ⇒ Boolean
Checks if middleware has been added to the list.
173 174 175 |
# File 'lib/cloudtasker/middleware/chain.rb', line 173 def exists?(klass) any? { |entry| entry.klass == klass } end |
#insert_after(oldklass, newklass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware after another middleware.
159 160 161 162 163 164 |
# File 'lib/cloudtasker/middleware/chain.rb', line 159 def insert_after(oldklass, newklass, *args) i = entries.index { |entry| entry.klass == newklass } new_entry = i.nil? ? Entry.new(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) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware before another middleware.
143 144 145 146 147 148 |
# File 'lib/cloudtasker/middleware/chain.rb', line 143 def insert_before(oldklass, newklass, *args) i = entries.index { |entry| entry.klass == newklass } new_entry = i.nil? ? Entry.new(newklass, *args) : entries.delete_at(i) i = entries.index { |entry| entry.klass == oldklass } || 0 entries.insert(i, new_entry) end |
#invoke(*args) ⇒ Object
Invoke the chain of middlewares.
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/cloudtasker/middleware/chain.rb', line 210 def invoke(*args) return yield if empty? chain = retrieve.dup traverse_chain = lambda do if chain.empty? yield else chain.shift.call(*args, &traverse_chain) end end traverse_chain.call end |
#prepend(klass, *args) ⇒ Array<Cloudtasker::Middleware::Chain::Entry>
Add a middleware at the beginning of the list.
129 130 131 132 |
# File 'lib/cloudtasker/middleware/chain.rb', line 129 def prepend(klass, *args) remove(klass) if exists?(klass) entries.insert(0, Entry.new(klass, *args)) end |
#remove(klass) ⇒ Object
Remove a middleware from the list.
104 105 106 |
# File 'lib/cloudtasker/middleware/chain.rb', line 104 def remove(klass) entries.delete_if { |entry| entry.klass == klass } end |
#retrieve ⇒ Array<any>
Return a list of instantiated middlewares. Each middleware gets initialize with the args originally passed to ‘add`, `insert_before` etc.
192 193 194 |
# File 'lib/cloudtasker/middleware/chain.rb', line 192 def retrieve map(&:make_new) end |