sbmt-kafka_consumer 2.3.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +33 -0
- data/lib/sbmt/kafka_consumer/base_consumer.rb +22 -2
- data/lib/sbmt/kafka_consumer/inbox_consumer.rb +2 -1
- data/lib/sbmt/kafka_consumer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3aaf5a3d0cab0fe477ab5af189b754b642c403036eec6f7329818645a1dff362
|
4
|
+
data.tar.gz: f198d12579b3437f903fff1c7be33246c5121b1b37bb735e861c7c0f9cbb0314
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 468be419229e1096fbae7a0892607c33c8913f686478bca5d8ed8816ac83c8932be7683eedbfbc8eeb55225578dd74707c7bc256947691e65fa19d7142fdb095
|
7
|
+
data.tar.gz: 105b7fdd0b37927b92e81819035442705e9a636828469f47b61ce5bb67850db2f1c921e5d45b9bcadedb89eaceb6e929a3fde903672517a9aae5810e68ef0627
|
data/CHANGELOG.md
CHANGED
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|
13
13
|
|
14
14
|
### Fixed
|
15
15
|
|
16
|
+
## [2.4.0] - 2024-06-06
|
17
|
+
|
18
|
+
### Added
|
19
|
+
|
20
|
+
- Added option `midlewares` to add middleware before message processing
|
21
|
+
|
16
22
|
## [2.3.1] - 2024-06-05
|
17
23
|
|
18
24
|
### Fixed
|
data/README.md
CHANGED
@@ -183,12 +183,45 @@ consumer_groups:
|
|
183
183
|
#### `consumer.init_attrs` options for `BaseConsumer`
|
184
184
|
|
185
185
|
- `skip_on_error` - optional, default false, omit consumer errors in message processing and commit the offset to Kafka
|
186
|
+
- `middlewares` - optional, default [], type String, add middleware before message processing
|
187
|
+
|
188
|
+
```yaml
|
189
|
+
init_attrs:
|
190
|
+
middlewares: ['SomeMiddleware']
|
191
|
+
```
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
class SomeMiddleware
|
195
|
+
def call(message)
|
196
|
+
yield if message.payload.id.to_i % 2 == 0
|
197
|
+
end
|
198
|
+
end
|
199
|
+
```
|
200
|
+
__CAUTION__:
|
201
|
+
- ⚠️ `yield` is mandatory for all middleware, as it returns control to the `process_message` method.
|
186
202
|
|
187
203
|
#### `consumer.init_attrs` options for `InboxConsumer`
|
188
204
|
|
189
205
|
- `inbox_item` - required, name of the inbox item class
|
190
206
|
- `event_name` - optional, default nil, used when the inbox item keep several event types
|
191
207
|
- `skip_on_error` - optional, default false, omit consumer errors in message processing and commit the offset to Kafka
|
208
|
+
- `middlewares` - optional, default [], type String, add middleware before message processing
|
209
|
+
|
210
|
+
```yaml
|
211
|
+
init_attrs:
|
212
|
+
middlewares: ['SomeMiddleware']
|
213
|
+
```
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
class SomeMiddleware
|
217
|
+
def call(message)
|
218
|
+
yield if message.payload.id.to_i % 2 == 0
|
219
|
+
end
|
220
|
+
end
|
221
|
+
```
|
222
|
+
__CAUTION__:
|
223
|
+
- ⚠️ `yield` is mandatory for all middleware, as it returns control to the `process_message` method.
|
224
|
+
- ⚠️ Doesn't work with `process_batch`.
|
192
225
|
|
193
226
|
#### `deserializer.init_attrs` options
|
194
227
|
|
@@ -5,9 +5,10 @@ module Sbmt
|
|
5
5
|
class BaseConsumer < Karafka::BaseConsumer
|
6
6
|
attr_reader :trace_id
|
7
7
|
|
8
|
-
def self.consumer_klass(skip_on_error: false)
|
8
|
+
def self.consumer_klass(skip_on_error: false, middlewares: [])
|
9
9
|
Class.new(self) do
|
10
10
|
const_set(:SKIP_ON_ERROR, skip_on_error)
|
11
|
+
const_set(:MIDDLEWARES, middlewares.map(&:constantize))
|
11
12
|
|
12
13
|
def self.name
|
13
14
|
superclass.name
|
@@ -93,7 +94,7 @@ module Sbmt
|
|
93
94
|
# so we trigger it explicitly to catch undeserializable message early
|
94
95
|
message.payload
|
95
96
|
|
96
|
-
process_message(message)
|
97
|
+
call_middlewares(message, middlewares) { process_message(message) }
|
97
98
|
|
98
99
|
mark_as_consumed!(message)
|
99
100
|
end
|
@@ -102,6 +103,10 @@ module Sbmt
|
|
102
103
|
self.class::SKIP_ON_ERROR
|
103
104
|
end
|
104
105
|
|
106
|
+
def middlewares
|
107
|
+
self.class::MIDDLEWARES
|
108
|
+
end
|
109
|
+
|
105
110
|
# can be overridden in consumer to enable message logging
|
106
111
|
def log_payload?
|
107
112
|
false
|
@@ -132,6 +137,21 @@ module Sbmt
|
|
132
137
|
def message_payload(message)
|
133
138
|
message.payload || message.raw_payload
|
134
139
|
end
|
140
|
+
|
141
|
+
def call_middlewares(message, middlewares)
|
142
|
+
return yield if middlewares.empty?
|
143
|
+
|
144
|
+
chain = middlewares.map { |middleware_class| middleware_class.new }
|
145
|
+
|
146
|
+
traverse_chain = proc do
|
147
|
+
if chain.empty?
|
148
|
+
yield
|
149
|
+
else
|
150
|
+
chain.shift.call(message, &traverse_chain)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
traverse_chain.call
|
154
|
+
end
|
135
155
|
end
|
136
156
|
end
|
137
157
|
end
|
@@ -6,11 +6,12 @@ module Sbmt
|
|
6
6
|
IDEMPOTENCY_HEADER_NAME = "Idempotency-Key"
|
7
7
|
DEFAULT_SOURCE = "KAFKA"
|
8
8
|
|
9
|
-
def self.consumer_klass(inbox_item:, event_name: nil, skip_on_error: false, name: nil)
|
9
|
+
def self.consumer_klass(inbox_item:, event_name: nil, skip_on_error: false, name: nil, middlewares: [])
|
10
10
|
Class.new(self) do
|
11
11
|
const_set(:INBOX_ITEM_CLASS_NAME, inbox_item)
|
12
12
|
const_set(:EVENT_NAME, event_name)
|
13
13
|
const_set(:SKIP_ON_ERROR, skip_on_error)
|
14
|
+
const_set(:MIDDLEWARES, middlewares.map(&:constantize))
|
14
15
|
|
15
16
|
def self.name
|
16
17
|
superclass.name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sbmt-kafka_consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sbermarket Ruby-Platform Team
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|