Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Should be the initial commit to have everything working

  • Loading branch information...
commit 8215614651def6c9cad4c5d8b7a9d443606eb8ec 1 parent d79c777
@zanker authored
View
1  .gitignore
@@ -1,5 +1,6 @@
*.gem
*.idea
+Gemfile.lock
.bundle
tmp
.yardoc
View
1  .rspec
@@ -0,0 +1 @@
+--color
View
7 .travis.yml
@@ -0,0 +1,7 @@
+bundler_args: --without guard
+rvm:
+ - 1.8.7
+ - 1.9.3
+ - ruby-head
+ - jruby-18mode
+ - jruby-19mode
View
8 Gemfile
@@ -0,0 +1,8 @@
+source :rubygems
+gemspec
+
+gem "rake"
+
+group :guard do
+ gem "guard-rspec", "~>0.6.0"
+end
View
4 Guardfile
@@ -0,0 +1,4 @@
+guard("rspec", :all_after_pass => false, :cli => "--fail-fast --color") do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) {|match| "spec/concurrency_limiter_spec.rb"}
+end
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Zachary Anker
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
7 README.md
@@ -0,0 +1,7 @@
+Resque Concurrency Limiter
+===
+Tested against Resque 1.23.0.
+
+Only let's one job run at a time, and limits how many of the same job can be queued at a time.
+
+Primarily for things like syncing complex data to a remote API. Where having the same job running twice at the same time can cause issues, but you still want to let multiple jobs be queued to ensure data is synced.
View
12 Rakefile
@@ -0,0 +1,12 @@
+require "bundler"
+Bundler.setup
+
+require "rake"
+require "rspec"
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new("spec") do |spec|
+ spec.pattern = "spec/**/*_spec.rb"
+end
+
+task :default => :spec
View
58 lib/resque/plugins/concurrency_limiter.rb
@@ -0,0 +1,58 @@
+module Resque
+ module Plugins
+ module ConcurrencyLimiter
+ def queue_limit
+ @queue_limit || 1
+ end
+
+ def concurrency_key(type, *args)
+ "conc-#{type}:#{name}-#{args.to_s}"
+ end
+
+ # This can be overridden however you want.
+ # Such as enqueuing it in a lower priority queue, or if you use resque-scheduler, to try again in 30 seconds.
+ def requeue(*args)
+ Resque.enqueue(self, *args)
+ end
+
+ def before_enqueue_concurrency(*args)
+ normalized = Resque.decode(Resque.encode(args))
+ key = concurrency_key("lock", normalized)
+
+ if Resque.redis.incr(key) <= queue_limit
+ true
+
+ # Have too many queued already, revert our increment
+ else
+ Resque.redis.decr(key)
+ false
+ end
+ end
+
+ def around_perform_concurrency(*args)
+ # Job has been removed from the queue, need to decrement here in case
+ # we couldn't get the lock and it increments again.
+ Resque.redis.decr(concurrency_key("lock", args))
+
+ lock = Resque.redis.setnx(concurrency_key("active", args), true)
+ # Couldn't get the lock, requeue and will run it again later
+ unless lock
+ self.resqueue(args)
+ return
+ end
+
+ begin
+ yield
+ ensure
+ Resque.redis.del(concurrency_key("active", args))
+ end
+ end
+
+ # Clear our lock on running the job
+ def on_failure_concurrency(error, *args)
+ Resque.redis.del(concurrency_key("active", args))
+ end
+ end
+ end
+end
+
View
17 resque-concurrency-limiter.gemspec
@@ -0,0 +1,17 @@
+Gem::Specification.new do |s|
+ s.name = "resque-concurrency-limiter"
+ s.version = "0.0.1"
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Zachary Anker"]
+ s.email = ["zach.anker@gmail.com"]
+ s.homepage = "http://github.com/zanker/resque-concurrency-limiter"
+ s.summary = "Resque concurrency limiter"
+ s.description = "Limits how many jobs can be run at a time and limits how many can be queued."
+
+ s.files = Dir.glob("lib/**/*") + %w[LICENSE README.md Rakefile]
+ s.require_path = "lib"
+
+ s.add_runtime_dependency "resque", "~>1.23.0"
+
+ s.add_development_dependency "rspec", "~>2.11.0"
+end
View
37 spec/concurrency_limiter_spec.rb
@@ -0,0 +1,37 @@
+require "resque"
+require "resque/plugins/concurrency_limiter"
+
+describe Resque::Plugins::ConcurrencyLimiter do
+ class LockTest
+ extend Resque::Plugins::ConcurrencyLimiter
+ @queue = :conc_test
+
+ def self.perform(args)
+ raise "Perform ran"
+ end
+ end
+
+ it "passes lint" do
+ lambda {
+ Resque::Plugin.lint(Resque::Plugins::ConcurrencyLimiter)
+ }.should_not raise_exception
+ end
+
+ it "runs on resque version" do
+ Resque::Plugin.respond_to?(:before_enqueue_hooks).should be_true
+ Resque::Plugin.respond_to?(:around_hooks).should be_true
+ Resque::Plugin.respond_to?(:failure_hooks).should be_true
+ end
+
+ it "enqueues a job up to the limit" do
+ 3.times { Resque.enqueue(LockTest, {"a" => 1, "b" => 2})}
+
+ Resque.redis.llen("queue:conc_test").should have(1).job
+
+ 3.times do |i|
+ Resque.enqueue(LockTest, {"a" => i})
+ end
+
+ Resque.redis.llne("queue:conc_test").should have(4).jobs
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.