简体   繁体   中英

How to get params from controller into Sidekiq worker (rails)

I have certain actions in my app, and when they occur mailers are sent out.

My actions and mailers and everything work just fine. However, I'm trying to pass this job over a sidekiq worker.

My workers in the app are working fine, but I'm having issues getting my mailers to work via this workers.

Here's my worker

class SendMailerWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform
    @candidate = Candidate.find(params[:id])
    @candidate.destroy
    TestMailer.send_request()
  end

end

And here's my controller

  def destroy
    SendMailerWorker.perform_async
  end

Yet this set up doesn't work, I'm getting the following error: NameError: undefined local variable or method `params'

So the question is, how do I access params in the worker?

Just pass your parameters from controller to sidekiq worker

class SendMailerWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(candidate_id)
    @candidate = Candidate.find(candidate_id)
    @candidate.destroy
    TestMailer.send_request()
  end
end
  def destroy
    SendMailerWorker.perform_async(params[:id])
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM