sidekiq_error_label 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e139082e8a73bfdabb2ca42dfee491388accd04
4
+ data.tar.gz: 26e5b03da02e6ddc74e2aa8f6ba52f9326c0ccda
5
+ SHA512:
6
+ metadata.gz: 7a7d2b01572ea0803d6ce6f2977907084e9943a07e6c38641766d6f2ab481fde92e22bd88acaccc411bfd283f0baf7e5c3704d08fbfd06c8395ac92a6bda01c6
7
+ data.tar.gz: e6c32776b81e353f3a8c269fd730d4ce0255bca4fafc0c0bcc97af3ecd97f404ff2d8960fecb03f14928725a6a53f8c4ab6bf715b9032d3e52feae23f8068fe4
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq_error_label.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tema Bolshakov
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # SidekiqErrorLabel
2
+
3
+ Label sidekiq exception.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sidekiq_error_label'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sidekiq_error_label
18
+
19
+ ## Usage
20
+
21
+ Include middleware to server chain and configure list of errors which should be marked as `SilentException`.
22
+
23
+ ```ruby
24
+ module SilentException
25
+ end
26
+
27
+ Sidekiq.configure_server do |config|
28
+ config.server_middleware do |chain|
29
+ chain.add SidekiqErrorLabel::Middleware,
30
+ exceptions: [
31
+ Errno::ECONNRESET,
32
+ Net::OpenTimeout,
33
+ Errno::EHOSTUNREACH,
34
+ Net::ReadTimeout
35
+ ],
36
+ retries_threshold: 3,
37
+ as: SilentException
38
+ end
39
+ end
40
+ ```
41
+
42
+
43
+ Middleware extands listed exceptions with `SilentException` module if same job
44
+ have been failing with one of this errors for `retries_threshold` times. So you may catch them by the module name.
45
+
46
+ If no `:as` option given exceptions would be labeled with `SidekiqErrorLabel::Middleware::Labels::Default`
47
+
48
+ E.g. you may configure Airbrake to log those types of errors only if job have failed `retries_threshold` times in a row:
49
+
50
+ ```ruby
51
+ Airbrake.configure do |config|
52
+ config.ignore << SilentException
53
+ end
54
+ ```
55
+
56
+ You may create labels on the fly:
57
+
58
+ ```ruby
59
+ SidekiqErrorLabel::Middleware.label(:default) #=> SidekiqErrorLabel::Middleware::Labels::Default
60
+ SidekiqErrorLabel::Middleware.label(:Silent) #=> SidekiqErrorLabel::Middleware::Labels::Silent
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/SPBTV/sidekiq_error_label/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,4 @@
1
+ module SidekiqErrorLabel::Middleware::Labels
2
+ module Default
3
+ end
4
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_support/inflections'
2
+
3
+ class SidekiqErrorLabel::Middleware
4
+ require_relative 'labels'
5
+
6
+ RETRIES_THRESHOLD = 5
7
+
8
+ def initialize(options = {})
9
+ @exceptions = options.fetch(:exceptions, [])
10
+ @retries_threshold = options.fetch(:retries_threshold, RETRIES_THRESHOLD)
11
+ @label = options.fetch(:as, self.class.label)
12
+ end
13
+
14
+ def call(worker, job, queue)
15
+ yield
16
+ rescue *@exceptions => error
17
+ if label_exception?(job)
18
+ error.extend @label
19
+ raise error
20
+ else
21
+ raise
22
+ end
23
+ end
24
+
25
+ def label_exception?(job)
26
+ job['retry_count'] && job['retry_count'] < @retries_threshold
27
+ end
28
+
29
+ def self.label(name = :default)
30
+ label_name = name.to_s.classify.to_sym
31
+ if Labels.constants.include?(label_name)
32
+ Labels.const_get(label_name)
33
+ else
34
+ Labels.const_set(label_name, Module.new)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module SidekiqErrorLabel
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "sidekiq_error_label/version"
2
+ require 'sidekiq_error_label/middleware'
3
+
4
+ module SidekiqErrorLabel
5
+ # Your code goes here...
6
+ 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_error_label/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq_error_label"
8
+ spec.version = SidekiqErrorLabel::VERSION
9
+ spec.authors = ["Tema Bolshakov"]
10
+ spec.email = ["abolshakov@spbtv.com"]
11
+ spec.summary = %q{Label sidekiq exception.}
12
+ spec.description = %q{Label sidekiq exception.}
13
+ spec.homepage = "https://github.com/SPBTV/sidekiq_error_label"
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_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "sidekiq"
25
+ spec.add_runtime_dependency 'activesupport'
26
+ end
@@ -0,0 +1,104 @@
1
+ require 'sidekiq/worker'
2
+
3
+ RSpec.describe SidekiqErrorLabel::Middleware do
4
+ let(:worker) do
5
+ Class.new do
6
+ include ::Sidekiq::Worker
7
+ end
8
+ end
9
+ let(:my_label) { Module.new }
10
+ let(:exception) { Class.new(StandardError) }
11
+
12
+ let(:job) do
13
+ { 'class' => 'Bob', 'args' => [1,2,'foo'], 'retry' => 2, 'jid' => 'jid-string' }
14
+ end
15
+
16
+ context '#label?' do
17
+ let(:retries_threshold) { SidekiqErrorLabel::Middleware::RETRIES_THRESHOLD }
18
+ subject do
19
+ SidekiqErrorLabel::Middleware.new exceptions: [exception]
20
+ end
21
+
22
+ it 'should not #label_exception? if retry_count is not set' do
23
+ expect(subject.label_exception?(job.dup)).to be_falsey
24
+ end
25
+
26
+ it '#label_exception? if retry_count is less then retries_threshold' do
27
+ the_job = job.dup.merge('retry_count' => retries_threshold - 1)
28
+ expect(subject.label_exception?(the_job)).to be_truthy
29
+ end
30
+
31
+ it 'should not #label_exception? if retry_count is equals to retries_threshold' do
32
+ the_job = job.dup.merge('retry_count' => retries_threshold)
33
+ expect(subject.label_exception?(the_job)).to be_falsey
34
+ end
35
+
36
+ it 'should not #label_exception? if retry_count is greater then retries_threshold' do
37
+ the_job = job.dup.merge('retry_count' => retries_threshold + 1)
38
+ expect(subject.label_exception?(the_job)).to be_falsey
39
+ end
40
+
41
+ it 'should count :retries_threshold options' do
42
+ separator = SidekiqErrorLabel::Middleware.new exceptions: [exception], retries_threshold: 1
43
+ the_job = job.dup.merge('retry_count' => 1)
44
+ expect(separator.label_exception?(the_job)).to be_falsey
45
+ end
46
+ end
47
+
48
+ context '#call' do
49
+ subject do
50
+ SidekiqErrorLabel::Middleware.new exceptions: [exception]
51
+ end
52
+
53
+ it 'raise not labeled exception' do
54
+ expect(subject).to receive(:label_exception?).with(job).and_return(false)
55
+ begin
56
+ subject.call(worker, job, 'default') do
57
+ raise exception
58
+ end
59
+ rescue => error
60
+ expect(error).not_to be_kind_of SidekiqErrorLabel::Middleware.label
61
+ expect(error).to be_kind_of exception
62
+ end
63
+ end
64
+
65
+ it 'raise labeled exception' do
66
+ expect(subject).to receive(:label_exception?).with(job).and_return(true)
67
+ begin
68
+ subject.call(worker, job, 'default') do
69
+ raise exception
70
+ end
71
+ rescue => error
72
+ expect(error).to be_kind_of SidekiqErrorLabel::Middleware.label
73
+ expect(error).to be_kind_of exception
74
+ end
75
+ end
76
+
77
+ it 'count :as option' do
78
+ separator = SidekiqErrorLabel::Middleware.new exceptions: [exception], as: my_label
79
+ expect(separator).to receive(:label_exception?).with(job).and_return(true)
80
+ begin
81
+ separator.call(worker, job, 'default') do
82
+ raise exception
83
+ end
84
+ rescue => error
85
+ expect(error).not_to be_kind_of SidekiqErrorLabel::Middleware.label
86
+ expect(error).to be_kind_of my_label
87
+ expect(error).to be_kind_of exception
88
+ end
89
+ end
90
+ end
91
+
92
+ context '.label' do
93
+ it 'create new label' do
94
+ label = SidekiqErrorLabel::Middleware.label(:default)
95
+ expect(label).to eq SidekiqErrorLabel::Middleware::Labels::Default
96
+ end
97
+
98
+ it 'return same label if called twice' do
99
+ label = SidekiqErrorLabel::Middleware.label(:default)
100
+
101
+ expect(SidekiqErrorLabel::Middleware.label(:default)).to eq label
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,78 @@
1
+ require 'sidekiq_error_label'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, make a
12
+ # separate helper file that requires this one and then use it only in the specs
13
+ # that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # The settings below are suggested to provide a good initial experience
21
+ # with RSpec, but feel free to customize to your heart's content.
22
+ # These two settings work together to allow you to limit a spec run
23
+ # to individual examples or groups you care about by tagging them with
24
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
25
+ # get run.
26
+ config.filter_run :focus
27
+ config.run_all_when_everything_filtered = true
28
+
29
+ # Many RSpec users commonly either run the entire suite or an individual
30
+ # file, and it's useful to allow more verbose output when running an
31
+ # individual spec file.
32
+ if config.files_to_run.one?
33
+ # Use the documentation formatter for detailed output,
34
+ # unless a formatter has already been configured
35
+ # (e.g. via a command-line flag).
36
+ config.default_formatter = 'doc'
37
+ end
38
+
39
+ # Print the 10 slowest examples and example groups at the
40
+ # end of the spec run, to help surface which specs are running
41
+ # particularly slow.
42
+ config.profile_examples = 10
43
+
44
+ # Run specs in random order to surface order dependencies. If you find an
45
+ # order dependency and want to debug it, you can fix the order by providing
46
+ # the seed, which is printed after each run.
47
+ # --seed 1234
48
+ config.order = :random
49
+
50
+ # Seed global randomization in this process using the `--seed` CLI option.
51
+ # Setting this allows you to use `--seed` to deterministically reproduce
52
+ # test failures related to randomization by passing the same `--seed` value
53
+ # as the one that triggered the failure.
54
+ Kernel.srand config.seed
55
+
56
+ # rspec-expectations config goes here. You can use an alternate
57
+ # assertion/expectation library such as wrong or the stdlib/minitest
58
+ # assertions if you prefer.
59
+ config.expect_with :rspec do |expectations|
60
+ # Enable only the newer, non-monkey-patching expect syntax.
61
+ # For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ expectations.syntax = :expect
64
+ end
65
+
66
+ # rspec-mocks config goes here. You can use an alternate test double
67
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
68
+ config.mock_with :rspec do |mocks|
69
+ # Enable only the newer, non-monkey-patching expect syntax.
70
+ # For more details, see:
71
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ mocks.syntax = :expect
73
+
74
+ # Prevents you from mocking or stubbing a method that does not exist on
75
+ # a real object. This is generally recommended.
76
+ mocks.verify_partial_doubles = true
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_error_label
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tema Bolshakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sidekiq
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Label sidekiq exception.
84
+ email:
85
+ - abolshakov@spbtv.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/sidekiq_error_label.rb
97
+ - lib/sidekiq_error_label/labels.rb
98
+ - lib/sidekiq_error_label/middleware.rb
99
+ - lib/sidekiq_error_label/version.rb
100
+ - sidekiq_error_label.gemspec
101
+ - spec/sidekiq_error_label/middleware_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/SPBTV/sidekiq_error_label
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Label sidekiq exception.
127
+ test_files:
128
+ - spec/sidekiq_error_label/middleware_spec.rb
129
+ - spec/spec_helper.rb