Our team recently has converted all our background jobs from delayed job to Sidekiq. There are many things that we have learned along the way. One of the useful tools is to utilize sidekiq middleware to manipulate the job when pushing the job to a redis server or right before the server executing the job.
For instance, we have one use case that we'd need to have an owner_id
value injected into the arguments when pushing to the job. But when executing the job, we do not need that any more.
In config/initializers/sidekiq.rb
we add this server middleware block as:
class SidekiqServerMiddleware
def call(_worker, job, _queue)
# eject owner_id arg
job['args'] = job['args'][0].filter{|j| !j['owner_id']} if job['args'] && job['args'][0].is_a?(Array)
yield
end
end
And then we need to add the middleware config to the Sidekiq server config:
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add SidekiqServerMiddleware
end
end
Now the Sidekiq server knows that before execute the jobs, middleware call will be run first which is to remove owner_id
.
In the end, we'd like to collect all the error messages that occurred during the middleware process, so we send the those messages to error message service provider (we use AppSignal here).
class SidekiqServerMiddleware
def call(worker, job, queue)
begin
yield
rescue => error
Appsignal.send_error(error)
raise
end
end
end