使用Sidekiq删除请求

juud5qan  于 2022-10-15  发布在  Ruby
关注(0)|答案(1)|浏览(111)

我是Ruby on Rails和Sidekiq的新手。我想将此删除请求设置为在Sidekiq队列中完成,但我不知道如何将其发送到Perform方法,我正在将Book模型发送到Perform方法

我的控制器操作码

def destroy
    BaseWorkerJob.perform_async(Book)
end

我的BaseWorkerJob类代码

class BaseWorkerJob
   include Sidekiq::Job
   sidekiq_options retry:0

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

SideKiq错误

enter image description here
Ruby 3.1.2
Rails 7.0.4

0x6upsns

0x6upsns1#

您可以将模型名称和对象ID发送给工作器

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

试试看!

相关问题