Created
March 21, 2011 23:57
-
-
Save wxmn/880498 to your computer and use it in GitHub Desktop.
Slight improvements to Simple Worker base class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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