Skip to content

Instantly share code, notes, and snippets.

@bts
Forked from bkeepers/plugin.rb
Created September 30, 2011 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bts/1255003 to your computer and use it in GitHub Desktop.
Save bts/1255003 to your computer and use it in GitHub Desktop.
Theoretical plugin API for Qu
# 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!)
# 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 Qu::Plugin::AutoRetry < Qu::Plugin
# 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?
# bts: use class methods instead
#
# 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
class ProcessPresentation < Qu::Job
# 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
def initialize(presentation_id)
@presentation = Presentation.find(presentation)
end
def perform
# …
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment