sidekiq-squelch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c82ea2475d5c4a68ed3224c2274ca7c3400fc50
4
+ data.tar.gz: 8f9ca45149f4784a1f3d3c67f4021e0afcd39222
5
+ SHA512:
6
+ metadata.gz: 85229dc6cd716020d20d75281085d46d7568d1b8433a8aff444793b6263d466bae6712defa7e1f24cfa6a5029900f1fa1d5f02f5ef678184a6acab7ce7b26c58
7
+ data.tar.gz: 4ab7f40b6602003f558c0466302b27c393cfeb82c2c59e0fb74dea74d2d5dab3f1097368ec6294c3558e6d7a4b58503d18cb73de125d49b23b588687a33aed1b
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
15
+ *.rdb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
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-squelch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bob Breznak
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/Procfile ADDED
@@ -0,0 +1 @@
1
+ redis: redis-server
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Sidekiq::Squelch
2
+
3
+ Turns down the noise when executing Sidekiq jobs. Shit happens, its a fact. Sidekiq is pretty good at dealing with that by requing when an uncaught exception is thrown. And if you're monitoring errors using something like [Sentry](https://getsentry.com/) this can be a bit noisy when executing something that has a regular failures and recovery (i.e. network calls). Squelch allows you to set thresholds for common errors that occur so that you can cut down the noise and see when something is really going wrong.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sidekiq-squelch'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sidekiq-squelch
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/sidekiq-squelch/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new :spec
6
+
7
+ task default: :spec
@@ -0,0 +1,2 @@
1
+ :squelch:
2
+ - ['CaughtError', 10, 60]
@@ -0,0 +1,26 @@
1
+ require 'sidekiq'
2
+ require 'raven/base'
3
+ require './lib/sidekiq/squelch'
4
+ require 'byebug'
5
+
6
+ class CaughtError < StandardError; end
7
+ class UncaughtError < StandardError; end
8
+
9
+ class DerpyWorker
10
+ include Sidekiq::Worker
11
+
12
+ def perform(ex)
13
+ raise CaughtError if ex == 'caught'
14
+ raise UncaughtError
15
+ end
16
+ end
17
+
18
+ Raven.configure do |config|
19
+ config.dsn = 'https://7d3e06e5127f4f5e804584b7ab79c0b4:c19b1bb109e7470c9fe029fd6776e925@app.getsentry.com/31722'
20
+ end
21
+
22
+ handler = Sidekiq::Squelch::ErrorHandler.new Sidekiq::Squelch::Notifiers::Raven.new
23
+
24
+ Sidekiq.configure_server do |config|
25
+ config.error_handlers << handler
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'time'
2
+ require 'sidekiq'
3
+ require 'active_support/core_ext/string'
4
+
5
+ require 'sidekiq/squelch/version'
6
+ require 'sidekiq/squelch/middleware'
7
+ require 'sidekiq/squelch/error_handler'
8
+
9
+ require 'sidekiq/squelch/notifiers/raven'
@@ -0,0 +1,56 @@
1
+ module Sidekiq
2
+ module Squelch
3
+ class ErrorHandler
4
+ attr_accessor :notifier
5
+
6
+ def initialize(notifier)
7
+ @notifier = notifier
8
+ end
9
+
10
+ def call(ex, context)
11
+ increment_exception_counter ex
12
+ notifier.notify ex, context unless squelched? ex
13
+ end
14
+
15
+ private
16
+
17
+ def squelched?(ex)
18
+ config = config_for ex
19
+ return false unless config
20
+
21
+ time = (Time.now - config[:period]).to_f
22
+ key = key_for ex
23
+
24
+ count = Sidekiq.redis do |redis|
25
+ redis.zremrangebyscore key, '-inf', time
26
+ redis.zcount key, '-inf', '+inf'
27
+ end
28
+
29
+ count < config[:threshold]
30
+ end
31
+
32
+ def increment_exception_counter(ex)
33
+ return unless config_for ex
34
+
35
+ time = Time.now.to_f
36
+ key = key_for ex
37
+ value = { thrown_at: time, message: ex.message }.to_json
38
+
39
+ Sidekiq.redis { |redis| redis.zadd key, time, value }
40
+ end
41
+
42
+ def config_for(ex)
43
+ return unless Sidekiq.options[:squelch]
44
+
45
+ config = Sidekiq.options[:squelch].find { |s| s.first.to_s == ex.to_s }
46
+ return unless config
47
+
48
+ Hash[[:error, :threshold, :period].zip config]
49
+ end
50
+
51
+ def key_for(ex)
52
+ [:squelch, ex.class.to_s.underscore].join(':')
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ module Sidekiq
2
+ module Squelch
3
+ class Middleware
4
+ def initialize(notifier)
5
+ @notifier = notifier
6
+ end
7
+
8
+ def call(worker, msg, queue)
9
+ yield
10
+ rescue => ex
11
+ @handler ||= ErrorHandler.new
12
+ @handler.notifier = @notifier
13
+
14
+ @handler.call ex, {}
15
+
16
+ raise
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module Sidekiq
2
+ module Squelch
3
+ module Notifiers
4
+ class Raven
5
+ def notify(exception, context = {})
6
+ ::Raven.capture_exception exception, extra: { sidekiq: context }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Squelch
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/squelch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sidekiq-squelch'
8
+ spec.version = Sidekiq::Squelch::VERSION
9
+ spec.authors = ['Bob Breznak']
10
+ spec.email = ['bob.breznak@gmail.com']
11
+ spec.summary = 'Better error management for Sidekiq'
12
+ spec.homepage = 'https://github.com/bobbrez/sidekiq-squelch'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'sidekiq', '>= 3.0.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+
25
+ spec.add_development_dependency 'foreman', '~> 0.74.0'
26
+
27
+ spec.add_development_dependency 'pry', '~> 0.10.0'
28
+ spec.add_development_dependency 'pry-byebug', '~> 1.3.3'
29
+
30
+
31
+ spec.add_development_dependency 'timecop', '~> 0.7.1'
32
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
33
+ spec.add_development_dependency 'simplecov', '~> 0.9.0'
34
+ end
data/spec/config.yml ADDED
@@ -0,0 +1,2 @@
1
+ :squelch
2
+ - ['*', 20, 200]
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Sidekiq
4
+ module Squelch
5
+ describe Middleware do
6
+ let(:peroid) { 600 }
7
+ let(:threshold) { 6 }
8
+ let(:notifier) { BasicNotifier.new }
9
+ subject { Middleware.new notifier }
10
+
11
+ before(:each) { Sidekiq.redis { |redis| redis.flushall } }
12
+ before(:each) { Sidekiq.options = Sidekiq.options.merge squelch: [ [ 'BasicError', threshold, peroid] ] }
13
+
14
+ after(:each) { Sidekiq.options.delete :squelch }
15
+
16
+ it 'raises any errors encountered' do
17
+ expect {
18
+ subject.call(nil, nil, nil) { raise BasicError.new }
19
+ }.to raise_error
20
+ end
21
+
22
+ it 'increments the error count' do
23
+ expect(basic_error_count).to eq 0
24
+ expect { subject.call(nil, nil, nil) { raise BasicError.new } }.to raise_error
25
+ expect(basic_error_count).to eq 1
26
+ end
27
+
28
+ it 'it prunes the error count based on the peroid' do
29
+ Timecop.travel Time.now do
30
+ threshold.times {
31
+ expect {
32
+ subject.call(nil, nil, nil) { raise BasicError.new }
33
+ }.to raise_error
34
+ }
35
+ end
36
+
37
+ expect(notifier.count).to eq 0
38
+
39
+ Timecop.travel Time.now + peroid * 2 do
40
+ threshold.times {
41
+ expect {
42
+ subject.call(nil, nil, nil) { raise BasicError.new }
43
+ }.to raise_error
44
+ }
45
+ end
46
+
47
+ expect(notifier.count).to eq 0
48
+ end
49
+
50
+ it 'notifies after hitting the threshold' do
51
+ (threshold + 5).times {
52
+ expect {
53
+ subject.call(nil, nil, nil) { raise BasicError.new }
54
+ }.to raise_error
55
+ }
56
+
57
+ expect(notifier.count).to eq 5
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def basic_error_count
66
+ Sidekiq.redis { |redis| redis.zcount 'squelch:basic_error', '-inf', '+inf' }
67
+ end
68
+
69
+ class BasicError < StandardError; end
70
+ class DifferentError < StandardError; end
71
+
72
+ class BasicNotifier
73
+ attr_reader :exception, :options, :count
74
+
75
+ def initialize
76
+ reset
77
+ end
78
+
79
+ def reset
80
+ @count = 0
81
+ end
82
+
83
+ def notify(ex, **opts)
84
+ @count += 1
85
+ @exception = ex
86
+ @options = opts
87
+ end
88
+ end
@@ -0,0 +1,17 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'pry'
5
+ require 'pry-byebug'
6
+ require 'timecop'
7
+
8
+ Dir[File.join './spec/support/**/*.rb'].each { |f| require f }
9
+
10
+ require File.expand_path('../../lib/sidekiq/squelch', __FILE__)
11
+
12
+ RSpec.configure do |config|
13
+ config.profile_examples = 10
14
+
15
+ Kernel.srand config.seed
16
+ config.order = :random
17
+ end
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start
@@ -0,0 +1,16 @@
1
+ def redis_add_queues(queues)
2
+ Sidekiq.redis do |redis|
3
+ queues.each { |q| redis.sadd Dynamiq::QUEUE_LIST, q }
4
+ end
5
+ end
6
+
7
+ def redis_queue_job(dynamic_queue, score, args = nil)
8
+ payload = { retry: true, class: "Foo", jid: SecureRandom.hex, enqueued_at: Time.now.to_f }
9
+ payload.merge! queue: dynamic_queue, score: score, args: args
10
+
11
+ Sidekiq.redis do |redis|
12
+ redis.zadd [Dynamiq::QUEUE_LIST, dynamic_queue].join(':'), score, payload.to_json
13
+ end
14
+
15
+ payload
16
+ end
@@ -0,0 +1,4 @@
1
+ require 'sidekiq'
2
+
3
+ Sidekiq.logger = nil
4
+ Sidekiq.redis = { namespace: 'test' }
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-squelch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bob Breznak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-21 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.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
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: foreman
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.74.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.74.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.7.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.7.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.9.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.9.0
139
+ description:
140
+ email:
141
+ - bob.breznak@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - Gemfile
149
+ - LICENSE.txt
150
+ - Procfile
151
+ - README.md
152
+ - Rakefile
153
+ - example/config.yml
154
+ - example/sidekiq.rb
155
+ - lib/sidekiq/squelch.rb
156
+ - lib/sidekiq/squelch/error_handler.rb
157
+ - lib/sidekiq/squelch/middleware.rb
158
+ - lib/sidekiq/squelch/notifiers/raven.rb
159
+ - lib/sidekiq/squelch/version.rb
160
+ - sidekiq-squelch.gemspec
161
+ - spec/config.yml
162
+ - spec/lib/sidekiq/squelch/middleware_spec.rb
163
+ - spec/spec_helper.rb
164
+ - spec/support/coverage.rb
165
+ - spec/support/redis.rb
166
+ - spec/support/sidekiq.rb
167
+ homepage: https://github.com/bobbrez/sidekiq-squelch
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.2.2
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Better error management for Sidekiq
191
+ test_files:
192
+ - spec/config.yml
193
+ - spec/lib/sidekiq/squelch/middleware_spec.rb
194
+ - spec/spec_helper.rb
195
+ - spec/support/coverage.rb
196
+ - spec/support/redis.rb
197
+ - spec/support/sidekiq.rb
198
+ has_rdoc: