Make any method perfomed later.
Add this line to your application's Gemfile:
gem 'activejob-perform_later'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activejob-perform_later
class MyClass
def self.a_method(arg1, arg2)
# do stuff
end
perform_later :a_method
perform_later :a_method, queue: :low_priority
perform_later :a_method, wait: 10.minutes
perform_later :a_method, wait: 60.minutes, queue: :low_priority
end
#calling the method will actually schedule the method to be performed later by ActiveJob
MyClass.a_method(1, 2)
class MyClass
def self.a_method(arg1, arg2)
# do stuff
end
end
# calling the method directly will perform it inline
MyClass.a_method(1,2)
# calling the method with perform_later will schedule the performing in Active Job
MyClass.perform_later.a_method(1,2)
MyClass.perform_later(queue: :low_priority).a_method(1,2)
MyClass.perform_later(wait: 10.minutes).a_method(1,2)
MyClass.perform_later(wait: 10.minutes, queue: :low_priority).a_method(1,2)
MyClass.perform_later(wait_until: 60.minutes.from_now, queue: :low_priority).a_method(1,2)
end
This works only if your class is safe to be passed to Active Job (e.g.: it's global identificable with GlobalID. Active Record implements GlobalID since 4.2.0):
class User < ActiveRecord::Base
def recalculate_billing
# do stuff
end
end
# calling the method with perform_later will schedule the performing in Active Job
User.last.perform_later.recalculate_billing
User.last.perform_later(queue: :low_priority).recalculate_billing
User.last.perform_later(wait: 10.minutes).recalculate_billing
User.last.perform_later(wait_until: 60.minutes.from_now).recalculate_billing
User.last.perform_later(wait: 10.minutes, queue: :low_priority).recalculate_billing
User.last.perform_later(wait_until: 60.minutes.from_now, queue: :low_priority).recalculate_billing
end
- Fork it ( https://github.com/cristianbica/activejob-perform_later/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