ears 0.3.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f0fa1b69a334f5ec5d649478a46d0c78298621a2c879c3b62457b8dd41723fe
4
- data.tar.gz: e34d3bee6f15435ca5c9847b384aad75e6acd3ccd06dbb9a751a798f63f3a077
3
+ metadata.gz: b1bc62c3b6367b99bdd80d44f670f79bd38709587df8e0bbcb72201772b99a74
4
+ data.tar.gz: b2fd12a20e5b0879fc1a69efe3729e6a422f8cc20f57007098a4da21d0198b4c
5
5
  SHA512:
6
- metadata.gz: 4d914d9a7e5ce22b6595899952626bbbec3b1885a6ff0b4fbf768560d4382dadb64578a9c305d0573afba36918e0eccd8c704ccfb681a76f606a09cf8b23b2c6
7
- data.tar.gz: f6f95145e4c70a565cd95755602116755e79000ca530883aadcf8d9461aacdf3d6828bf67533ed7f7458471473972839ef5745b1e44c933705678be119260f63
6
+ metadata.gz: 21299077971e38f928ef72d047726ad99218fc6d4b3c0d73cdd9a179f0cf87d8d5fab6f129a7d4c5dd20d97190d3d1d73daf81f229f3bd79cb21d60a73c9af95
7
+ data.tar.gz: 8e92b3f7bba6f494fd563f210dd43f03a95e4df0bf1f722dd1a6a3052fee9c82038679d8e7568a0de5e1e6581cdb343caa6092cc151f63890c2105a0da3b8134
data/.tool-versions CHANGED
@@ -1,2 +1,2 @@
1
- ruby 3.0.0
2
- nodejs 15.11.0
1
+ ruby 3.0.2
2
+ nodejs 16.9.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.0 (2021-11-18)
4
+
5
+ - add `connection_name` to configuration and make it a mandatory setting
6
+ - validate configuration after calling `configure`
7
+
8
+ ## 0.5.0 (2021-11-03)
9
+
10
+ - fix the fact that the configuration was never used and bunny silently fell back to the `RABBITMQ_URL` env var
11
+
12
+ ## 0.4.3 (2021-07-26)
13
+
14
+ ### Changes
15
+
16
+ - Ears will not exit gracefully on unhandled errors anymore. You have to take care of proper flushing and cleanup yourself (see README for example).
17
+
18
+ ## 0.3.3 (2021-06-07)
19
+
20
+ ### Changes
21
+
22
+ - Ears will now exit gracefully on unhandled errors to give the application a chance to do flushing and cleanup work
23
+
3
24
  ## 0.3.2 (2021-05-21)
4
25
 
5
26
  ### Changes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ears (0.3.2)
4
+ ears (0.6.0)
5
5
  bunny
6
6
  multi_json
7
7
 
@@ -10,18 +10,20 @@ GEM
10
10
  specs:
11
11
  amq-protocol (2.3.2)
12
12
  ast (2.4.2)
13
- bunny (2.17.0)
13
+ bunny (2.19.0)
14
14
  amq-protocol (~> 2.3, >= 2.3.1)
15
+ sorted_set (~> 1, >= 1.0.2)
15
16
  diff-lcs (1.4.4)
16
17
  multi_json (1.15.0)
17
18
  parallel (1.20.1)
18
- parser (3.0.0.0)
19
+ parser (3.0.2.0)
19
20
  ast (~> 2.4.1)
20
21
  rainbow (3.0.0)
21
- rake (13.0.3)
22
+ rake (13.0.6)
23
+ rbtree (0.4.4)
22
24
  redcarpet (3.5.1)
23
25
  regexp_parser (2.1.1)
24
- rexml (3.2.4)
26
+ rexml (3.2.5)
25
27
  rspec (3.10.0)
26
28
  rspec-core (~> 3.10.0)
27
29
  rspec-expectations (~> 3.10.0)
@@ -35,27 +37,32 @@ GEM
35
37
  diff-lcs (>= 1.2.0, < 2.0)
36
38
  rspec-support (~> 3.10.0)
37
39
  rspec-support (3.10.2)
38
- rubocop (1.12.0)
40
+ rubocop (1.20.0)
39
41
  parallel (~> 1.10)
40
42
  parser (>= 3.0.0.0)
41
43
  rainbow (>= 2.2.2, < 4.0)
42
44
  regexp_parser (>= 1.8, < 3.0)
43
45
  rexml
44
- rubocop-ast (>= 1.2.0, < 2.0)
46
+ rubocop-ast (>= 1.9.1, < 2.0)
45
47
  ruby-progressbar (~> 1.7)
46
48
  unicode-display_width (>= 1.4.0, < 3.0)
47
- rubocop-ast (1.4.1)
48
- parser (>= 2.7.1.5)
49
- rubocop-rake (0.5.1)
50
- rubocop
51
- rubocop-rspec (2.2.0)
49
+ rubocop-ast (1.11.0)
50
+ parser (>= 3.0.1.1)
51
+ rubocop-rake (0.6.0)
52
+ rubocop (~> 1.0)
53
+ rubocop-rspec (2.4.0)
52
54
  rubocop (~> 1.0)
53
55
  rubocop-ast (>= 1.1.0)
54
56
  ruby-progressbar (1.11.0)
57
+ set (1.0.1)
58
+ sorted_set (1.0.3)
59
+ rbtree
60
+ set (~> 1.0)
55
61
  unicode-display_width (2.0.0)
56
62
  yard (0.9.26)
57
63
 
58
64
  PLATFORMS
65
+ arm64-darwin-20
59
66
  x86_64-darwin-20
60
67
  x86_64-linux
61
68
 
@@ -70,4 +77,4 @@ DEPENDENCIES
70
77
  yard
71
78
 
72
79
  BUNDLED WITH
73
- 2.2.3
80
+ 2.2.22
data/README.md CHANGED
@@ -22,16 +22,19 @@ Or install it yourself as:
22
22
 
23
23
  ### Basic usage
24
24
 
25
- First, you should configure where `Ears` should connect to.
25
+ First, you should configure `Ears`.
26
26
 
27
27
  ```ruby
28
28
  require 'ears'
29
29
 
30
30
  Ears.configure do |config|
31
31
  config.rabbitmq_url = 'amqp://user:password@myrmq:5672'
32
+ config.connection_name = 'My Consumer'
32
33
  end
33
34
  ```
34
35
 
36
+ _Note_: `connection_name` is a mandatory setting!
37
+
35
38
  Next, define your exchanges, queues, and consumers by calling `Ears.setup`.
36
39
 
37
40
  ```ruby
@@ -63,6 +66,16 @@ class MyConsumer < Ears::Consumer
63
66
  end
64
67
  ```
65
68
 
69
+ And, do not forget to run it. Be prepared that unhandled errors will be reraised. So, take care of cleanup work.
70
+
71
+ ```ruby
72
+ begin
73
+ Ears.run!
74
+ ensure
75
+ # all your cleanup work goes here...
76
+ end
77
+ ```
78
+
66
79
  At the end of the `#work` method, you must always return `ack`, `reject`, or `requeue` to signal what should be done with the message.
67
80
 
68
81
  ### Middlewares
data/ears.gemspec CHANGED
@@ -19,6 +19,12 @@ Gem::Specification.new do |spec|
19
19
  spec.metadata['changelog_uri'] =
20
20
  'https://github.com/ivx/ears/blob/master/CHANGELOG.md'
21
21
 
22
+ spec.post_install_message =
23
+ '
24
+ Ears: the new version changed the exit behaviour in case of uncaught exceptions.
25
+ You may want to have a look into the CHANGELOG!
26
+ '
27
+
22
28
  spec.files =
23
29
  Dir.chdir(File.expand_path('..', __FILE__)) do
24
30
  `git ls-files -z`.split("\x0").reject do |f|
@@ -1,13 +1,23 @@
1
1
  module Ears
2
2
  # The class representing the global {Ears} configuration.
3
3
  class Configuration
4
+ class ConnectionNameMissing < StandardError
5
+ end
6
+
4
7
  DEFAULT_RABBITMQ_URL = 'amqp://guest:guest@localhost:5672'
5
8
 
6
9
  # @return [String] the connection string for RabbitMQ.
7
10
  attr_accessor :rabbitmq_url
8
11
 
12
+ # @return [String] the name for the RabbitMQ connection.
13
+ attr_accessor :connection_name
14
+
9
15
  def initialize
10
16
  @rabbitmq_url = DEFAULT_RABBITMQ_URL
11
17
  end
18
+
19
+ def validate!
20
+ raise ConnectionNameMissing unless connection_name
21
+ end
12
22
  end
13
23
  end
data/lib/ears/consumer.rb CHANGED
@@ -46,11 +46,14 @@ module Ears
46
46
  # @param [String] payload The payload of the received message.
47
47
  # @raise [InvalidReturnError] if you return something other than +:ack+, +:reject+ or +:requeue+ from {#work}.
48
48
  def process_delivery(delivery_info, metadata, payload)
49
- self.class.middlewares.reverse.reduce(
50
- work_proc,
51
- ) do |next_middleware, middleware|
52
- nest_middleware(middleware, next_middleware)
53
- end.call(delivery_info, metadata, payload)
49
+ self
50
+ .class
51
+ .middlewares
52
+ .reverse
53
+ .reduce(work_proc) do |next_middleware, middleware|
54
+ nest_middleware(middleware, next_middleware)
55
+ end
56
+ .call(delivery_info, metadata, payload)
54
57
  end
55
58
 
56
59
  protected
@@ -0,0 +1,46 @@
1
+ require 'ears/middleware'
2
+
3
+ module Ears
4
+ module Middlewares
5
+ # A middleware that automatically puts messages on an error queue when the specified number of retries are exceeded.
6
+ class MaxRetries < Middleware
7
+ # @param [Hash] opts The options for the middleware.
8
+ # @option opts [Integer] :retries The number of retries before the message is sent to the error queue.
9
+ # @option opts [String] :error_queue The name of the queue where messages should be sent to when the max retries are reached.
10
+ def initialize(opts)
11
+ super()
12
+ @retries = opts.fetch(:retries)
13
+ @error_queue = opts.fetch(:error_queue)
14
+ end
15
+
16
+ def call(delivery_info, metadata, payload, app)
17
+ return handle_exceeded(payload) if retries_exceeded?(metadata)
18
+ app.call(delivery_info, metadata, payload)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :retries, :error_queue
24
+
25
+ def handle_exceeded(payload)
26
+ Bunny::Exchange
27
+ .default(Ears.channel)
28
+ .publish(payload, routing_key: error_queue)
29
+ :ack
30
+ end
31
+
32
+ def retries_exceeded?(metadata)
33
+ rejected_deaths =
34
+ metadata
35
+ .headers
36
+ .fetch('x-death', [])
37
+ .select { |death| death['reason'] == 'rejected' }
38
+
39
+ return false unless rejected_deaths.any?
40
+
41
+ retry_count = rejected_deaths.map { |death| death['count'] }.max
42
+ retry_count > @retries
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/ears/setup.rb CHANGED
@@ -62,7 +62,7 @@ module Ears
62
62
  .create_channel(nil, 1, true)
63
63
  .tap do |channel|
64
64
  channel.prefetch(args.fetch(:prefetch, 1))
65
- channel.on_uncaught_exception { |error| Thread.main.raise(error) }
65
+ channel.on_uncaught_exception { |error| Ears.error!(error) }
66
66
  end
67
67
  end
68
68
 
data/lib/ears/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ears
2
- VERSION = '0.3.2'
2
+ VERSION = '0.6.0'
3
3
  end
data/lib/ears.rb CHANGED
@@ -18,13 +18,20 @@ module Ears
18
18
  # @yieldparam configuration [Ears::Configuration] The global configuration instance.
19
19
  def configure
20
20
  yield(configuration)
21
+ configuration.validate!
21
22
  end
22
23
 
23
24
  # The global RabbitMQ connection used by Ears.
24
25
  #
25
26
  # @return [Bunny::Session]
26
27
  def connection
27
- @connection ||= Bunny.new.tap { |conn| conn.start }
28
+ @connection ||=
29
+ Bunny
30
+ .new(
31
+ configuration.rabbitmq_url,
32
+ connection_name: configuration.connection_name,
33
+ )
34
+ .tap { |conn| conn.start }
28
35
  end
29
36
 
30
37
  # The channel for the current thread.
@@ -36,7 +43,7 @@ module Ears
36
43
  .create_channel(nil, 1, true)
37
44
  .tap do |channel|
38
45
  channel.prefetch(1)
39
- channel.on_uncaught_exception { |error| Thread.main.raise(error) }
46
+ channel.on_uncaught_exception { |error| Ears.error!(error) }
40
47
  end
41
48
  end
42
49
 
@@ -51,12 +58,22 @@ module Ears
51
58
  running = true
52
59
  Signal.trap('INT') { running = false }
53
60
  Signal.trap('TERM') { running = false }
54
- sleep 1 while running
61
+ sleep 1 while running && @error.nil?
62
+ raise @error if @error
63
+ end
64
+
65
+ # Signals that an uncaught error has occurred and the process should be stopped.
66
+ #
67
+ # @param [Exception] error The unhandled error that occurred.
68
+ def error!(error)
69
+ puts(error.full_message)
70
+ @error = error
55
71
  end
56
72
 
57
73
  # Used internally for testing.
58
74
  def reset!
59
75
  @connection = nil
76
+ @configuration = nil
60
77
  Thread.current[:ears_channel] = nil
61
78
  end
62
79
  end
data/package-lock.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "ears",
3
+ "lockfileVersion": 2,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "name": "ears",
8
+ "devDependencies": {
9
+ "@invisionag/prettier-config-ivx": "^3.0.1",
10
+ "@prettier/plugin-ruby": "^2.0.0",
11
+ "prettier": "^2.4.1"
12
+ }
13
+ },
14
+ "node_modules/@invisionag/prettier-config-ivx": {
15
+ "version": "3.0.1",
16
+ "resolved": "https://registry.npmjs.org/@invisionag/prettier-config-ivx/-/prettier-config-ivx-3.0.1.tgz",
17
+ "integrity": "sha512-l9oq6oWH/5f55StS8btmB4LfebfziO9tOY62ItJBwF4nKeMgI/iKBJ97r9Sqkxbq3qOKERuG1u4rAStX9m9GTg==",
18
+ "dev": true,
19
+ "peerDependencies": {
20
+ "prettier": ">=2.0.0"
21
+ }
22
+ },
23
+ "node_modules/@prettier/plugin-ruby": {
24
+ "version": "2.0.0",
25
+ "resolved": "https://registry.npmjs.org/@prettier/plugin-ruby/-/plugin-ruby-2.0.0.tgz",
26
+ "integrity": "sha512-pA0rjTi5J7cT86XPNhXp7CpdV2Tlyj5oqDIsnQRxMu2P7LY2KJI/pyOSM8pzTH8BgRyQfe1P1NPCcTwxUnUWtQ==",
27
+ "dev": true,
28
+ "dependencies": {
29
+ "prettier": ">=2.3.0"
30
+ }
31
+ },
32
+ "node_modules/prettier": {
33
+ "version": "2.4.1",
34
+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.4.1.tgz",
35
+ "integrity": "sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA==",
36
+ "dev": true,
37
+ "bin": {
38
+ "prettier": "bin-prettier.js"
39
+ },
40
+ "engines": {
41
+ "node": ">=10.13.0"
42
+ }
43
+ }
44
+ },
45
+ "dependencies": {
46
+ "@invisionag/prettier-config-ivx": {
47
+ "version": "3.0.1",
48
+ "resolved": "https://registry.npmjs.org/@invisionag/prettier-config-ivx/-/prettier-config-ivx-3.0.1.tgz",
49
+ "integrity": "sha512-l9oq6oWH/5f55StS8btmB4LfebfziO9tOY62ItJBwF4nKeMgI/iKBJ97r9Sqkxbq3qOKERuG1u4rAStX9m9GTg==",
50
+ "dev": true,
51
+ "requires": {}
52
+ },
53
+ "@prettier/plugin-ruby": {
54
+ "version": "2.0.0",
55
+ "resolved": "https://registry.npmjs.org/@prettier/plugin-ruby/-/plugin-ruby-2.0.0.tgz",
56
+ "integrity": "sha512-pA0rjTi5J7cT86XPNhXp7CpdV2Tlyj5oqDIsnQRxMu2P7LY2KJI/pyOSM8pzTH8BgRyQfe1P1NPCcTwxUnUWtQ==",
57
+ "dev": true,
58
+ "requires": {
59
+ "prettier": ">=2.3.0"
60
+ }
61
+ },
62
+ "prettier": {
63
+ "version": "2.4.1",
64
+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.4.1.tgz",
65
+ "integrity": "sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA==",
66
+ "dev": true
67
+ }
68
+ }
69
+ }
data/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
- "name": "on-prem-importer",
2
+ "name": "ears",
3
3
  "private": true,
4
4
  "scripts": {
5
5
  "prettify": "prettier \"**/*.{ru,rb,yml,yaml,md,gemspec,json}\" --ignore-path=\".gitignore\"",
6
- "lint": "yarn prettify --check",
7
- "format": "yarn prettify --write"
6
+ "lint": "npm run prettify -- --check",
7
+ "format": "npm run prettify -- --write"
8
8
  },
9
9
  "devDependencies": {
10
- "@invisionag/prettier-config-ivx": "^2.1.1",
11
- "@prettier/plugin-ruby": "^1.5.2",
12
- "prettier": "^2.2.0"
10
+ "@invisionag/prettier-config-ivx": "^3.0.1",
11
+ "@prettier/plugin-ruby": "^2.0.0",
12
+ "prettier": "^2.4.1"
13
13
  },
14
14
  "prettier": "@invisionag/prettier-config-ivx"
15
15
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ears
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Mainz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-21 00:00:00.000000000 Z
11
+ date: 2021-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -163,10 +163,11 @@ files:
163
163
  - lib/ears/middleware.rb
164
164
  - lib/ears/middlewares/appsignal.rb
165
165
  - lib/ears/middlewares/json.rb
166
+ - lib/ears/middlewares/max_retries.rb
166
167
  - lib/ears/setup.rb
167
168
  - lib/ears/version.rb
169
+ - package-lock.json
168
170
  - package.json
169
- - yarn.lock
170
171
  homepage: https://github.com/ivx/ears
171
172
  licenses:
172
173
  - MIT
@@ -175,7 +176,10 @@ metadata:
175
176
  homepage_uri: https://github.com/ivx/ears
176
177
  source_code_uri: https://github.com/ivx/ears
177
178
  changelog_uri: https://github.com/ivx/ears/blob/master/CHANGELOG.md
178
- post_install_message:
179
+ post_install_message: |2
180
+
181
+ Ears: the new version changed the exit behaviour in case of uncaught exceptions.
182
+ You may want to have a look into the CHANGELOG!
179
183
  rdoc_options: []
180
184
  require_paths:
181
185
  - lib
@@ -190,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
194
  - !ruby/object:Gem::Version
191
195
  version: '0'
192
196
  requirements: []
193
- rubygems_version: 3.2.3
197
+ rubygems_version: 3.2.22
194
198
  signing_key:
195
199
  specification_version: 4
196
200
  summary: A gem for building RabbitMQ consumers.
data/yarn.lock DELETED
@@ -1,20 +0,0 @@
1
- # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
- # yarn lockfile v1
3
-
4
-
5
- "@invisionag/prettier-config-ivx@^2.1.1":
6
- version "2.1.1"
7
- resolved "https://registry.yarnpkg.com/@invisionag/prettier-config-ivx/-/prettier-config-ivx-2.1.1.tgz#8899ec027cfde823ff497f2d67326bc14fbdf4bd"
8
- integrity sha512-vujtO8XVSwoWU6k1lRZzvOeMCNvX2injJsEfG5rAfQGBjcN0J4a0zwXYBb6GI4gNZsYj6C6iD1Nk2HsMXKFByg==
9
-
10
- "@prettier/plugin-ruby@^1.5.2":
11
- version "1.5.3"
12
- resolved "https://registry.yarnpkg.com/@prettier/plugin-ruby/-/plugin-ruby-1.5.3.tgz#04c1058fce59651c38c02ace39faa7a0daa4264b"
13
- integrity sha512-YXXf0PGsUOMk40UlfnjDeF3NuRmKE4ddN4L2drFaskhqcB/P9jNGabz5FsGJdJt+ibPxfRsqx05gfxGLZo1wHg==
14
- dependencies:
15
- prettier ">=1.10"
16
-
17
- prettier@>=1.10, prettier@^2.2.0:
18
- version "2.2.1"
19
- resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
20
- integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==