Module: Faktory::Middleware
- Defined in:
- lib/faktory/middleware/chain.rb
Overview
Middleware is code configured to run before/after a job is processed. It is patterned after Rack middleware. Middleware exists for the client side (pushing jobs to a queue) as well as the worker side (when jobs are actually executed).
To add middleware to run when a job is pushed to Faktory:
Faktory.configure_client do |config|
config.push_middleware do |chain|
chain.add MyClientHook
end
end
To run middleware when a job is executed within the worker process, add it to the worker_middleware:
Faktory.configure_worker do |config|
config.worker_middleware do |chain|
chain.add MyServerHook
chain.remove ActiveRecord
end
end
To insert immediately preceding another entry:
Faktory.configure_client do |config|
config.middleware do |chain|
chain.insert_before ActiveRecord, MyClientHook
end
end
To insert immediately after another entry:
Faktory.configure_client do |config|
config.middleware do |chain|
chain.insert_after ActiveRecord, MyClientHook
end
end
This is an example of a minimal worker middleware:
class MyServerHook
def call(worker_instance, job)
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(job, conn_pool)
puts "Before push"
result = yield
puts "After push"
result
end
end