checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8013e20dadb47994367e66c8bdbed92b6a0abf8d571d3bc6b7071d511dfe82b5
4
+ data.tar.gz: c6a9d958fb78125df046ad404093b3f41cde5fe5401a2dfc9a2cef79dc572779
5
+ SHA512:
6
+ metadata.gz: 81e265582b9f338f7e2ef4c1ba206130b6759544a0b0ecc009ebb3b5154c6389458f7aa61b94bbac9e3b7f1d0febde08d45551ac06bf5d4289cdec398ae5e188
7
+ data.tar.gz: 631c33cbbbc9ecfb9f6f72ebe29b74ac040ed0c99f500a62e4e38ac1b7e1f37bd79edaf424ee2d78144f31d5f8efece27f4401b8245d8091b6e82a0fab7faf4f
data/.gitignore ADDED
@@ -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
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -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).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -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__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=#x27;\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/sidekiq/haron.rb ADDED
@@ -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
data/lib/sidekiq/haron/client_middleware.rb ADDED
@@ -0,0 +1,10 @@
1
+ class Sidekiq::Haron::ClientMiddleware
2
+
3
+ def call(worker_class, msg, queue, redis_pool=nil)
4
+ if msg['retry_count'].blank? # don't store on retry
5
+ Sidekiq::Haron.transmitter.save(worker_class, msg, queue, redis_pool)
6
+ end
7
+ yield
8
+ end
9
+
10
+ end
data/lib/sidekiq/haron/exception_logger.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'sidekiq/exception_handler'
2
+ class Sidekiq::Haron::ExceptionLogger < Sidekiq::ExceptionHandler::Logger
3
+
4
+ def self.install
5
+ Sidekiq.error_handlers.delete_if{|eh| eh.is_a? Sidekiq::ExceptionHandler::Logger }
6
+ Sidekiq.error_handlers.unshift Sidekiq::Haron::ExceptionLogger.new
7
+ end
8
+
9
+ def call(ex, ctxHash)
10
+ jid = ctxHash.present? && ctxHash[:job]['jid']
11
+ Sidekiq::Haron.transmitter.load(jid) if jid.present?
12
+ Sidekiq::Haron.transmitter.tagged{ super(ex, ctxHash) }
13
+ end
14
+
15
+ end
data/lib/sidekiq/haron/formatter.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Sidekiq::Haron::Formatter < Sidekiq::Logging::Pretty
2
+
3
+ def call(severity, time, program_name, message)
4
+ result = "#{context} #{severity.to_s[0]}: #{message}\n"
5
+ if ENV['RAILS_LOG_TO_STDOUT'].present?
6
+ result
7
+ else
8
+ "#{time.utc.iso8601(3)} #{result}"
9
+ end
10
+ end
11
+
12
+ end
data/lib/sidekiq/haron/job_logger.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'sidekiq/job_logger'
2
+ class Sidekiq::Haron::JobLogger < Sidekiq::JobLogger
3
+
4
+ def call(item, queue)
5
+ Sidekiq::Haron.transmitter.load(item['jid'])
6
+ Sidekiq::Haron.transmitter.tagged do
7
+ Sidekiq.logger.info("with args #{item['args'].inspect}")
8
+ super
9
+ end
10
+ end
11
+
12
+ end
data/lib/sidekiq/haron/server_middleware.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Sidekiq::Haron::ServerMiddleware
2
+
3
+ def call(worker, msg, queue)
4
+ Sidekiq::Haron.transmitter.load(msg['jid'])
5
+ yield
6
+ end
7
+
8
+ end
data/lib/sidekiq/haron/storage.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Sidekiq::Haron::Storage
2
+ DEFAULT_EXPIRY = 30 * 24 * 60 * 60 # 30 days
3
+
4
+ protected
5
+
6
+ def store_for_id(id, data, redis_pool=nil)
7
+ redis_connection(redis_pool) do |conn|
8
+ conn.multi do
9
+ conn.hmset key(id), *(data.to_a.flatten(1))
10
+ conn.expire key(id), DEFAULT_EXPIRY
11
+ end[0]
12
+ end
13
+ end
14
+
15
+ def read_for_id(id)
16
+ Sidekiq.redis do |conn|
17
+ conn.hgetall(key(id))
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def redis_connection(redis_pool=nil)
24
+ if redis_pool
25
+ redis_pool.with do |conn|
26
+ yield conn
27
+ end
28
+ else
29
+ Sidekiq.redis do |conn|
30
+ yield conn
31
+ end
32
+ end
33
+ end
34
+
35
+ def key(id)
36
+ "sidekiq:haron:#{id}"
37
+ end
38
+ end
data/lib/sidekiq/haron/transmitter.rb ADDED
@@ -0,0 +1,39 @@
1
+ class Sidekiq::Haron::Transmitter
2
+ include Sidekiq::Haron::Storage
3
+
4
+ def save worker_class, msg, queue, redis_pool
5
+ data = saved_data(worker_class, msg, queue)
6
+ store_for_id msg['jid'], data, redis_pool
7
+ end
8
+
9
+ def load jid
10
+ load_data read_for_id(jid)
11
+ rescue => e
12
+ Sidekiq.logger.error "loading data error - #{e.to_s}"
13
+ {}
14
+ end
15
+
16
+ def tagged
17
+ Rails.logger.tagged(tags) do
18
+ Sidekiq.logger.tagged(tags) do
19
+ yield
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ def tags
26
+ []
27
+ end
28
+
29
+ private
30
+
31
+ def saved_data
32
+ raise NotImplemented
33
+ end
34
+
35
+ def load_data
36
+ raise NotImplemented
37
+ end
38
+
39
+ end
data/lib/sidekiq/haron/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Haron
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
data/sidekiq-haron.gemspec ADDED
@@ -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.1
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: []