Where developers come to connect, share, build and be inspired.

75

Free background jobs on heroku

9301 views

Rails + Sidekiq with a single heroku dyno

Sidekiq is a gem for background jobs and a great alternative to Resque or DelayedJob. To set it up with a rails app on heroku normally requires an extra worker process.

You can avoid the need for an extra dyno by using unicorn as your webserver. Unicorn lets you spawn your sidekiq workers within your web dyno.

Sidekiq your web dyno!

if your Profile looks like this

web: bundle exec unicorn -p $PORT -E $RACK_ENV -c ./config/unicorn.rb

then just add something like the following to your config/unicorn.rb

before_fork do |server, worker|
   @sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")
end

Redis connection limitation

If you are using sidekiq with heroku's redis-to-go nano addon you might also need to tweak you sidekiq concurrency and connection size to not max out nano's 10 connections limit. (see this blog post for details)

Here are some settings I use on a small app in production on heroku with three unicorn worker processes, sidekiq concurrency set to two (see bundle exec sidekiq -c 2 above) and redis-to-go's nano plan. I did not really fiddle around with it much - it works without problems so far for me.

config/unicorn.rb

worker_processes 3
after_fork do |server, worker|
  Sidekiq.configure_client do |config|
    config.redis = { :size => 1 }
  end
  Sidekiq.configure_server do |config|
    config.redis = { :size => 5 }
  end
end

Alternative

You can also use something like sucker_punch. It does all the work within your rails process itself whereas Sidekiq runs as a separate long-running process, independent from your Rails process.

Comments

  • Profile_picture_may_2012
    jalada

    I actually implemented it. Time will tell if it works for sure. This kills the parent process as well, which on Heroku will bounce your entire dyno (which is what I wanted):

    @worker_pid = spawn('bundle exec sidekiq')
    t = Thread.new {
      Process.wait(@worker_pid)
      puts "Worker died. Bouncing unicorn."
      Process.kill 'QUIT', Process.pid
    }
    # Just in case
    t.abort_on_exception = true
    

    The idea of using Thread.new { Process.wait() } comes from the implementation of Process.detach, which is essentially the same.

  • Profile_picture_may_2012
    jalada

    How would you handle if the process dies? Heroku won't notice & bounce your dyno and Unicorn doesn't notice either.

  • 0_iykox7k9_md63p7nwxpmx2vz_fow3xxnwgkvx2lbyoomore9bs86eulwstynczysoxtqiwrzrqss
    arolave

    looks like it to me - here is my puma.rb using 2 workers. Not tested under load just yet - and make sure you have you don't hit your db and redis connection limits.

    environment ENV['RACK_ENV']
    workers 2
    threads 2,5 # 4-6
    preload_app!
    
    on_worker_boot do
      @sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")
    end
    

  • Blank-mugshot
    linjunpop
  • Blank-mugshot
    dommmel

    @linjunpop - I mentioned sucker_punch in the last paragraph "Alternatives". Anything particular you wanted to say?

  • Blank-mugshot
    linjunpop

    @dommmel Oh, Sorry. I guess I had Evernote an old version of this tip.

  • Blank-mugshot
    emas80

    Very nice! I was wondering if there was an alternative to the two dynos on heroku. I'll give a try

  • Blank-mugshot
    dommmel

    @jalada Good question. I guess you could use Process.waitpid in some way... haven't done it myself

  • Blank-mugshot
    dommmel

    @jalada Cool - please keep us updated on how it works out

  • Blank-mugshot
    miguelperez

    Hi guys, @jalada @dommmel did it work?

  • 1796458_10102115315742217_1239067443_n
    rpearce

    Hi, how can this be done with Puma? Concurrency noob here

  • Blank-mugshot
    dontfidget

    Thanks for the tip. You mentioned sucker punch as an alternative but am I right in thinking that with sucker punch, I wouldn't be able to start a job from a rake task (heroku scheduler, for instance) or some other scheduling job and run it in the background of one of the existing heroku dynos?

  • 0eeaaf2e8b6bead6a4b97c411c14ef24_normal
    matixmatix

    Does worker perform even when the Heroku app goes to sleep?

  • 0_ffbqyoyrgns4wo-s5inmye0bhnojiugs5apjywmr0podq0zdwdberdvza4e6eyit625zlaklqjxr
    leoromanovsky

    @matixmatix Set up the New Relic plugin to ping your server periodically and it will always be awake!

Add a comment