sidekiq_middlewares 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58d19e122710ee61085ace66515a1f856e6303d9
4
- data.tar.gz: 184c9d55a540287db3d3fa870681afe9199da7bd
3
+ metadata.gz: 5ce493c5d4b804657ed9b78b09bc19c796f29991
4
+ data.tar.gz: f67f58242c92f348aed2517783634c06130fcde0
5
5
  SHA512:
6
- metadata.gz: d29cddfc92e9aec4dde97bfdce4f49f84c893f1b37d2e2abe72b0d6f7810bd28b8e7d603cd51334da966d4069a016034de99a6149b3c495e8bffab406b159ee8
7
- data.tar.gz: 8d922e12bb3c3ccf27368d37dc55e3904d00565849fa096d1cad0c1cc2bdc379e305b82c13074cae41167ae73527a9f33ccb7accd4ad1157384d58de038d94d3
6
+ metadata.gz: c672f9bf8978c7867d08f228011023383a7769f95014a1718210f64b6ce735618531d1fa9768fea6453b0fdedd31faac3e0a1c24ab0616e8055090ed2d1731e1
7
+ data.tar.gz: 12da02767215af852fe6c297c5d66f0701f6a08ad08cbcac8a34c3fcea78578a58aad40a720072f959a1d0b790a2a4da0bac9c5b4a2a53f2844d0e9d9cefafc8
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
- --format documentation
2
1
  --color
3
2
  --require spec_helper
data/.travis.yml CHANGED
@@ -1,18 +1,18 @@
1
1
  language: ruby
2
- script: rspec spec -f d
2
+ script: bundle exec rake
3
+
4
+ before_install:
5
+ - gem update --system --no-document
3
6
 
4
7
  install:
5
8
  - gem update bundler
6
9
  - bundle install
7
10
 
8
11
  rvm:
9
- - 1.9
10
- - 2.0.0
11
- - 2.1.1
12
- - 2.1.2
13
12
  - 2.2.2
14
13
  - 2.3.1
15
14
  - 2.4.1
15
+ - 2.5.1
16
16
  - jruby-19mode
17
17
 
18
18
  - ruby-head
data/README.md CHANGED
@@ -14,25 +14,38 @@ Ruby Sidekiq Middlewares For common uses
14
14
  * this proc will receive one argument, a message object which is a Hash type
15
15
  * If the logger can handle Hash messages, such as TwP's [logging](https://github.com/TwP/logging) libary, than you don't need this
16
16
 
17
- ```ruby
18
- require 'json'
19
- require 'logger'
17
+ first you need a logger that respond to :info method, and format the message object into an expected format.
20
18
 
19
+ For simplicity, in the example this will be json.
20
+ ```ruby
21
21
  require 'json'
22
22
  require 'logger'
23
-
24
23
  json_logger = Logger.new(STDOUT)
25
24
  json_logger.formatter = proc do |severity, datetime, progname, msg|
26
25
  JSON.dump(msg) + "\n"
27
26
  end
27
+ ```
28
+
29
+ Than you can use it with the Benchmarker middleware like this:
28
30
 
31
+ ```ruby
29
32
  Sidekiq.configure_server do |config|
30
33
  config.redis = sidekiq_redis_config
31
34
 
32
35
  config.server_middleware do |chain|
33
36
  chain.add SidekiqMiddlewares::Benchmarker, logger: json_logger
34
- # or
35
- # chain.add SidekiqMiddlewares::Benchmarker, logger: Logger.new(STDOUT), formatter: proc { |m| JSON.dump(m) + "\n" }
37
+ end
38
+ end
39
+ ```
40
+
41
+ Or if you don't want to provide logger who also know about how to format, you can give an optional formatter to the middleware.
42
+
43
+ ```ruby
44
+ Sidekiq.configure_server do |config|
45
+ config.redis = sidekiq_redis_config
46
+
47
+ config.server_middleware do |chain|
48
+ chain.add SidekiqMiddlewares::Benchmarker, logger: Logger.new(STDOUT), formatter: proc { |m| JSON.dump(m) + "\n" }
36
49
  end
37
50
  end
38
51
  ```
@@ -1,4 +1,6 @@
1
1
  require "sidekiq_middlewares/version"
2
+
2
3
  module SidekiqMiddlewares
3
4
  autoload :Benchmarker, "sidekiq_middlewares/benchmarker"
5
+ autoload :MdcCleaner, "sidekiq_middlewares/mdc_cleaner"
4
6
  end
@@ -1,35 +1,37 @@
1
- require 'benchmark'
2
- require 'sidekiq_middlewares'
1
+ module SidekiqMiddlewares
2
+ class Benchmarker
3
3
 
4
- class SidekiqMiddlewares::Benchmarker
4
+ def initialize(opts = {})
5
+ @logger = opts[:logger] || raise(ArgumentError, 'missing keyword: logger')
6
+ @formatter = opts[:formatter] || proc { |message| message }
7
+ end
5
8
 
6
- def initialize(opts = {})
7
- @logger = opts[:logger] || raise(ArgumentError, 'missing keyword: logger')
8
- @formatter = opts[:formatter] || proc { |message| message }
9
- end
9
+ def call(_worker, job, _queue)
10
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
+ yield
12
+ ensure
13
+ elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
14
+ @logger.info(@formatter.call(message_to_log(job, elapsed_time)))
15
+ end
10
16
 
11
- def call(_worker, job, _queue, &next_middleware_call)
12
- measurement = Benchmark.measure(&next_middleware_call)
13
- @logger.info(@formatter.call(message_to_log(job, measurement)))
14
- end
17
+ private
15
18
 
16
- private
19
+ def message_to_log(job, measurement)
20
+ base_by(job).merge(results_by(measurement))
21
+ end
17
22
 
18
- def message_to_log(job, measurement)
19
- base_by(job).merge(results_by(measurement))
20
- end
21
-
22
- def base_by(job)
23
- {
24
- 'jid' => job['jid'],
25
- 'queue' => job['queue'],
26
- 'worker_class' => job['class'],
27
- 'created_at' => job['created_at'],
28
- 'enqueued_at' => job['enqueued_at']
29
- }
30
- end
23
+ def base_by(job)
24
+ {
25
+ 'jid' => job['jid'],
26
+ 'queue' => job['queue'],
27
+ 'worker_class' => job['class'],
28
+ 'created_at' => job['created_at'],
29
+ 'enqueued_at' => job['enqueued_at']
30
+ }
31
+ end
31
32
 
32
- def results_by(measurement)
33
- { 'execution_time_sec' => measurement.real }
33
+ def results_by(elapsed_time)
34
+ {'execution_time_sec' => elapsed_time}
35
+ end
34
36
  end
35
37
  end
@@ -0,0 +1,8 @@
1
+ module SidekiqMiddlewares
2
+ class MdcCleaner
3
+ def call(*_args)
4
+ Logging.mdc.clear if defined? Logging
5
+ yield
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module SidekiqMiddlewares
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -5,21 +5,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'sidekiq_middlewares/version'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = 'sidekiq_middlewares'
9
- spec.version = SidekiqMiddlewares::VERSION
10
- spec.authors = ['Emarsys Smart Insight Team']
11
- spec.email = ['smart-insight-dev@emarsys.com']
8
+ spec.name = 'sidekiq_middlewares'
9
+ spec.version = SidekiqMiddlewares::VERSION
10
+ spec.authors = ['Emarsys Smart Insight Team']
11
+ spec.email = ['smart-insight-dev@emarsys.com']
12
12
 
13
- spec.summary = 'Commonly reused Sidekiq Middlewares such as Benchmarker'
14
- spec.description = 'Commonly reused Sidekiq Middlewares such as Benchmarker'
15
- spec.homepage = 'https://github.com/emartech/sidekiq_middlewares'
13
+ spec.summary = 'Commonly reused Sidekiq Middlewares such as Benchmarker'
14
+ spec.description = 'Commonly reused Sidekiq Middlewares such as Benchmarker'
15
+ spec.homepage = 'https://github.com/emartech/sidekiq_middlewares'
16
+ spec.required_ruby_version = ">= 2.2.2"
16
17
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
19
  f.match(%r{^(test|spec|features)/})
19
20
  end
20
- spec.bindir = 'exe'
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ['lib']
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 1.14'
25
26
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_middlewares
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emarsys Smart Insight Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2018-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/setup
71
71
  - lib/sidekiq_middlewares.rb
72
72
  - lib/sidekiq_middlewares/benchmarker.rb
73
+ - lib/sidekiq_middlewares/mdc_cleaner.rb
73
74
  - lib/sidekiq_middlewares/version.rb
74
75
  - sidekiq_middlewares.gemspec
75
76
  homepage: https://github.com/emartech/sidekiq_middlewares
@@ -83,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
85
  - - ">="
85
86
  - !ruby/object:Gem::Version
86
- version: '0'
87
+ version: 2.2.2
87
88
  required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  requirements:
89
90
  - - ">="
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  version: '0'
92
93
  requirements: []
93
94
  rubyforge_project:
94
- rubygems_version: 2.5.1
95
+ rubygems_version: 2.6.13
95
96
  signing_key:
96
97
  specification_version: 4
97
98
  summary: Commonly reused Sidekiq Middlewares such as Benchmarker