sidekiq-haron 0.1.2

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: 7a9131278d68746096c17ea16bd644f4dd043afcae8d20904e960a0d6d7cb7bc
4
+ data.tar.gz: b5894f6abb499eb92aaac11ab7c7b1acd62f7da1ac1f42681a8693b6fbc64572
5
+ SHA512:
6
+ metadata.gz: cd25ecc4856efaa674bf4855cdf417ce66c173d8d520ca94d63c9ea9d343d4e0947db93b5e2300c63b84ee985bf8877ba74c6eb1affc5b23afbe6a5b7e04c853
7
+ data.tar.gz: 5ec609ea9aaec81c856ea477956ec3df8f73a99b7885d622d4a501bc97482aa5dd528235a411741ac5b0092ec7b162caf8bc7df38834d3768af63a177712f466
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sidekiq-haron.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Serg F
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,73 @@
1
+ # Sidekiq::Haron
2
+
3
+ Transfer some metadata to sidekiq job, can tag sidekiq logs and add logging job args. Example request_id or other request info.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sidekiq-haron'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sidekiq-haron
20
+
21
+ ## Usage
22
+
23
+ Аdd to project class inheriting from `Sidekiq::Haron::Transmitter` like this:
24
+
25
+ ```ruby
26
+ class HaronTransmitter < Sidekiq::Haron::Transmitter
27
+
28
+ def saved_data *args
29
+ {
30
+ request_id: Current.request_id,
31
+ parent_request_id: Current.parent_request_id
32
+ }
33
+ end
34
+
35
+ def load_data hash
36
+ Current.request_id = hash['request_id'].presence || SecureRandom.hex
37
+ Current.parent_request_id = hash['parent_request_id']
38
+ end
39
+
40
+ def tags
41
+ [Current.parent_request_id]
42
+ end
43
+
44
+ end
45
+ ```
46
+
47
+ Аdd to `config/initializers/sidekiq.rb`:
48
+
49
+ ```ruby
50
+ Sidekiq::Haron.install(HaronTransmitter)
51
+ ```
52
+
53
+ Now all your sidekiq log have `Current.parent_request_id` value as tag and log job args too:
54
+
55
+ ```
56
+ 2020-01-14T14:22:09.035Z TestWorker JID-940645f14b345b3b4031d1cc I: [4f445354] with args [1, {"q"=>2}]
57
+ 2020-01-14T14:22:09.035Z TestWorker JID-940645f14b345b3b4031d1cc I: [4f445354] start
58
+ 2020-01-14T14:22:09.041Z TestWorker JID-940645f14b345b3b4031d1cc I: [4f445354] done: 0.006 sec
59
+ ```
60
+
61
+ ## Development
62
+
63
+ 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.
64
+
65
+ 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).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Rnd-Soft/sidekiq-haron.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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 "tttt"
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(__FILE__)
@@ -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,59 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/haron/formatter'
3
+ require 'sidekiq/haron/storage'
4
+ require 'sidekiq/haron/transmitter'
5
+ require 'sidekiq/haron/job_logger'
6
+ require 'sidekiq/haron/exception_logger'
7
+ require 'sidekiq/haron/client_middleware'
8
+ require 'sidekiq/haron/server_middleware'
9
+ require 'sidekiq/haron/version'
10
+
11
+ module Sidekiq
12
+ module Haron
13
+
14
+ def self.transmitter
15
+ Sidekiq.options[:transmitter]
16
+ end
17
+
18
+ def self.transmitter= v
19
+ Sidekiq.options[:transmitter] = v
20
+ end
21
+
22
+ def self.install transmitter_class
23
+ Sidekiq::Haron.transmitter = transmitter_class.new
24
+ set_loggers
25
+ Sidekiq.configure_server do |c|
26
+ configure_client_middleware(c)
27
+ configure_server_middleware(c)
28
+ end
29
+ Sidekiq.configure_client do |c|
30
+ configure_client_middleware(c)
31
+ end
32
+ end
33
+
34
+ def self.set_loggers
35
+ Sidekiq.options[:job_logger] = Sidekiq::Haron::JobLogger
36
+ Sidekiq.logger.formatter = Sidekiq::Haron::Formatter.new
37
+ Sidekiq.logger = ActiveSupport::TaggedLogging.new(Sidekiq.logger)
38
+ Sidekiq::Haron::ExceptionLogger.install
39
+ end
40
+
41
+ def self.configure_client_middleware(sidekiq_config)
42
+ sidekiq_config.client_middleware do |chain|
43
+ chain.add Sidekiq::Haron::ClientMiddleware
44
+ end
45
+ end
46
+
47
+ def self.configure_server_middleware(sidekiq_config)
48
+ sidekiq_config.server_middleware do |chain|
49
+ if Sidekiq.major_version < 5
50
+ chain.insert_after Sidekiq::Middleware::Server::Logging,
51
+ Sidekiq::Haron::ServerMiddleware
52
+ else
53
+ chain.add Sidekiq::Haron::ServerMiddleware
54
+ end
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ module Sidekiq
2
+ module Haron
3
+ class ClientMiddleware
4
+
5
+ def call(worker_class, msg, queue, redis_pool=nil)
6
+ if msg['retry_count'].blank? # don't store on retry
7
+ Sidekiq::Haron.transmitter.save(worker_class, msg, queue, redis_pool)
8
+ end
9
+ yield
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'sidekiq/exception_handler'
2
+ module Sidekiq
3
+ module Haron
4
+ class ExceptionLogger < Sidekiq::ExceptionHandler::Logger
5
+
6
+ def self.install
7
+ Sidekiq.error_handlers.delete_if{|eh| eh.is_a? Sidekiq::ExceptionHandler::Logger }
8
+ Sidekiq.error_handlers.unshift Sidekiq::Haron::ExceptionLogger.new
9
+ end
10
+
11
+ def call(ex, ctxHash)
12
+ jid = ctxHash.present? && ctxHash[:job]['jid']
13
+ Sidekiq::Haron.transmitter.load(jid) if jid.present?
14
+ Sidekiq::Haron.transmitter.tagged{ super(ex, ctxHash) }
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module Sidekiq
2
+ module Haron
3
+ class Formatter < Sidekiq::Logging::Pretty
4
+
5
+ def call(severity, time, program_name, message)
6
+ result = "#{context} #{severity.to_s[0]}: #{message}\n"
7
+ if ENV['RAILS_LOG_TO_STDOUT'].present?
8
+ result
9
+ else
10
+ "#{time.utc.iso8601(3)} #{result}"
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'sidekiq/job_logger'
2
+ module Sidekiq
3
+ module Haron
4
+ class JobLogger < Sidekiq::JobLogger
5
+
6
+ def call(item, queue)
7
+ Sidekiq::Haron.transmitter.load(item['jid'])
8
+ Sidekiq::Haron.transmitter.tagged do
9
+ Sidekiq.logger.info("with args #{item['args'].inspect}")
10
+ super
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module Sidekiq
2
+ module Haron
3
+ class ServerMiddleware
4
+
5
+ def call(worker, msg, queue)
6
+ Sidekiq::Haron.transmitter.load(msg['jid'])
7
+ yield
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ module Sidekiq
2
+ module Haron
3
+ module Storage
4
+
5
+ DEFAULT_EXPIRY = 30 * 24 * 60 * 60 # 30 days
6
+
7
+ protected
8
+
9
+ def store_for_id(id, data, redis_pool=nil)
10
+ redis_connection(redis_pool) do |conn|
11
+ conn.multi do
12
+ conn.hmset key(id), *(data.to_a.flatten(1))
13
+ conn.expire key(id), DEFAULT_EXPIRY
14
+ end[0]
15
+ end
16
+ end
17
+
18
+ def read_for_id(id)
19
+ Sidekiq.redis do |conn|
20
+ conn.hgetall(key(id))
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def redis_connection(redis_pool=nil)
27
+ if redis_pool
28
+ redis_pool.with do |conn|
29
+ yield conn
30
+ end
31
+ else
32
+ Sidekiq.redis do |conn|
33
+ yield conn
34
+ end
35
+ end
36
+ end
37
+
38
+ def key(id)
39
+ "sidekiq:haron:#{id}"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ module Sidekiq
2
+ module Haron
3
+ class Transmitter
4
+ include Sidekiq::Haron::Storage
5
+
6
+ def save worker_class, msg, queue, redis_pool
7
+ data = saved_data(worker_class, msg, queue)
8
+ store_for_id msg['jid'], data, redis_pool
9
+ end
10
+
11
+ def load jid
12
+ load_data read_for_id(jid)
13
+ rescue => e
14
+ Sidekiq.logger.error "loading data error - #{e.to_s}"
15
+ {}
16
+ end
17
+
18
+ def tagged
19
+ Rails.logger.tagged(tags) do
20
+ Sidekiq.logger.tagged(tags) do
21
+ yield
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ def tags
28
+ []
29
+ end
30
+
31
+ private
32
+
33
+ def saved_data
34
+ raise NotImplemented
35
+ end
36
+
37
+ def load_data
38
+ raise NotImplemented
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Haron
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sidekiq/haron/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-haron"
8
+ spec.version = Sidekiq::Haron::VERSION
9
+ spec.authors = ["Serg F"]
10
+ spec.email = ["wolferingys@gmail.com"]
11
+
12
+ spec.summary = %q{Transfer some data to sidekiq job and can tag sidekiq logs}
13
+ spec.description = %q{Transfer some metadata to sidekiq job, can tag sidekiq logs and add logging job args}
14
+ spec.homepage = "https://github.com/Rnd-Soft/sidekiq-haron"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "sidekiq", "~> 5.2"
27
+ spec.add_dependency "rails"
28
+ spec.add_development_dependency "bundler", "~> 1.17"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "awesome_print"
32
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-haron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Serg F
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-14 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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
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.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
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: awesome_print
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
+ description: Transfer some metadata to sidekiq job, can tag sidekiq logs and add logging
98
+ job args
99
+ email:
100
+ - wolferingys@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/sidekiq/haron.rb
114
+ - lib/sidekiq/haron/client_middleware.rb
115
+ - lib/sidekiq/haron/exception_logger.rb
116
+ - lib/sidekiq/haron/formatter.rb
117
+ - lib/sidekiq/haron/job_logger.rb
118
+ - lib/sidekiq/haron/server_middleware.rb
119
+ - lib/sidekiq/haron/storage.rb
120
+ - lib/sidekiq/haron/transmitter.rb
121
+ - lib/sidekiq/haron/version.rb
122
+ - sidekiq-haron.gemspec
123
+ homepage: https://github.com/Rnd-Soft/sidekiq-haron
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.0.6
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Transfer some data to sidekiq job and can tag sidekiq logs
146
+ test_files: []