I've created a module:
module DomainSystem
def set_db
@domain = request.domain
...
end
...
end
Which I've included in a Worker:
class InvoiceSchedulerWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
include DomainSystem
recurrence { daily }
def perform
@var = set_db
...
end
Now whenever I call this schedule worker I get an error:
2015-09-29T10:04:57.108Z 7998 TID-ow95f5gng WARN: {"class"=>"InvoiceSchedulerWorker", "args"=>[], "retry"=>true, "queue"=>"default", "jid"=>"557fb942292b0d7e72043d74", "created_at"=>1443438228.0314932, "enqueued_at"=>1443521097.099235, "error_message"=>"undefined local variable or method `request' for #<InvoiceSchedulerWorker:0x007fc15a307500>", "error_class"=>"NameError", "failed_at"=>1443521097.103601, "retry_count"=>0}
2015-09-29T10:04:57.108Z 7998 TID-ow95f5gng WARN: NameError: undefined local variable or method `request' for #<InvoiceSchedulerWorker:0x007fc15a307500>
2015-09-29T10:04:57.108Z 7998 TID-ow95f5gng WARN: /Library/WebServer/Documents/ruby/myjarvis/lib/domain_system.rb:50:in `set_db'
Why is that?
I thought the request
variable was available throughout the whole Rails app...
Sidekiq is not part of your Rails app. It's executed as a separate process so there is no way for Sidekiq to know details of your request.
Even worse, you should pass only "simple" argument values to Sidekiq worker, while those arguments are stored as JSON string in Redis database.
So there is no way to share Rails state with Sidekiq, except passing all required parameters manually.