switch_gear_sidekiq-middleware 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: e87a04410e84969c44b5f397b4385cc69fee127c
4
+ data.tar.gz: 1f139cd1d03d899224042904415aedf85114ef95
5
+ SHA512:
6
+ metadata.gz: e1ea08a4b828a19576475dd0a4c7feacf4b4776406239c6a1ec6e74ea438d5e20cb4e8ae0bd705e5b157e778589781e233d2e4c241b66c4c4932c88ca3fc9e84
7
+ data.tar.gz: ca56bd84113390139da75fe13291b442a9211ccdd3b92ac881bbbfdb74bb6b29146341397cc5488b761257cdb3d4fea065e54fba38e792beb0b4805db103c2d6
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ coverage/
11
+ .ruby-version
12
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at anthony.s.ross@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in switch_gear_sidekiq-middleware.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Anthony Ross
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # SwitchGearSidekiq::Middleware
2
+
3
+ This gem is a small [Sidekiq](https://www.github.com/mperham/sidekiq) middlware that uses the [SwitchGear](https://www.github.com/allcentury/switch_gear) gem to allow for a circuit breaker to be used across sidekiq jobs. This is done using the `Redis` client found in `SwitchGear`.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'switch_gear_sidekiq-middleware'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install switch_gear_sidekiq-middleware
21
+
22
+ ## Usage
23
+
24
+ You need to add the middlware to your sidekiq initializer like so:
25
+
26
+ ```ruby
27
+ # use your own redis connecton or checkout one from the sidekiq pool
28
+ redis = Redis.new || Sidekiq::Client.new.redis_pool.checkout
29
+
30
+ breaker = SwitchGearSidekiq::Breaker.new do |cb|
31
+ cb.client = redis
32
+ cb.worker = YourWorkerClass
33
+ cb.failure_limit = 2
34
+ cb.reset_timeout = 5
35
+ cb.logger = Sidekiq.logger
36
+ end
37
+
38
+ Sidekiq.configure_server do |config|
39
+ config.server_middleware do |chain|
40
+ chain.add SwitchGearSidekiq::Middleware, breakers: [breaker]
41
+ end
42
+ end
43
+ ```
44
+
45
+ You can pass an array of breakers to the middleware. The middlware looks up the breaker by the worker class you pass in and needs to match the worker Sidekiq would initialize when it's popped off the queue.
46
+
47
+ There is a helper class called `SwitchGearSidekiq::Breaker` which is a really thin wrapper around [SwitchGear::CircuitBreaker::Redis](https://www.github.com/allcentury/switch_gear). Please see that gems documentation on what can be configured.
48
+
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/allcentury/switch_gear_sidekiq-middleware. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "switch_gear_sidekiq/middleware"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ require 'sidekiq'
2
+ require 'switch_gear'
3
+ require 'redis'
4
+
5
+ class HardWorker
6
+ include Sidekiq::Worker
7
+ sidekiq_options retry: false
8
+
9
+ def perform(name)
10
+ http_result = ["Success!", "Fail"].sample
11
+ raise RuntimeError.new("Failed to fetch something for #{name}") if http_result == "Fail"
12
+ Sidekiq.logger.info "#{http_result} getting something for #{name}"
13
+ end
14
+ end
15
+
16
+ # use your own redis connecton or checkout one from the sidekiq pool
17
+ redis = Redis.new || Sidekiq::Client.new.redis_pool.checkout
18
+ Sidekiq.logger.level = :debug
19
+
20
+ breaker = SwitchGearSidekiq::Breaker.new do |cb|
21
+ cb.client = redis
22
+ cb.worker = HardWorker
23
+ cb.failure_limit = 2
24
+ cb.reset_timeout = 5
25
+ cb.logger = Sidekiq.logger
26
+ end
27
+
28
+ Sidekiq.configure_server do |config|
29
+ config.server_middleware do |chain|
30
+ chain.add SwitchGearSidekiq::Middleware, breakers: [breaker]
31
+ end
32
+ end
33
+
34
+ ["joe", "jane", "mary", "steve", "harry", "jeff", "sally", "tracy", "anna"].each { |handle| HardWorker.perform_async(handle) }
35
+
36
+ # Run this file on the commandline via:
37
+ # sidekiq -c 2 -r ./examples/sidekiq.rb
@@ -0,0 +1,33 @@
1
+ require 'switch_gear'
2
+
3
+ module SwitchGearSidekiq
4
+ class Breaker < ::SwitchGear::CircuitBreaker::Redis
5
+ attr_accessor :worker
6
+ def initialize
7
+ yield self
8
+ @namespace = "circuit_breaker_#{worker}"
9
+ @logger = logger || Sidekiq.logger
10
+ # dummy lambda to allow easy invocation of job
11
+ @circuit = circuit || -> (sk_job) { sk_job.call }
12
+ run_validations
13
+ end
14
+
15
+ def to_s
16
+ <<~EOF
17
+ [SwitchGearSidekiq::Breaker] - Breaker config
18
+ namespace: #{namespace}
19
+ logger: #{logger}
20
+ circuit: #{circuit}
21
+ reset_timeout: #{reset_timeout}
22
+ failure_limit: #{failure_limit}
23
+ EOF
24
+ end
25
+
26
+ private
27
+
28
+ def run_validations
29
+ msg = "please provider a worker"
30
+ raise(ArgumentError, msg) if !worker
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'switch_gear_sidekiq/breaker'
2
+
3
+ module SwitchGearSidekiq
4
+ class Middleware
5
+ VERSION = "0.1.0"
6
+
7
+ attr_reader :breakers
8
+
9
+ def initialize(breakers:)
10
+ @breakers = breakers.each_with_object({}) do |breaker, hash|
11
+ hash[breaker.worker] = breaker
12
+ end
13
+ run_validations
14
+ end
15
+
16
+ def call(worker, msg, queue, &block)
17
+ breaker = breakers[worker.class]
18
+ Sidekiq.logger.debug "Breaker being used: #{breaker}"
19
+
20
+ breaker.call(block)
21
+ rescue SwitchGear::CircuitBreaker::OpenError
22
+ retry_in = breaker.reset_timeout + 1
23
+ fail_msg = "Circuit is open for worker: #{breaker.worker} - blocking all calls;"
24
+ fail_msg += " Jobs will try again in: #{retry_in} "
25
+ Sidekiq.logger.warn fail_msg
26
+
27
+ # Here we'll re-enqueue the job to be 1 second above the reset_timeout period
28
+ worker.class.perform_in(retry_in, *msg['args'])
29
+ end
30
+
31
+ private
32
+
33
+ def run_validations
34
+ msg = "You must pass in an instance of a SwitchGear::CircuitBreaker"
35
+ fail msg if breakers.any? { |klass, breaker| !breaker.is_a? SwitchGear::CircuitBreaker }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'switch_gear_sidekiq/middleware'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "switch_gear_sidekiq-middleware"
8
+ spec.version = SwitchGearSidekiq::Middleware::VERSION
9
+ spec.authors = ["Anthony Ross"]
10
+ spec.email = ["anthony.s.ross@gmail.com"]
11
+
12
+ spec.summary = %q{A Sidekiq middleware that implements the circuit breaker pattern}
13
+ spec.description = %q{This gem allows for users of Sidekiq in a distributed system to use a common circuit breaker that can be used across any number of Sidekiq servers"}
14
+ spec.homepage = "https://github.com/allcentury/switch_gear_sidekiq-middlware"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'sidekiq'
25
+ spec.add_dependency 'switch_gear', '~> 0.2.0'
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "simplecov", "~> 0.13"
31
+ spec.add_development_dependency "yard", "~> 0.9"
32
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: switch_gear_sidekiq-middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Ross
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-05 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: switch_gear
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.13'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.13'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
125
+ description: This gem allows for users of Sidekiq in a distributed system to use a
126
+ common circuit breaker that can be used across any number of Sidekiq servers"
127
+ email:
128
+ - anthony.s.ross@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - examples/sidekiq.rb
144
+ - lib/switch_gear_sidekiq/breaker.rb
145
+ - lib/switch_gear_sidekiq/middleware.rb
146
+ - lib/switch_gear_sidekiq/middleware/version.rb
147
+ - switch_gear_sidekiq-middleware.gemspec
148
+ homepage: https://github.com/allcentury/switch_gear_sidekiq-middlware
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.6.8
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: A Sidekiq middleware that implements the circuit breaker pattern
172
+ test_files: []