A Resque plugin. Requires Resque >= 1.19 and a >= 1.9 Ruby (MRI, JRuby or Rubinius).
If you want to limit the number of performs of a job for a given period, extend it with this module.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install resque-waiting-room
require 'resque/plugins/waiting_room' class UpdateDataFromExternalAPI extend Resque::Plugins::WaitingRoom # This job can be performed 10 times every 30 seconds can_be_performed times: 10, period: 30 def self.perform(repo_id) blah end end
If 10 UpdateDataFromExternalAPI jobs have been performed in 20 seconds, for the next 10 seconds UpdateDataFromExternalAPI jobs will be placed in the waiting_room queue and processed when possible. When the first 30 seconds are elapsed, the counter is set back to 0 and 10 jobs can be performed again. You got to manually tweak the queue names in your workers though.
We include a matcher
require 'spec/support/matchers' describe MyJob do it 'is rate limited' do MyJob.should be_only_performed(times: 100, period: 300) end end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Added some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
- Thomas Devol @socialchorus for adding the RSpec matcher
- Max Dunn @maxdunn210 for making me switch Resque 2 specific code in it's own branch