Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Resque Intro

Resque Intro

Done @ Apero Ruby Lille

Robin Komiwes

October 31, 2012
Tweet

More Decks by Robin Komiwes

Other Decks in Programming

Transcript

  1. RESQUE
    /ˈrɛskyu/

    View full-size slide

  2. J 4228
    700+ forks
    defunktocat
    defunkt/resque

    View full-size slide

  3. DelayedJob
    SQS
    Starling
    ActiveMessaging
    BackgroundJob
    Beanskalkd
    RESQUE

    View full-size slide

  4. Background jobs
    JSON pending jobs in Redis
    Ruby
    multiples Queues
    Persistents Workers
    Classes that respond to `perform`

    View full-size slide

  5. # lib/resque/archive.rb
    class Archive
    @queue = :file_serve
    def self.perform(repo_id, branch = 'master')
    repo = Repository.find(repo_id)
    repo.create_archive(branch)
    end
    end
    # app/models/repository.rb
    class Repository
    def async_create_archive(branch)
    Resque.enqueue(Archive, self.id, branch)
    end
    end

    View full-size slide

  6. Steve Klabnik
    "Yet Another GitHub
    Project They've Open
    Sourced and Then
    Abandoned."
    But Resque is super
    awesome [...]
    http://blog.steveklabnik.com/posts/2012-09-22-resque--let-s-do-this

    View full-size slide

  7. Fresc | Smart Images Feedback
    RESQUE
    5 workers
    10+ ≠ queues

    View full-size slide

  8. Asynchronous
    manipulation
    upload to CDN
    Emails delivering
    Images
    PDF reports generation
    ...

    View full-size slide

  9. plugins
    resque-scheduler
    resque-retry
    resque-mailer
    resque-lock

    View full-size slide

  10. daily_digest:
    cron: "0 */24 * * *"
    class: Digest
    description: "The one time a day activity digest"
    args: [1]
    bidaily_digest:
    cron: "0 */12 * * *"
    class: Digest
    description: "The two time a day activity digest"
    args: [2]
    instant_digest:
    cron: "*/5 * * * *"
    class: Digest
    description: "Send each 5 minuts a digest"
    args: [3]

    View full-size slide

  11. module UploadImageToCdn
    extend Resque::Plugins::ExponentialBackoff
    @queue = :cdn_upload
    @backoff_strategy = [0, 0, 0, 0.5, 1, 2, 5, 10]
    def perform( image_id )
    # ...
    end
    extend self
    end

    View full-size slide

  12. class UserMailer < ActionMailer::Base
    include Resque::Mailer
    # ...
    end

    View full-size slide

  13. require 'resque/plugins/lock'
    module SummarizeProject
    extend Resque::Plugins::Lock
    @queue = :summarize_project
    def perform( gallery_id )
    gallery = Gallery.find( gallery_id )
    gallery.summarize
    end
    extend self
    end

    View full-size slide

  14. Run | development
    ★ rake task to start workers

    View full-size slide

  15. Run | production
    ★ Configure Upstart to auto-restart workers
    when they are down.
    ★ Capistrano deploy hook to shutdown
    running workers.
    after "deploy:create_symlink", "deploy:stop_resque"

    View full-size slide