Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Object to Sidekiq

I tried passing an small object to sidekiq, but it converts it to a hash. This object is a tracking object for mixpanel. I also tried accessing session variables in my Worker but they aren't available there either.

Thoughts?

Controller

MixpanelWorker.perform_async(@mixpanel, 'person', mixpanel_distinct_id, cleaned_params)

MixpanelWorker

  def perform(mixpanel_object, type_of_send, distinct_id, event)
    case type_of_send
    when 'person'
      mixpanel_object.people.set(distinct_id, event)
    when 'event'
      mixpanel_object.track(distinct_id, event)
    end
  end
like image 467
Elijah Murray Avatar asked May 13 '14 08:05

Elijah Murray


People also ask

How do I run on Sidekiq?

To run sidekiq, you will need to open a terminal, navigate to your application's directory, and start the sidekiq process, exactly as you would start a web server for the application itself. When the command executes you will see a message that sidekiq has started.

How Sidekiq uses Redis?

Configuring SidekiqThe server is the sidekiq process which pulls jobs from Redis. That means when deploying, our web dynos in Heroku will use a max of size number of connections to push jobs to Redis, no matter how many threads they have.

How does Sidekiq queue work?

Sidekiq server process pulls jobs from the queue in Redis and processes them. Like your web processes, Sidekiq boots Rails so your jobs and workers have the full Rails API, including Active Record, available for use. The server will instantiate the worker and call perform with the given arguments.

What is Sidekiq in Ruby?

Sidekiq is an open source job scheduler written in Ruby. It's important to be aware that Sidekiq by default doesn't do scheduling, it only executes jobs. The Enterprise version comes with scheduling out of the box.


1 Answers

Best way to approach the problem you are trying to solve is to save the object data in a database and then pass the id of @mixpanel to your MixpanelWorker job like so:

@mixpanel = Mixpanel.create [your parameters]
MixpanelWorker.perform_async @mixpanel.id, 'person', mixpanel_distinct_id, cleaned_params

Then your MixpanelWorker would handle the job like this:

def perform(mixpanel_id, type_of_send, distinct_id, event)
  mixpanel_object = Mixpanel.find mixpanel_id

  case type_of_send
   when 'person'
     mixpanel_object.people.set(distinct_id, event)
   when 'event'
     mixpanel_object.track(distinct_id, event)
  end
end

Also read the link that Mike Perham posted, I think he may know a little about Sidekiq ;).

like image 84
Bill Watts Avatar answered Sep 20 '22 07:09

Bill Watts