railway-ipc 4.0.0 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +21 -3
- data/lib/railway_ipc/consumer/consumer.rb +2 -2
- data/lib/railway_ipc/incoming_message.rb +2 -2
- data/lib/railway_ipc/message_encoders.rb +23 -0
- data/lib/railway_ipc/models/consumed_message.rb +10 -0
- data/lib/railway_ipc/models/published_message.rb +17 -8
- data/lib/railway_ipc/outgoing_message.rb +38 -0
- data/lib/railway_ipc/publisher.rb +5 -56
- data/lib/railway_ipc/version.rb +1 -1
- data/lib/railway_ipc.rb +2 -0
- data/priv/migrations/add_railway_ipc_consumed_messages.rb +1 -1
- data/priv/migrations/add_railway_ipc_published_messages.rb +1 -1
- data/railway_ipc.gemspec +2 -2
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc7c266e2cb4bb91515fb490ec2986262f6b2031c6ddf930aa4b9a4951c73f18
|
4
|
+
data.tar.gz: '079506cb2cf5f62702490733b0759501a48aff202dc2d77616540e9b7d585734'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d546243d3989cc7545fd1afe5c54973139df71cd32bbf6cbeb2c052ef657603154e7ec0f6f690c494e87cc808d94f07adce0a44f6a0f1fd62a862de912184c5b
|
7
|
+
data.tar.gz: 9813c0f55a7075a9e23ba9abb1dbcfe50990a868f6a0ccd2f08424cc58cb2491761008ef7e689dc7cdd617194223bcfbf4e51a201ca7c934ca932d3c092e4e38
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.2
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
5
|
|
7
6
|
## [Unreleased]
|
8
7
|
### Added
|
@@ -10,6 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
10
9
|
### Removed
|
11
10
|
### Fixed
|
12
11
|
|
12
|
+
## [5.0.0] - 2021-02-17
|
13
|
+
### Added
|
14
|
+
* Message encoders. Messages can now be encoded using either binary protobufs (the default) or JSON protobufs.
|
15
|
+
* `RailwayIpc::OutgoingMessage` abstraction that encapsulates everything about a message to be published.
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
* `Publisher#publish` now takes an optional `format` parameter that specifies how the message should be encoded. It is added to the message header when the message is published. The default format is `binary_protobuf`.
|
19
|
+
* (Breaking change) `Publisher#publish` now returns an `OutgoingMessage` instead of a `Bunny::Exchange`.
|
20
|
+
* Refactor `PublishedMessage#store_message` to take an `OutgoingMessage`.
|
21
|
+
|
22
|
+
### Removed
|
23
|
+
* (Breaking change) Remove deprecated `SingletonPublisher`
|
24
|
+
|
25
|
+
## [4.0.1] - 2021-01-12
|
26
|
+
### Fixed
|
27
|
+
* Fixed `undefined method fetch for Bunny::MessageProperties` error. `Bunny::MessageProperties` isn't really a Hash, it wraps one (and doesn't provide a `#fetch` method).
|
28
|
+
|
13
29
|
## [4.0.0] - 2021-01-11
|
14
30
|
### Added
|
15
31
|
* JSON decoder for consumers that can handle JSON encoded Protobufs. Note that the publishers do not (yet) have the option of encoding the messages as JSON.
|
@@ -96,7 +112,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
96
112
|
### Added
|
97
113
|
- Correlation ID and message UUID are auto generated for messages for IDs are not passed in [#23](https://github.com/learn-co/railway_ipc_gem/pull/23)
|
98
114
|
|
99
|
-
[Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/
|
115
|
+
[Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v5.0.0...HEAD
|
116
|
+
[5.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.1...v5.0.0
|
117
|
+
[4.0.1]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.0...v4.0.1
|
100
118
|
[4.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v3.0.0...v4.0.0
|
101
119
|
[3.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.2...v3.0.0
|
102
120
|
[2.2.2]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.1...v2.2.2
|
@@ -60,8 +60,8 @@ module RailwayIpc
|
|
60
60
|
# methods as part of Railway's public API since clients can (and do)
|
61
61
|
# override them. -BN
|
62
62
|
def work_with_params(payload, _delivery_info, metadata)
|
63
|
-
|
64
|
-
|
63
|
+
headers = metadata.headers || {}
|
64
|
+
message_format = headers.fetch('message_format', 'binary_protobuf')
|
65
65
|
|
66
66
|
message = RailwayIpc::IncomingMessage.new(payload, message_format: message_format)
|
67
67
|
RailwayIpc::ProcessIncomingMessage.call(self, message)
|
@@ -47,8 +47,8 @@ module RailwayIpc
|
|
47
47
|
|
48
48
|
def get_decoder(name)
|
49
49
|
{
|
50
|
-
'
|
51
|
-
'
|
50
|
+
'binary_protobuf' => RailwayIpc::MessageDecoders::ProtobufBinaryDecoder,
|
51
|
+
'json_protobuf' => RailwayIpc::MessageDecoders::ProtobufJsonDecoder
|
52
52
|
}.fetch(name, DEFAULT_DECODER)
|
53
53
|
end
|
54
54
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailwayIpc
|
4
|
+
module MessageEncoders
|
5
|
+
ProtobufBinaryEncoder = lambda do |message|
|
6
|
+
{
|
7
|
+
type: message.type,
|
8
|
+
encoded_message: Base64.encode64(message.proto.class.encode(message.proto))
|
9
|
+
}.to_json
|
10
|
+
rescue NoMethodError
|
11
|
+
raise RailwayIpc::InvalidProtobuf.new("Message #{message} is not a valid protobuf")
|
12
|
+
end
|
13
|
+
|
14
|
+
ProtobufJsonEncoder = lambda do |message|
|
15
|
+
{
|
16
|
+
type: message.type,
|
17
|
+
encoded_message: message.proto.to_h
|
18
|
+
}.to_json
|
19
|
+
rescue NoMethodError
|
20
|
+
raise RailwayIpc::InvalidProtobuf.new("Message #{message} is not a valid protobuf")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -54,8 +54,18 @@ module RailwayIpc
|
|
54
54
|
|
55
55
|
private
|
56
56
|
|
57
|
+
# rails <= 5.1 uses this method to know the name of the created_at/updated_at fields
|
57
58
|
def timestamp_attributes_for_create
|
58
59
|
super << :inserted_at
|
59
60
|
end
|
61
|
+
|
62
|
+
# rails >= 6.0 moved this to the class level and uses strings instead of symbols
|
63
|
+
class << self
|
64
|
+
private
|
65
|
+
|
66
|
+
def timestamp_attributes_for_create
|
67
|
+
super << 'inserted_at'
|
68
|
+
end
|
69
|
+
end
|
60
70
|
end
|
61
71
|
end
|
@@ -7,23 +7,32 @@ module RailwayIpc
|
|
7
7
|
|
8
8
|
validates :uuid, :status, presence: true
|
9
9
|
|
10
|
-
def self.store_message(
|
11
|
-
encoded_message = RailwayIpc::Rabbitmq::Payload.encode(message)
|
10
|
+
def self.store_message(outgoing_message)
|
12
11
|
create!(
|
13
|
-
uuid:
|
14
|
-
message_type:
|
15
|
-
user_uuid:
|
16
|
-
correlation_id:
|
17
|
-
encoded_message:
|
12
|
+
uuid: outgoing_message.uuid,
|
13
|
+
message_type: outgoing_message.type,
|
14
|
+
user_uuid: outgoing_message.user_uuid,
|
15
|
+
correlation_id: outgoing_message.correlation_id,
|
16
|
+
encoded_message: outgoing_message.encoded,
|
18
17
|
status: 'sent',
|
19
|
-
exchange:
|
18
|
+
exchange: outgoing_message.exchange
|
20
19
|
)
|
21
20
|
end
|
22
21
|
|
23
22
|
private
|
24
23
|
|
24
|
+
# rails <= 5.1 uses this method to know the name of the created_at/updated_at fields
|
25
25
|
def timestamp_attributes_for_create
|
26
26
|
super << :inserted_at
|
27
27
|
end
|
28
|
+
|
29
|
+
# rails >= 6.0 moved this to the class level and uses strings instead of symbols
|
30
|
+
class << self
|
31
|
+
private
|
32
|
+
|
33
|
+
def timestamp_attributes_for_create
|
34
|
+
super << 'inserted_at'
|
35
|
+
end
|
36
|
+
end
|
28
37
|
end
|
29
38
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailwayIpc
|
4
|
+
class OutgoingMessage
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
attr_reader :proto, :exchange, :format
|
8
|
+
|
9
|
+
def_delegators :@proto, :uuid, :user_uuid, :correlation_id
|
10
|
+
|
11
|
+
def initialize(proto, exchange, format=nil)
|
12
|
+
proto.uuid = SecureRandom.uuid if proto.uuid.blank?
|
13
|
+
proto.correlation_id = SecureRandom.uuid if proto.correlation_id.blank?
|
14
|
+
@proto = proto
|
15
|
+
@exchange = exchange
|
16
|
+
@format = format
|
17
|
+
end
|
18
|
+
|
19
|
+
def type
|
20
|
+
proto.class.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def encoded
|
24
|
+
@encoded ||= encoder.call(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
DEFAULT_ENCODER = RailwayIpc::MessageEncoders::ProtobufBinaryEncoder
|
30
|
+
|
31
|
+
def encoder
|
32
|
+
{
|
33
|
+
'binary_protobuf' => RailwayIpc::MessageEncoders::ProtobufBinaryEncoder,
|
34
|
+
'json_protobuf' => RailwayIpc::MessageEncoders::ProtobufJsonEncoder
|
35
|
+
}.fetch(format, DEFAULT_ENCODER)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -1,55 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module RailwayIpc
|
4
|
-
class SingletonPublisher < Sneakers::Publisher
|
5
|
-
include ::Singleton
|
6
|
-
|
7
|
-
def self.exchange(exchange)
|
8
|
-
@exchange_name = exchange
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.exchange_name
|
12
|
-
raise 'Subclass must set the exchange' unless @exchange_name
|
13
|
-
|
14
|
-
@exchange_name
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize
|
18
|
-
super(exchange: self.class.exchange_name, exchange_type: :fanout)
|
19
|
-
end
|
20
|
-
|
21
|
-
def publish(message, published_message_store=RailwayIpc::PublishedMessage)
|
22
|
-
RailwayIpc.logger.warn('DEPRECATED: Use new PublisherInstance class', log_message_options)
|
23
|
-
ensure_message_uuid(message)
|
24
|
-
ensure_correlation_id(message)
|
25
|
-
RailwayIpc.logger.info('Publishing message', log_message_options(message))
|
26
|
-
result = super(RailwayIpc::Rabbitmq::Payload.encode(message))
|
27
|
-
published_message_store.store_message(self.class.exchange_name, message)
|
28
|
-
result
|
29
|
-
rescue RailwayIpc::InvalidProtobuf => e
|
30
|
-
RailwayIpc.logger.error('Invalid protobuf', log_message_options(message))
|
31
|
-
raise e
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def ensure_message_uuid(message)
|
37
|
-
message.uuid = SecureRandom.uuid if message.uuid.blank?
|
38
|
-
message
|
39
|
-
end
|
40
|
-
|
41
|
-
def ensure_correlation_id(message)
|
42
|
-
message.correlation_id = SecureRandom.uuid if message.correlation_id.blank?
|
43
|
-
message
|
44
|
-
end
|
45
|
-
|
46
|
-
def log_message_options(message=nil)
|
47
|
-
options = { feature: 'railway_ipc_publisher', exchange: self.class.exchange_name }
|
48
|
-
message.nil? ? options : options.merge(protobuf: { type: message.class, data: message })
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
3
|
module RailwayIpc
|
54
4
|
class Publisher
|
55
5
|
attr_reader :exchange_name, :message_store
|
@@ -60,13 +10,12 @@ module RailwayIpc
|
|
60
10
|
end
|
61
11
|
|
62
12
|
# rubocop:disable Metrics/AbcSize
|
63
|
-
def publish(message)
|
64
|
-
|
65
|
-
|
13
|
+
def publish(message, format='binary_protobuf')
|
14
|
+
outgoing_message = OutgoingMessage.new(message, exchange_name, format)
|
15
|
+
stored_message = message_store.store_message(outgoing_message)
|
66
16
|
RailwayIpc.logger.info('Publishing message', log_message_options(message))
|
67
|
-
|
68
|
-
|
69
|
-
exchange.publish(RailwayIpc::Rabbitmq::Payload.encode(message))
|
17
|
+
exchange.publish(outgoing_message.encoded, headers: { message_format: format })
|
18
|
+
outgoing_message
|
70
19
|
rescue RailwayIpc::InvalidProtobuf => e
|
71
20
|
RailwayIpc.logger.error('Invalid protobuf', log_message_options(message))
|
72
21
|
raise e
|
data/lib/railway_ipc/version.rb
CHANGED
data/lib/railway_ipc.rb
CHANGED
@@ -14,8 +14,10 @@ require 'railway_ipc/unknown_message.pb'
|
|
14
14
|
require 'railway_ipc/rabbitmq/adapter'
|
15
15
|
require 'railway_ipc/handler'
|
16
16
|
require 'railway_ipc/handler_store'
|
17
|
+
require 'railway_ipc/message_encoders'
|
17
18
|
require 'railway_ipc/message_decoders'
|
18
19
|
require 'railway_ipc/incoming_message'
|
20
|
+
require 'railway_ipc/outgoing_message'
|
19
21
|
require 'railway_ipc/connection_manager'
|
20
22
|
require 'railway_ipc/publisher'
|
21
23
|
require 'railway_ipc/responder'
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class AddRailwayIpcPublishedMessages < ActiveRecord::Migration
|
3
|
+
class AddRailwayIpcPublishedMessages < ActiveRecord::Migration[4.2]
|
4
4
|
def change
|
5
5
|
create_table :railway_ipc_published_messages, id: false do |t|
|
6
6
|
t.uuid :uuid, null: false
|
data/railway_ipc.gemspec
CHANGED
@@ -47,9 +47,9 @@ Gem::Specification.new do |spec|
|
|
47
47
|
# Setup for testing Rails type code within mock Rails app
|
48
48
|
spec.add_development_dependency 'database_cleaner', '~> 1.7'
|
49
49
|
spec.add_development_dependency 'listen', '~> 3.0.5'
|
50
|
-
spec.add_development_dependency 'pg', '~>
|
50
|
+
spec.add_development_dependency 'pg', '~> 1.1'
|
51
51
|
spec.add_development_dependency 'pry', '~> 0.13'
|
52
|
-
spec.add_development_dependency 'rails', '~>
|
52
|
+
spec.add_development_dependency 'rails', '~> 6.0'
|
53
53
|
spec.add_development_dependency 'rspec-rails'
|
54
54
|
spec.add_development_dependency 'shoulda-matchers', '~> 4.2'
|
55
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: railway-ipc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ''
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -156,14 +156,14 @@ dependencies:
|
|
156
156
|
requirements:
|
157
157
|
- - "~>"
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version: '
|
159
|
+
version: '1.1'
|
160
160
|
type: :development
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
|
-
version: '
|
166
|
+
version: '1.1'
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: pry
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -184,14 +184,14 @@ dependencies:
|
|
184
184
|
requirements:
|
185
185
|
- - "~>"
|
186
186
|
- !ruby/object:Gem::Version
|
187
|
-
version:
|
187
|
+
version: '6.0'
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - "~>"
|
193
193
|
- !ruby/object:Gem::Version
|
194
|
-
version:
|
194
|
+
version: '6.0'
|
195
195
|
- !ruby/object:Gem::Dependency
|
196
196
|
name: rspec-rails
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -249,8 +249,10 @@ files:
|
|
249
249
|
- lib/railway_ipc/incoming_message.rb
|
250
250
|
- lib/railway_ipc/logger.rb
|
251
251
|
- lib/railway_ipc/message_decoders.rb
|
252
|
+
- lib/railway_ipc/message_encoders.rb
|
252
253
|
- lib/railway_ipc/models/consumed_message.rb
|
253
254
|
- lib/railway_ipc/models/published_message.rb
|
255
|
+
- lib/railway_ipc/outgoing_message.rb
|
254
256
|
- lib/railway_ipc/publisher.rb
|
255
257
|
- lib/railway_ipc/rabbitmq/adapter.rb
|
256
258
|
- lib/railway_ipc/rabbitmq/payload.rb
|
@@ -295,7 +297,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
295
297
|
- !ruby/object:Gem::Version
|
296
298
|
version: '0'
|
297
299
|
requirements: []
|
298
|
-
rubygems_version: 3.
|
300
|
+
rubygems_version: 3.1.4
|
299
301
|
signing_key:
|
300
302
|
specification_version: 4
|
301
303
|
summary: IPC components for Rails
|