sidekiq-dynamic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24a7afa0adb95b136305ed6637204e42d21d6dca
4
+ data.tar.gz: dce54e09f38333dc656699267c27919625d899e9
5
+ SHA512:
6
+ metadata.gz: da57581a769c873e3471b7637a9488bbab323ef1ae16c2b5aa37220ce2a7c595a87a1f435b1d472837b523b2ca050d42d1458e7ab7eb851cd507b3a5739bcc4f
7
+ data.tar.gz: 2daf27243b3d0cebfd61831d9a4a2e94bd450e1e5e159f039b5c2cee4129f28647b01a6c0fc1cac2049816ccbdb20a1d88ff5b434ed2cef59e58cda55c4572c3
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - jruby-19mode
5
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-dynamic.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andre Arko & Jo Pu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # Sidekiq::Dynamic
2
+
3
+ Sidekiq::Dynamic allows Sidekiq jobs to choose their queue or shard/pool based on the job's arguments.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem 'sidekiq-dynamic'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ If you have a lot of Sidekiq queues and/or jobs, chances are good that you will eventually overrun the ability of a single Redis instance. When that happens, you need to be able to shard your jobs across multiple Redis instances easily. Let's say you have two Redis instances, and you want to send Sidekiq jobs to both of them.
15
+
16
+ ```
17
+ # List shards somewhere (you could even use Sidekiq.config)
18
+ shards = {
19
+ :cache => "127.0.0.1:5901",
20
+ :images => "127.0.0.1:5902"
21
+ }
22
+ ```
23
+
24
+ Using a regular Sidekiq::Worker, it's pretty easy to assign particular jobs to a particular queue or shard.
25
+
26
+ ```
27
+ # Assign a worker a static queue, and/or a static shard
28
+ require "sidekiq/worker"
29
+
30
+ class StaticSidekiqWorker
31
+ include Sidekiq::Worker
32
+ sidekiq_options queue: "cache_regenerator", pool: shards[:cache]
33
+ end
34
+ ```
35
+
36
+ In contrast to vanilla Sidekiq workers, Sidekiq::Dynamic workers allow you to examine the job arguments and choose a queue or shard for the job to be sent to when the job is queued.
37
+
38
+ ```
39
+ # Dynamic jobs use the job arguments to determine queue or shard
40
+ require "sidekiq/dynamic/worker"
41
+
42
+ class DynamicShardAndQueueWorker
43
+ include Sidekiq::Dynamic::Worker
44
+
45
+ # In this example, every shard will have both queues.
46
+ dynamic_queue do |args|
47
+ rand(1).zero? ? "cache_sweeper" : "image_generator"
48
+ end
49
+
50
+ dynamic_shard do |args|
51
+ case args.first
52
+ when "hard_work", "other_hard_work"
53
+ shards[:cache]
54
+ else
55
+ shards[:images]
56
+ end
57
+ end
58
+
59
+ end
60
+ ```
61
+
62
+ Keep in mind that neither this gem nor Sidekiq itself helps with running Sidekiq workers on each shard and queue. You'll need to start separate Sidekiq processes that are configured to talk to each Redis shard, and you'll need to list the queues those processes should work from.
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/sidekiq-dynamic/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,2 @@
1
+ require "sidekiq/dynamic/version"
2
+ require "sidekiq/dynamic/worker"
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Dynamic
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require "sidekiq/worker"
2
+
3
+ module Sidekiq
4
+ module Dynamic
5
+ module Worker
6
+
7
+ def self.included(base)
8
+ base.send(:include, Sidekiq::Worker)
9
+ base.class_attribute :dynamic_shard_proc
10
+ base.class_attribute :dynamic_queue_proc
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def dynamic_shard(&block)
16
+ self.dynamic_shard_proc = block
17
+ end
18
+
19
+ def dynamic_queue(&block)
20
+ self.dynamic_queue_proc = block
21
+ end
22
+
23
+ def client_push(item) # :nodoc:
24
+ old_pool = Thread.current[:sidekiq_via_pool]
25
+ item['queue'] = dynamic_queue_proc.call(item['args']) if dynamic_queue_proc
26
+ Thread.current[:sidekiq_via_pool] ||= dynamic_shard_proc.call(item['args']) if dynamic_shard_proc
27
+ super(item)
28
+ ensure
29
+ Thread.current[:sidekiq_via_pool] = old_pool
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/dynamic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-dynamic"
8
+ spec.version = Sidekiq::Dynamic::VERSION
9
+ spec.authors = ["André Arko & Jo Pu"]
10
+ spec.email = ["andre+jo@wanelo.com"]
11
+ spec.summary = %q{Extends Sidekiq workers to allow dynamic queue and shard choice.}
12
+ spec.description = %q{Sidekiq-dynamic creates a subclass of Sidekiq::Worker, named Sidekiq::Dynamic::Worker, that allows each worker class to run code that determines which queue and Redis shard a job will be sent to.}
13
+ spec.homepage = "http://www.github.com/wanelo/sidekiq-dynamic"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sidekiq", "~> 3.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ end
@@ -0,0 +1,82 @@
1
+ require "sidekiq/dynamic/worker"
2
+ # hay guise sidekiq/worker depends on Sidekiq but doesn't require it
3
+ require "sidekiq"
4
+
5
+ RSpec.describe Sidekiq::Dynamic::Worker do
6
+ let(:worker) {
7
+ Class.new do
8
+ include Sidekiq::Dynamic::Worker
9
+ class_attribute :class_redis_pool
10
+ end
11
+ }
12
+
13
+ let(:redis) { double("redis", sadd: true).tap { |redis|
14
+ allow(redis).to receive(:with).and_yield(redis)
15
+ allow(redis).to receive(:multi).and_yield.and_return([])
16
+ }}
17
+
18
+ let(:other_redis) { double("other_redis", sadd: true).tap { |redis|
19
+ allow(redis).to receive(:with).and_yield(redis)
20
+ allow(redis).to receive(:multi).and_yield.and_return([])
21
+ }}
22
+
23
+ before do
24
+ Sidekiq.instance_variable_set(:@redis, redis)
25
+ end
26
+
27
+ it "can instantiate a Sidekiq::Dynamic::Worker" do
28
+ expect(worker.new).to be_kind_of(Sidekiq::Dynamic::Worker)
29
+ end
30
+
31
+ it "can still enqueue a job" do
32
+ expect(redis).to receive(:lpush).once.with('queue:default', instance_of(Array))
33
+ worker.perform_async("omg")
34
+ end
35
+
36
+ it "can still enqueue a job to a static queue" do
37
+ worker.class_eval { sidekiq_options queue: 'amazing' }
38
+
39
+ expect(redis).to receive(:lpush).once.with('queue:amazing', instance_of(Array))
40
+ worker.perform_async("omg")
41
+ end
42
+
43
+ it "can enqueue a job to a dynamic queue" do
44
+ worker.class_eval do
45
+ dynamic_queue { |args| args.first }
46
+ end
47
+
48
+ expect(redis).to receive(:lpush).once.with('queue:omg', instance_of(Array))
49
+ worker.perform_async("omg")
50
+ end
51
+
52
+ it "can still enqueue a job to a static shard" do
53
+ worker.class_redis_pool = other_redis
54
+ worker.class_eval { sidekiq_options pool: class_redis_pool }
55
+
56
+ expect(other_redis).to receive(:lpush).once.with('queue:default', instance_of(Array))
57
+ worker.perform_async("omg")
58
+ end
59
+
60
+ it "can enqueue a job to a dynamic shard" do
61
+ worker.class_redis_pool = other_redis
62
+ worker.class_eval do
63
+ dynamic_shard { |args| class_redis_pool }
64
+ end
65
+
66
+ expect(other_redis).to receive(:lpush).once.with('queue:default', instance_of(Array))
67
+ worker.perform_async("omg")
68
+ end
69
+
70
+ it "can ignores the dynamic shard during `Sidekiq::Client.via`" do
71
+ worker.class_redis_pool = other_redis
72
+ worker.class_eval do
73
+ dynamic_shard { |args| class_redis_pool }
74
+ end
75
+
76
+ expect(redis).to receive(:lpush).once.with('queue:default', instance_of(Array))
77
+ Sidekiq::Client.via(redis) do
78
+ worker.perform_async("omg")
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,89 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # This setting enables warnings. It's recommended, but in some cases may
59
+ # be too noisy due to issues in dependencies.
60
+ config.warnings = true
61
+
62
+ # Many RSpec users commonly either run the entire suite or an individual
63
+ # file, and it's useful to allow more verbose output when running an
64
+ # individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output,
67
+ # unless a formatter has already been configured
68
+ # (e.g. via a command-line flag).
69
+ config.default_formatter = 'doc'
70
+ end
71
+
72
+ # Print the 10 slowest examples and example groups at the
73
+ # end of the spec run, to help surface which specs are running
74
+ # particularly slow.
75
+ config.profile_examples = 10
76
+
77
+ # Run specs in random order to surface order dependencies. If you find an
78
+ # order dependency and want to debug it, you can fix the order by providing
79
+ # the seed, which is printed after each run.
80
+ # --seed 1234
81
+ config.order = :random
82
+
83
+ # Seed global randomization in this process using the `--seed` CLI option.
84
+ # Setting this allows you to use `--seed` to deterministically reproduce
85
+ # test failures related to randomization by passing the same `--seed` value
86
+ # as the one that triggered the failure.
87
+ Kernel.srand config.seed
88
+ =end
89
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-dynamic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - André Arko & Jo Pu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ description: Sidekiq-dynamic creates a subclass of Sidekiq::Worker, named Sidekiq::Dynamic::Worker,
70
+ that allows each worker class to run code that determines which queue and Redis
71
+ shard a job will be sent to.
72
+ email:
73
+ - andre+jo@wanelo.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/sidekiq/dynamic.rb
86
+ - lib/sidekiq/dynamic/version.rb
87
+ - lib/sidekiq/dynamic/worker.rb
88
+ - sidekiq-dynamic.gemspec
89
+ - spec/sidekiq/dynamic/worker_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: http://www.github.com/wanelo/sidekiq-dynamic
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Extends Sidekiq workers to allow dynamic queue and shard choice.
115
+ test_files:
116
+ - spec/sidekiq/dynamic/worker_spec.rb
117
+ - spec/spec_helper.rb