Skip to content

Instantly share code, notes, and snippets.

@bts
Forked from bkeepers/plugin.rb
Created September 30, 2011 21:20
Show Gist options
  • Select an option

  • Save bts/1255003 to your computer and use it in GitHub Desktop.

Select an option

Save bts/1255003 to your computer and use it in GitHub Desktop.

Revisions

  1. bts revised this gist Sep 30, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions option1.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # If I'm writing a plugin, I'd typically like to have a namespace to add my
    # own classes, so I would skip this option:
    # bts: If I'm writing a plugin, I'd typically like to have a namespace to add
    # my own classes, so I would skip this option:

    # Qu.plugin(:autoretry) do |*errors|
    # options = {:limit => 3}.merge!(errors.extract_options!)
  2. bts revised this gist Sep 30, 2011. 3 changed files with 32 additions and 15 deletions.
    25 changes: 14 additions & 11 deletions option1.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,15 @@
    Qu.plugin(:autoretry) do |*errors|
    options = {:limit => 3}.merge!(errors.extract_options!)
    errors << Exception if errors.empty?
    # If I'm writing a plugin, I'd typically like to have a namespace to add my
    # own classes, so I would skip this option:

    before :failure do |job, e|
    if errors.any? {|error| error === e }
    job.data[:retries] += 1
    Qu.requeue job unless job.config[:retries] >= limit
    halt # don't report the error yet
    end
    end
    end
    # Qu.plugin(:autoretry) do |*errors|
    # options = {:limit => 3}.merge!(errors.extract_options!)
    # errors << Exception if errors.empty?

    # before :failure do |job, e|
    # if errors.any? {|error| error === e }
    # job.data[:retries] += 1
    # Qu.requeue job unless job.config[:retries] >= limit
    # halt # don't report the error yet
    # end
    # end
    # end
    15 changes: 12 additions & 3 deletions option2.rb
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,25 @@
    class Qu::Plugin::AutoRetry < Qu::Plugin
    plugin :autoretry
    # bts: forego the following line, and introspect plugin name from class via
    # underscoring the class' name
    #
    # plugin :autoretry

    # bts: use "acts" class methods instead of registering callbacks in
    # #initialize
    before_failure :retry
    before :failure, :retry

    attr_reader :errors, :options

    def initialize(*errors)
    @options = {:limit => 3}.merge!(errors.extract_options!)
    @errors << Exception if errors.empty?

    before :failure, :retry
    # bts: use class methods instead
    #
    # before :failure, :retry
    end


    def retry(job, e)
    if errors.any? {|error| error === e }
    job.data[:retries] += 1
    7 changes: 6 additions & 1 deletion usage.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    class ProcessPresentation < Qu::Job
    autoretry Timeout::Error, :times => 5
    # bts: use convention of underscored class name, so we don't have to
    # declare the plugin name twice in the Plugin (via class' name and also via
    # the "plugin :autoretry" declaration)
    #
    auto_retry Timeout::Error, :times => 5
    # autoretry Timeout::Error, :times => 5

    attr_reader :presentation

  3. @bkeepers bkeepers renamed this gist Sep 30, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @bkeepers bkeepers revised this gist Sep 30, 2011. 3 changed files with 35 additions and 16 deletions.
    17 changes: 1 addition & 16 deletions gistfile1.rb → option1.rb
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,4 @@
    halt # don't report the error yet
    end
    end
    end


    class ProcessPresentation < Qu::Job
    autoretry Timeout::Error, :times => 5

    attr_reader :presentation

    def initialize(presentation_id)
    @presentation = Presentation.find(presentation)
    end

    def perform
    # …
    end
    end
    end
    21 changes: 21 additions & 0 deletions option2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    class Qu::Plugin::AutoRetry < Qu::Plugin
    plugin :autoretry

    attr_reader :errors, :options

    def initialize(*errors)
    @options = {:limit => 3}.merge!(errors.extract_options!)
    @errors << Exception if errors.empty?

    before :failure, :retry
    end


    def retry(job, e)
    if errors.any? {|error| error === e }
    job.data[:retries] += 1
    Qu.requeue job unless job.config[:retries] >= limit
    halt # don't report the error yet
    end
    end
    end
    13 changes: 13 additions & 0 deletions usage.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    class ProcessPresentation < Qu::Job
    autoretry Timeout::Error, :times => 5

    attr_reader :presentation

    def initialize(presentation_id)
    @presentation = Presentation.find(presentation)
    end

    def perform
    # …
    end
    end
  5. @bkeepers bkeepers created this gist Sep 30, 2011.
    27 changes: 27 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    Qu.plugin(:autoretry) do |*errors|
    options = {:limit => 3}.merge!(errors.extract_options!)
    errors << Exception if errors.empty?

    before :failure do |job, e|
    if errors.any? {|error| error === e }
    job.data[:retries] += 1
    Qu.requeue job unless job.config[:retries] >= limit
    halt # don't report the error yet
    end
    end
    end


    class ProcessPresentation < Qu::Job
    autoretry Timeout::Error, :times => 5

    attr_reader :presentation

    def initialize(presentation_id)
    @presentation = Presentation.find(presentation)
    end

    def perform
    # …
    end
    end