ResqueSlidingWindow
Want a sliding window for debouncing bursty Resque events? Here it is.
Installation
Add this line to your application's Gemfile:
gem 'resque_sliding_window'
gem 'resque-scheduler', require: 'resque_scheduler', git: "https://github.com/jonhyman/resque-scheduler.git" # prefered repo
And then execute:
$ bundle
Setup
You might have a glance at the resque-scheduler gem. You need to setup a worker. To run one manually, simply:
rake resque:scheduler
But again, see the resque-scheduler gem as there is some setup for the rake task.
Usage
module Jobs
class Publish
extend Resque::Plugins::SlidingWindow
@queue = :low
def self.perform(id)
# Crazy stuff
end
end
end
Resque.enqueue_in(30, Jobs::Publish, id)
This will put a job in a delayed queue for 30 seconds. If a similar job is entered (same args, same klass), it will be deleted in favor of the new 30-second job. However, it can only be pushed off for a 1m 30s from its initial queue-time before it force runs one. When it forces one through, others will not be run.
Default max_time
is 1m and 30s. To change this:
module Jobs
class Publish
extend Resque::Plugins::SlidingWindow
@queue = :low
def self.max_time
30.minutes
end
def self.perform(id)
# Crazy stuff
end
end
end
Contributing
- Fork it
- 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 new Pull Request