Search code examples
ruby-on-railssidekiq

Delaying a method chain using Sidekiq


I have a service object that has #call to execute an expensive operation. I would like to use Sidekiq to execute this operation.

I am using sidekiq and sidekiq-delay. The following gives me an error:

MyService.sidekiq_delay.new(arg).call

because when I call .sidekiq_delay and chain another method, it returns a string. How can I delay both the instantiation and #call on MyService using Sidekiq, without having to create another worker?

Edit

MyService.sidekiq_delay.new(arg).call gives me the following error

NoMethodError: undefined method `call' for "06139e957f4e73a7a84d9290":String 

MyService.new(arg).sidekiq_delay.call give me the following error:

NoMethodError: undefined method `sidekiq_delay' for #<MyService:0x007fa81d250a40>

Solution

  • You need to instantiate and then delay it.

    MyService.new(arg).delay.call
    

    EDIT:

    MyService.new(arg) should respond to find(id) method. If it don't, you need to write Custom Workers