diff --git a/README.md b/README.md index ef09458..d654104 100644 --- a/README.md +++ b/README.md @@ -58,25 +58,6 @@ will be preserved. Sidekiq::Queue['name'].unpause # allows workers to use the queue ``` -### Multiple processes - -Limits are applied per process. In case you have several worker -processes and want to have global locks between them, you'll need to -enable global mode by setting global option, eg: - -```yaml - :global: true -``` - -or - -```ruby - Sidekiq.options[:global] = true -``` - -*Note:* if you want to change limits from a rails console you also need -to use a global mode since it's a different from a worker process. - ### Blocking queue mode If you use strict queue ordering (it will be used if you don't specify queue weights) diff --git a/lib/sidekiq/extensions/queue.rb b/lib/sidekiq/extensions/queue.rb index 6a170e5..9ecde61 100644 --- a/lib/sidekiq/extensions/queue.rb +++ b/lib/sidekiq/extensions/queue.rb @@ -16,7 +16,7 @@ module Sidekiq end def mode - Sidekiq.options[:global] ? LimitFetch::Global : LimitFetch::Local + Sidekiq.options[:local] ? LimitFetch::Local : LimitFetch::Global end end end diff --git a/lib/sidekiq/limit_fetch.rb b/lib/sidekiq/limit_fetch.rb index 1dbbbb1..9bd8fec 100644 --- a/lib/sidekiq/limit_fetch.rb +++ b/lib/sidekiq/limit_fetch.rb @@ -19,7 +19,7 @@ class Sidekiq::LimitFetch end def initialize(options) - Global::Monitor.start! if options[:global] + Global::Monitor.start! unless options[:local] @queues = Queues.new options end diff --git a/lib/sidekiq/limit_fetch/queues.rb b/lib/sidekiq/limit_fetch/queues.rb index 2371a71..6d7ee8f 100644 --- a/lib/sidekiq/limit_fetch/queues.rb +++ b/lib/sidekiq/limit_fetch/queues.rb @@ -7,7 +7,7 @@ class Sidekiq::LimitFetch @queues = options[:queues] options[:strict] ? strict_order! : weighted_order! - set_selector options[:global] + set_selector options[:local] set_limits options[:limits] set_blocks options[:blocking] end @@ -26,8 +26,8 @@ class Sidekiq::LimitFetch private - def set_selector(global) - @selector = global ? Global::Selector : Local::Selector + def set_selector(local) + @selector = local ? Local::Selector : Global::Selector end def set_limits(limits) diff --git a/spec/sidekiq/extensions/queue_spec.rb b/spec/sidekiq/extensions/queue_spec.rb index ec67df7..07d7333 100644 --- a/spec/sidekiq/extensions/queue_spec.rb +++ b/spec/sidekiq/extensions/queue_spec.rb @@ -88,12 +88,12 @@ describe Sidekiq::Queue do end context 'global' do - before(:all) { Sidekiq.options[:global] = true } + before(:all) { Sidekiq.options[:local] = false } it_behaves_like :lock end context 'local' do - before(:all) { Sidekiq.options[:global] = false } + before(:all) { Sidekiq.options[:local] = true } it_behaves_like :lock end end diff --git a/spec/sidekiq/limit_fetch/global/monitor_spec.rb b/spec/sidekiq/limit_fetch/global/monitor_spec.rb index 11f99d3..da15fd7 100644 --- a/spec/sidekiq/limit_fetch/global/monitor_spec.rb +++ b/spec/sidekiq/limit_fetch/global/monitor_spec.rb @@ -1,13 +1,23 @@ require 'spec_helper' describe Sidekiq::LimitFetch::Global::Monitor do - let(:global) { true } let(:monitor) { described_class.start! ttl, timeout } let(:ttl) { 1 } let(:queue) { Sidekiq::Queue[name] } let(:name) { 'default' } before :each do + # namespaces = [ + # described_class::PROCESSOR_NAMESPACE, + # described_class::HEARTBEAT_NAMESPACE + # ] + + # Sidekiq.redis do |it| + # namespaces.flat_map {|namespace| + # it.keys(namespace + '*') + # }.each {|key| it.del key } + # end + monitor end diff --git a/spec/sidekiq/limit_fetch/queues_spec.rb b/spec/sidekiq/limit_fetch/queues_spec.rb index 8eb74c0..feeb7dd 100644 --- a/spec/sidekiq/limit_fetch/queues_spec.rb +++ b/spec/sidekiq/limit_fetch/queues_spec.rb @@ -3,17 +3,17 @@ require 'spec_helper' describe Sidekiq::LimitFetch::Queues do subject { described_class.new options } - let(:queues) { %w[queue1 queue2] } - let(:limits) {{ 'queue1' => 3 }} - let(:strict) { true } - let(:global) { false } - let(:blocking) { nil } + let(:queues) { %w[queue1 queue2] } + let(:limits) {{ 'queue1' => 3 }} + let(:strict) { true } + let(:local) {} + let(:blocking) {} let(:options) do - { queues: queues, - limits: limits, - strict: strict, - global: global, + { queues: queues, + limits: limits, + strict: strict, + local: local, blocking: blocking } end @@ -83,20 +83,20 @@ describe Sidekiq::LimitFetch::Queues do end end - context 'without global flag' do + context 'without local flag' do it_should_behave_like :selector - it 'without global flag should be local' do - subject.selector.should == Sidekiq::LimitFetch::Local::Selector + it 'without local flag should be global' do + subject.selector.should == Sidekiq::LimitFetch::Global::Selector end end - context 'with global flag' do - let(:global) { true } + context 'with local flag' do + let(:local) { true } it_should_behave_like :selector - it 'should use global selector' do - subject.selector.should == Sidekiq::LimitFetch::Global::Selector + it 'should use local selector' do + subject.selector.should == Sidekiq::LimitFetch::Local::Selector end end diff --git a/spec/sidekiq/limit_fetch_spec.rb b/spec/sidekiq/limit_fetch_spec.rb index 42a29ba..c6c3f98 100644 --- a/spec/sidekiq/limit_fetch_spec.rb +++ b/spec/sidekiq/limit_fetch_spec.rb @@ -11,7 +11,7 @@ describe Sidekiq::LimitFetch do end subject { described_class.new options } - let(:options) {{ queues: queues, limits: limits, global: global }} + let(:options) {{ queues: queues, limits: limits, local: local }} let(:queues) { %w(queue1 queue1 queue2 queue2) } let(:limits) {{ 'queue1' => 1, 'queue2' => 2 }} @@ -36,12 +36,12 @@ describe Sidekiq::LimitFetch do end context 'global' do - let(:global) { true } + let(:local) { false } it_behaves_like :strategy end context 'local' do - let(:global) { false } + let(:local) { true } it_behaves_like :strategy end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4bc98f3..d8b767a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,8 +3,8 @@ require 'sidekiq/limit_fetch' RSpec.configure do |config| config.before :each do Sidekiq::Queue.instance_variable_set :@instances, {} - Sidekiq.options[:global] = defined?(global) ? global : nil - + Sidekiq.options[:local] = defined?(local) ? local : nil + Sidekiq.redis do |it| clean_redis = ->(queue) do it.del "limit_fetch:limit:#{queue}"