Resque Waiting Room
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.
Installation
Add this line to your application's Gemfile:
gem 'resque-waiting-room'
And then execute:
$ bundle
Or install it yourself as:
$ gem install resque-waiting-room
Usage
Example -- 10 jobs processed every 30 seconds max
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.
Testing
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
Run tests with the following command
bundle exec rake
Contributing
- 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
Thanks to the following people for helping out
- Thomas Devol @socialchorus for adding the RSpec matcher
- Max Dunn @maxdunn210 for making me switch Resque 2 specific code in it's own branch
- Jeff Durand @johnnyiller for the update of has_remaining_performs_key using the latest form set and the fix for a rare ttl bug
- Tatsuya Takamura @ttakamura for raising the issue on the rare ttl bug
- Omry Zobel @omryYotpo for allowing some customization of the redis key