0

I am new to Ruby on Rails and Sidekiq. I want to set this delete request to be done in Sidekiq queue and I don't know how to send it to the perform method, I am sending the Book model to the perform method

My controller Action code

def destroy
    BaseWorkerJob.perform_async(Book)
end

My BaseWorkerJob class code

class BaseWorkerJob
   include Sidekiq::Job
   sidekiq_options retry:0

   def perform(book)
   # Do something
     book.find(params[:id]).destroy!
     sleep 15
   end
end

SideKiq Error

enter image description here

ruby 3.1.2
Rails 7.0.4

1 Answers1

1

You can send the model name and object id to the worker

def destroy
  BaseWorkerJob.perform_async(Book.to_s, params[:id])
end
class BaseWorkerJob
  include Sidekiq::Job
  sidekiq_options retry: 0

  def perform(klass_name, object_id)
    klass_name.constantize.find(object_id).destroy!
  end
end

Try it out!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • can you pls refer to me a good reference for how to use SideKiq with restful APIs? as I want to try this also in the Post Request but still have no idea how to do it. @rajdeep-singh – Mohamed Medhat Oct 05 '22 at 18:54