Search code examples
ruby-on-railsactionmailersidekiq

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?


Solution

  • 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