Skip to content

Instantly share code, notes, and snippets.

@wxmn
Created March 21, 2011 23:57
Show Gist options
  • Save wxmn/880498 to your computer and use it in GitHub Desktop.
Save wxmn/880498 to your computer and use it in GitHub Desktop.
Slight improvements to Simple Worker base class
class SmartWorker < SimpleWorker::Base
merge "../../vendor/some_gem/lib/some_gem.rb"
def initialize(args={})
args.each do |k, v|
instance_variable_set "@#{k.to_s}", v
instance_eval "class << self; attr_accessor :#{k.to_s}; end"
end
end
def smart_queue
if Rails.env.production? || Rails.env.staging?
self.queue(:priority => @priority||2)
else
self.run_local
end
end
end
class SomeWorker < SmartWorker
def run
@user = User.find(user_id)
@user.post_message(message)
end
end
#example
w = SomeWorker.new(:user_id => @user.id, :message => "Hello, world.", :priority => 1)
w.smart_queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment