A simple way to invoke methods asynchronously using Resque. There are a few gems that provide similar functionality. However this this implementation provides more expressive syntax that it gives the caller control over the invocation.
Add this line to your application's Gemfile:
gem 'resque-async'
And then execute:
$ bundle
Or install it yourself as:
$ gem install resque-async
Assuming
class MyObject < ActiveRecord::Base
def self.slow_class_method
end
def slow_instance_method
end
end
Supports Class Methods and ActiveRecord instance methods
MyObject.async(:high).slow_class_method
# ActiveRecord instances will be queued/loaded/invoked asynchronously
MyObject.new.async(:medium).slow_instance_method
Natural synchronous invocation
MyObject.slow_class_method
MyObject.new.slow_instance_method
Support high, medium, low priority out of the box. As well as custom worker/queue
# slow_class_method queued/invoked on 'high' queue
MyObject.async(:high).slow_class_method
# slow_class_method queued/invoked on 'medium' queue
MyObject.async(:medium).slow_class_method
# slow_class_method queued/invoked on 'low' queue
MyObject.async(:low).slow_class_method
OR
class MyCustomerWorker
extend ResqueAsync::Workers::AsyncClassWorker
@queue :crazy
end
MyObject.async(MyCustomerWorker).crazy_priority
resque-async-method hides the fact that a method may be invoked asynchronously. From the caller's perspective things can get really confusing.
With resque-async-method
class MyObject < ActiveRecord::Base
def slow_method
end
async_method :slow_method
end
# is this async or not? What does result equal? Better check the implementation of MyObject
result = MyObject.new.slow_method
With resque-async
class MyObject < ActiveRecord::Base
def slow_method
end
end
# this should be pretty obvious, even years later, that slow_method is invoked async
d = MyObject.new.async(:low).slow_method
- Fork it ( https://github.com/exitround/resque-async/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request