sidekiq-seize 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 306817cefcf71a2056aa390838be117d7d430bf8a40a0396ad8835e06ba658fc
4
+ data.tar.gz: f503a9231d785f0b49189a7d3069ecea50a595437d9f9b916587259b163bd021
5
+ SHA512:
6
+ metadata.gz: a4025302917611d5193eba6b8eca4a6c6b7bbf42d3961e00dbc781e398df972685783c1ef0dfec619ccdddef0c00e80bb1084477377dda33e71cefcb828a9374
7
+ data.tar.gz: d5821e5c30d3eb8b27718479409caf3faf053d798414a26751bd2d381e36441f5bc8236e6bab374c72162448a32019a4ecebf3bac72297a0cf7c285fc25845d5
@@ -0,0 +1,7 @@
1
+ tmp
2
+ gemfiles/*.lock
3
+ Gemfile.lock
4
+ .ruby-version
5
+ pkg
6
+ /.bundle
7
+ .idea
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ group :test do
7
+ gem 'rspec', '~> 3.6'
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Synaptic
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # sidekiq-seize
2
+
3
+ Sidekiq middleware that allows capturing exceptions and throwing only after last retry, useful for integrations with sentry, airbrake, rollbar, honeybadger etc, when you don't want to raise exceptions on each retry.
4
+
5
+ #### Installation
6
+
7
+ ```
8
+ $ gem install sidekiq-seize
9
+ ```
10
+
11
+ ### Worker example
12
+
13
+ ``` ruby
14
+ class MyWorker
15
+ include Sidekiq::Worker
16
+ sidekiq_options seize: true
17
+
18
+ def perform(params)
19
+ ...
20
+ end
21
+ end
22
+ ```
23
+
24
+
25
+ Seize only when certain types of exceptions occur
26
+
27
+ ``` ruby
28
+ class MyWorker
29
+ include Sidekiq::Worker
30
+ sidekiq_options seize: true, seize_exceptions_classes: [ActiveRecord::Deadlocked]
31
+
32
+ def perform(params)
33
+ ...
34
+ end
35
+ end
36
+ ```
37
+
38
+ #### Implementation Details
39
+
40
+ This middleware inherits from sidekiq `JobRetry` middleware, all exceptions in each retry until the final retry are captured and job is manually put into retry. For the last retry exception is raised naturally.
@@ -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,7 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/job_retry'
3
+ require 'sidekiq/middleware/server/seize'
4
+
5
+ ::Sidekiq.server_middleware do |chain|
6
+ chain.insert_before(::Sidekiq::JobRetry, Sidekiq::Middleware::Server::Seize)
7
+ end
@@ -0,0 +1,61 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/util'
3
+ require 'sidekiq/api'
4
+ require 'sidekiq/job_retry'
5
+
6
+ module Sidekiq
7
+ module Middleware
8
+ module Server
9
+ class Seize < ::Sidekiq::JobRetry
10
+
11
+ def call(worker, job, queue)
12
+ yield
13
+ rescue StandardError => e
14
+ options = worker.sidekiq_options_hash || {}
15
+ bubble_exception(options, job, e)
16
+ attempt_retry(worker, job, queue, e)
17
+ end
18
+
19
+ private
20
+
21
+ def bubble_exception(options, job, e)
22
+ raise e unless in_seize_mode?(options)
23
+ raise e unless retry_allowed?(options)
24
+ raise e unless seize_class?(options, e)
25
+
26
+ retry_count = job['retry_count'] || 0
27
+ last_try = retry_count == max_attempts_for(options) - 1
28
+ raise e if last_try
29
+ end
30
+
31
+ def retry_allowed?(options)
32
+ return false if !options['retry'].nil? && options['retry'] == false
33
+ return false if !options['retry'].nil? && options['retry'] == 0
34
+ true
35
+ end
36
+
37
+ def in_seize_mode?(options)
38
+ !options['seize'].nil? && options['seize'] == true
39
+ end
40
+
41
+ def seize_class?(options, e)
42
+ return true if options['seize_exceptions_classes'].nil?
43
+
44
+ options['seize_exceptions_classes'].each do |klass|
45
+ return true if klass == e.class
46
+ end
47
+
48
+ false
49
+ end
50
+
51
+ def max_attempts_for(options)
52
+ if options['retry'].is_a?(Integer)
53
+ options['retry']
54
+ else
55
+ @max_retries
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'sidekiq-seize'
5
+ gem.version = '0.4.0'
6
+ gem.authors = ['Rajat Goyal']
7
+ gem.email = ['rajat@synaptic.com']
8
+ gem.summary = 'Sidekiq middleware to silent errors and send only on dead'
9
+ gem.description = 'Sidekiq middleware that allows capturing exceptions and throwing only after last retry, when the job goes to dead queue.'
10
+ gem.license = 'MIT'
11
+ gem.executables = []
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
14
+ gem.require_paths = ['lib']
15
+ gem.required_ruby_version = '>= 2.2.2'
16
+
17
+ gem.add_runtime_dependency 'sidekiq', '~> 5.0', '>= 5.0.0'
18
+ gem.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
19
+ gem.add_development_dependency 'byebug'
20
+ end
@@ -0,0 +1,170 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/processor'
3
+
4
+ RSpec.describe Sidekiq::Middleware::Server::Seize do
5
+ TEST_EXCEPTION = ArgumentError
6
+ MAX_RETRY = 2
7
+
8
+ def build_job_hash(worker_class, args=[])
9
+ {'class' => worker_class, 'args' => args}
10
+ end
11
+
12
+ def fetch_retry_job
13
+ retry_set = Sidekiq::RetrySet.new
14
+ retry_job = retry_set.first
15
+ retry_set.clear
16
+ retry_job
17
+ end
18
+
19
+ def process_job(job_hash)
20
+ mgr = instance_double('Manager', options: {:queues => ['default']})
21
+ processor = ::Sidekiq::Processor.new(mgr)
22
+ job_msg = Sidekiq.dump_json(job_hash)
23
+ processor.process(Sidekiq::BasicFetch::UnitOfWork.new('queue:default', job_msg))
24
+ end
25
+
26
+ def initialize_middleware
27
+ Sidekiq.server_middleware do |chain|
28
+ chain.add Sidekiq::Middleware::Server::Seize
29
+ end
30
+ end
31
+
32
+ def initialize_worker_class(sidekiq_opts=nil)
33
+ worker_class_name = :TestDummyWorker
34
+ Object.send(:remove_const, worker_class_name) if Object.const_defined?(worker_class_name)
35
+ klass = Class.new do
36
+ include Sidekiq::Worker
37
+ sidekiq_options sidekiq_opts if sidekiq_opts
38
+ def perform
39
+ raise TEST_EXCEPTION, 'Oops'
40
+ end
41
+ end
42
+ Object.const_set(worker_class_name, klass)
43
+ end
44
+
45
+ def cleanup_redis
46
+ Sidekiq.redis {|c| c.flushdb }
47
+ end
48
+
49
+ shared_examples_for 'it should raise multiple errors' do
50
+ it 'throws exception on each retry' do
51
+ args ||= []
52
+ expect {
53
+ process_job(build_job_hash(worker_class, args))
54
+ }.to raise_error(TEST_EXCEPTION, 'Oops')
55
+ expect(Sidekiq::RetrySet.new.size).to eq(1)
56
+ retry_job = fetch_retry_job
57
+ expect(retry_job['retry_count']).to eq(0)
58
+ expect(retry_job['error_class']).to eq('ArgumentError')
59
+ expect(retry_job['error_message']).to eq('Oops')
60
+
61
+ expect {
62
+ process_job(retry_job.item)
63
+ }.to raise_error(TEST_EXCEPTION, 'Oops')
64
+ expect(Sidekiq::RetrySet.new.size).to eq(1)
65
+ retry_job = fetch_retry_job
66
+ expect(retry_job['retry_count']).to eq(1)
67
+ expect(retry_job['error_class']).to eq('ArgumentError')
68
+ expect(retry_job['error_message']).to eq('Oops')
69
+
70
+ expect {
71
+ process_job(retry_job.item)
72
+ }.to raise_error(TEST_EXCEPTION, 'Oops')
73
+ expect(Sidekiq::RetrySet.new.size).to eq(0)
74
+ end
75
+ end
76
+
77
+ shared_examples_for 'it should raise 1 errors' do
78
+ it 'raises the original error at the end' do
79
+ args ||= []
80
+ expect {
81
+ process_job(build_job_hash(worker_class, args))
82
+ }.to_not raise_error(TEST_EXCEPTION)
83
+ expect(Sidekiq::RetrySet.new.size).to eq(1)
84
+ retry_job = fetch_retry_job
85
+ expect(retry_job['retry_count']).to eq(0)
86
+
87
+ expect {
88
+ process_job(retry_job.item)
89
+ }.to_not raise_error(TEST_EXCEPTION)
90
+ expect(Sidekiq::RetrySet.new.size).to eq(1)
91
+ retry_job = fetch_retry_job
92
+ expect(retry_job['retry_count']).to eq(1)
93
+
94
+ expect {
95
+ process_job(retry_job.item)
96
+ }.to raise_error(TEST_EXCEPTION, 'Oops')
97
+ expect(Sidekiq::RetrySet.new.size).to eq(0)
98
+ end
99
+ end
100
+
101
+ shared_examples_for 'retry disabled, it should raise one error' do
102
+ it 'raises the error' do
103
+ args ||= []
104
+ expect {
105
+ process_job(build_job_hash(worker_class, args))
106
+ }.to raise_error(TEST_EXCEPTION, 'Oops')
107
+ expect(Sidekiq::RetrySet.new.size).to eq(0)
108
+ end
109
+ end
110
+
111
+ before(:each) do
112
+ cleanup_redis
113
+ end
114
+
115
+ context 'with default middleware config' do
116
+ before(:each) do
117
+ initialize_middleware
118
+ end
119
+
120
+ describe 'with nothing explicitly enabled' do
121
+ it_behaves_like 'retry disabled, it should raise one error' do
122
+ let!(:worker_class) { initialize_worker_class(retry: false) }
123
+ end
124
+
125
+ it_behaves_like 'retry disabled, it should raise one error' do
126
+ let!(:worker_class) { initialize_worker_class(retry: 0) }
127
+ end
128
+
129
+ it_behaves_like 'it should raise multiple errors' do
130
+ let!(:worker_class) { initialize_worker_class(retry: MAX_RETRY) }
131
+ end
132
+ end
133
+
134
+ describe 'with seize explicitly disabled' do
135
+ it_behaves_like 'retry disabled, it should raise one error' do
136
+ let!(:worker_class) { initialize_worker_class(seize: false, retry: 0) }
137
+ end
138
+
139
+ it_behaves_like 'retry disabled, it should raise one error' do
140
+ let!(:worker_class) { initialize_worker_class(seize: false, retry: false) }
141
+ end
142
+
143
+ it_behaves_like 'it should raise multiple errors' do
144
+ let!(:worker_class) { initialize_worker_class(seize: false, retry: MAX_RETRY) }
145
+ end
146
+ end
147
+
148
+ describe 'with seize explicitly enabled' do
149
+ it_behaves_like 'it should raise 1 errors' do
150
+ let!(:worker_class) { initialize_worker_class(seize: true, retry: MAX_RETRY) }
151
+ end
152
+
153
+ it_behaves_like 'retry disabled, it should raise one error' do
154
+ let!(:worker_class) { initialize_worker_class(seize: true, retry: false) }
155
+ end
156
+
157
+ it_behaves_like 'retry disabled, it should raise one error' do
158
+ let!(:worker_class) { initialize_worker_class(seize: true, retry: 0) }
159
+ end
160
+
161
+ it_behaves_like 'it should raise 1 errors' do
162
+ let!(:worker_class) { initialize_worker_class(seize: true, retry: MAX_RETRY, seize_exceptions_classes: [TEST_EXCEPTION]) }
163
+ end
164
+
165
+ it_behaves_like 'it should raise multiple errors' do
166
+ let!(:worker_class) { initialize_worker_class(seize: true, retry: MAX_RETRY, seize_exceptions_classes: [NoMemoryError]) }
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,102 @@
1
+ require 'sidekiq-seize'
2
+
3
+ $TESTING = true
4
+
5
+ Sidekiq::Logging.logger = nil
6
+
7
+ # This file was generated by the `rspec --init` command. Conventionally, all
8
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
9
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
10
+ # this file to always be loaded, without a need to explicitly require it in any
11
+ # files.
12
+ #
13
+ # Given that it is always loaded, you are encouraged to keep this file as
14
+ # light-weight as possible. Requiring heavyweight dependencies from this file
15
+ # will add to the boot time of your test suite on EVERY test run, even for an
16
+ # individual file that may not need all of that loaded. Instead, consider making
17
+ # a separate helper file that requires the additional dependencies and performs
18
+ # the additional setup, and require it from the spec files that actually need
19
+ # it.
20
+ #
21
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
+ # have no way to turn it off -- the option exists only for backwards
48
+ # compatibility in RSpec 3). It causes shared context metadata to be
49
+ # inherited by the metadata hash of host groups and examples, rather than
50
+ # triggering implicit auto-inclusion in groups with matching metadata.
51
+ config.shared_context_metadata_behavior = :apply_to_host_groups
52
+
53
+ # This allows you to limit a spec run to individual examples or groups
54
+ # you care about by tagging them with `:focus` metadata. When nothing
55
+ # is tagged with `:focus`, all examples get run. RSpec also provides
56
+ # aliases for `it`, `describe`, and `context` that include `:focus`
57
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
58
+ config.filter_run_when_matching :focus
59
+
60
+ # Allows RSpec to persist some state between runs in order to support
61
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
62
+ # you configure your source control system to ignore this file.
63
+ # config.example_status_persistence_file_path = "spec/examples.txt"
64
+
65
+ # Limits the available syntax to the non-monkey patched syntax that is
66
+ # recommended. For more details, see:
67
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
68
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
69
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
70
+ config.disable_monkey_patching!
71
+
72
+ # This setting enables warnings. It's recommended, but in some cases may
73
+ # be too noisy due to issues in dependencies.
74
+ config.warnings = true
75
+
76
+ # Many RSpec users commonly either run the entire suite or an individual
77
+ # file, and it's useful to allow more verbose output when running an
78
+ # individual spec file.
79
+ if config.files_to_run.one?
80
+ # Use the documentation formatter for detailed output,
81
+ # unless a formatter has already been configured
82
+ # (e.g. via a command-line flag).
83
+ config.default_formatter = "doc"
84
+ end
85
+
86
+ # Print the 10 slowest examples and example groups at the
87
+ # end of the spec run, to help surface which specs are running
88
+ # particularly slow.
89
+ config.profile_examples = 10
90
+
91
+ # Run specs in random order to surface order dependencies. If you find an
92
+ # order dependency and want to debug it, you can fix the order by providing
93
+ # the seed, which is printed after each run.
94
+ # --seed 1234
95
+ config.order = :random
96
+
97
+ # Seed global randomization in this process using the `--seed` CLI option.
98
+ # Setting this allows you to use `--seed` to deterministically reproduce
99
+ # test failures related to randomization by passing the same `--seed` value
100
+ # as the one that triggered the failure.
101
+ Kernel.srand config.seed
102
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-seize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Rajat Goyal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-13 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: 5.0.0
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.6.0
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '3.6'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.6.0
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '3.6'
53
+ - !ruby/object:Gem::Dependency
54
+ name: byebug
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ description: Sidekiq middleware that allows capturing exceptions and throwing only
68
+ after last retry, when the job goes to dead queue.
69
+ email:
70
+ - rajat@synaptic.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - Gemfile
78
+ - MIT-LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sidekiq-seize.rb
82
+ - lib/sidekiq/middleware/server/seize.rb
83
+ - sidekiq-seize.gemspec
84
+ - spec/seize_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage:
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.2.2
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.0.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Sidekiq middleware to silent errors and send only on dead
109
+ test_files:
110
+ - spec/seize_spec.rb
111
+ - spec/spec_helper.rb