Search code examples
rubysinatraeventmachinesidekiq

Using Eventmachine http request in a Sidekiq worker


So lets say I have a sidekiq process that sends off a http post request that I don't want to wait for. I don't want this to be a blocker on the speed of the workers.

One idea I have is to use this simple sample code for EventMachine Http Request

  EventMachine.run do
    http = EventMachine::HttpRequest.new("http://www.example.com").post :options => {...}
    http.callback do
      puts "got a response"
      puts http.response
      EventMachine.stop
    end
    puts "worker finished"
  end

so lets assume my worker process finishes before the callback is called. What will happen here? does this mean the pointer to the call back will fail? I'd like to understand the flow of control here.


Solution

  • Depending on what you need:

    1. You want to utilize CPU
      Sidekiq workers are very lightweight. You can run more of them to utilize CPU while waiting responce.
    2. You want workers to finish faster.
      You can enqueue each request to be proccessed by different worker. It will be like next_tick() in EM. I'm excited about Sidekiq and Celluloid because it changes the way we think. http://www.slideshare.net/KyleDrake/hybrid-concurrency-patterns?utm_source=rubyweekly&utm_medium=email