0

I am building a worker for a controller action, but sidekiq will not boot due to me calling params in the perform method. Any ideas on how to get this to work?

controller

def call_warrants_with_date_range
  CallLogWorker.perform_async(params[:call_log])
  redirect_to call_logs_path, notice: 'Calls were successfully made.'
end

worker

class CallLogWorker
  include Sidekiq::Worker

  def perform(params[:call_log])
    client         = Twilio::REST::Client.new TWILIO_ACCOUNT_SID, TWILIO_ACCOUNT_AUTH_TOKEN
    start_date         = params[:call_log][:warrant_start_date]
    end_date           = params[:call_log][:warrant_end_date]
    query = "SELECT people.id, warrants.warn_type, warrants.warn_date_issued, phone_numbers.phone_number 
    FROM people
    LEFT OUTER JOIN warrants ON people.id = warrants.person_id
    LEFT OUTER JOIN phone_numbers ON people.id = phone_numbers.person_id
    WHERE warrants.warn_date_issued BETWEEN ? AND ? AND warrants.warn_type = 'AW'"

    @numbers = CallLog.find_by_sql(["#{query}", start_date, end_date])
    @numbers.each do |dial|
      begin
       call = client.account.calls.create(
       :from => TWILIO_PHONE_NUMBER,
       :to => dial.phone_number,
       :url => 'http://twimlets.com/echo?Twiml=hello%20this%20is%20a%20test%20call%20please%20hang%20up&'
      )

       CallLog.create!({ phone: dial.phone_number, status: call.status, 
       warrant_start_date: start_date, warrant_end_date: end_date, person_id: dial.id})
       Note.create!({ body: call.status, person_id: dial.id })
    rescue Exception => e
       CallLog.create!({ phone: dial.phone_number, status: call.status, exception: e.to_s,
       warrant_start_date: start_date, warrantend_date: end_date, person_id: dial.id})
       Note.create!({ body: e.to_s, person_id: dial.id })
    end
  end
 end
end
Colton Seal
  • 379
  • 2
  • 14
  • I suggest you read [How do functions use hash arguments in Ruby](http://stackoverflow.com/questions/16576477/how-do-functions-use-hash-arguments-in-ruby) – Roman Kiselenko Feb 17 '15 at 16:12
  • You do not have access to the params inside of your worker... So you have to pass them as a argument to the perform_async method. – Joel Feb 17 '15 at 16:23

1 Answers1

8

In your worker:

def perform(params)
   start_date         = params[:call_log][:warrant_start_date]
   end_date           = params[:call_log][:warrant_end_date]
   ...etc
end

And then in your controller:

CallLogWorker.perform_async(params)

So you're parsing the hash params into the worker from the controller and then referring to it in your worker.

It's generally considered good practice to keep the data you pass into Sidekiq jobs as small as possible - see here for best practices. So you could go further and have:

In your worker:

def perform(start_date, end_date)
   ...job content
end

And in your controller:

CallLogWorker.perform_async(
  params[:call_log][:warrant_start_date],  
  params[:call_log][:warrant_end_date]
)
David Chan
  • 7,347
  • 1
  • 28
  • 49
Katherine
  • 171
  • 4