Sucker Punch Your Background Jobs – Brandon Hilkert



Sucker Punch Your Background Jobs – Brandon Hilkert

1 0


background_jobs-presentation

A reveal.js presentation for Delaware.rb about Ruby background processing libraries.

On Github brandonhilkert / background_jobs-presentation

Sucker Punch Your Background Jobs

Brandon Hilkert

http://brandonhilkert.com @brandonhilkert

Why am I talking about this?

girl_friday

config/initializers/girl_friday.rb

EMAIL_QUEUE = GirlFriday::WorkQueue.new(:email) do |msg|
  UserMailer.registration_email(msg).deliver
end
          

Do work

EMAIL_QUEUE.push(:email => @user.email, :name => @user.name)
          

girl_friday

Pros

  • Runs within a web process
  • Configurable persistence with Redis
  • Basic Web UI

Cons

  • Semi-complicated syntax
  • Actor pattern extracted from Rubinius

Delayed Job

Synchronous

Post.find(9).send_to_twitter!
          

Asynchronous

Post.find(9).delay.send_to_twitter!
          

Do work

rake jobs:work
          

Delayed Job

Pros

  • Simple to setup
  • Uses existing data store
  • Simple syntax

Cons

  • Requires additional worker
  • Could load master DB
  • No built-in Web UI
  • Encourages serializing instances

Resque

Asynchronous

class Archive
  @queue = :file_serve

  def self.perform(repo_id, branch = 'master')
    repo = Repository.find(repo_id)
    repo.create_archive(branch)
  end
end
          
Resque.enqueue(Archive, self.id, branch)
          

Do work

QUEUE=file_serve rake resque:work
          

Resque

Pros

  • Built-in Web UI
  • Scalable

Cons

  • Redis dependency
  • Requires separate worker processes

Celluloid

Celluloid

Celluloid is a concurrent object oriented programming framework for Ruby which lets you build multithreaded programs out of concurrent objects just as easily as you build sequential programs out of regular objects

Celluloid

Celluloid

DEMO

Sucker Punch

config/initializers/sucker_punch.rb

SuckerPunch.config do
  queue name: :log_queue, worker: LogWorker, size: 10
  queue name: :awesome_queue, worker: AwesomeWorker, size: 2
end
          

app/workers/log_worker.rb

class LogWorker
  include SuckerPunch::Worker

  def perform(event)
    Log.new(event).track
  end
end
          

Sucker Punch

Synchronous

SuckerPunch::Queue[:log_queue].perform("login")
          

Asynchronous

SuckerPunch::Queue[:log_queue].async.perform("login")
          

Sucker Punch

Pros

  • Runs within a web process
  • Easy syntax
  • Relies on Celluloid

Cons

  • No Web UI
  • No persistence

Sidekiq

app/workers/log_worker.rb

class HardWorker
  include Sidekiq::Worker

  def perform(name, count)
    puts 'Doing hard work'
  end
end
          

Asynchronous

HardWorker.perform_async('bob', 5)
          

Do work

sidekiq -c 25
          

Sidekiq

Sidekiq

Pros

  • Awesome Web UI!
  • Scalable
  • Relies on Celluloid
  • Persistence
  • Extensible middleware system

Cons

  • Redis dependency
  • Requires separate worker process

Questions?

Thank you!