railway-ipc 4.0.1 → 5.0.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
  SHA256:
3
- metadata.gz: 8fa6ce7e4b6f8667fa4939cc334dd66845c39e5f357bd4887eb030b7f4edcf3a
4
- data.tar.gz: ea8f959e0cf43ed38b71276baac73c4c4e417ee8d4323d47605390a95d573c53
3
+ metadata.gz: 5d520b8d56b0ef067e1a02228e1982541de4abce283d83ccdf7d168ee3cbb52f
4
+ data.tar.gz: b4b3eb85928de3ee6dd2a86f3e93e89567b37996af3e2476baa074de3cf5dc47
5
5
  SHA512:
6
- metadata.gz: e76cd2483ea799dd04f7acc27cd5f7799a6f7bd6d6e9bc6e64c08e0a621f55ad6e9ba37fb53c685fea4dd8847b78bde3b73bb97091d9f3764d15c3ce5237c84b
7
- data.tar.gz: 62955dfb46eb250156ff8d9c1c9c1570169bc1c45742ca231dbbceadeb091f71439793645620cad63ba5e0c21e393894aa13d96444ac0da4f440c0b7179076a7
6
+ metadata.gz: 1190af3d7a5e4bb953007b9c2f31668ba0e6048b9958af0927c9be59d01ce4f3f8139c205354e2b931bc08c831c8f9a6e40e94bc40410b9329dc709bc212c5a4
7
+ data.tar.gz: 24f7eba242e5879ec229b80e2d4b24af920bcfa4bfef2a5b0b12b1c8c9e408011b2bd7f0954fcfd27de64d4c7429b185dc9945073a78e1192868ea414a2cfc02
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,19 @@ 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
+
13
25
  ## [4.0.1] - 2021-01-12
14
26
  ### Fixed
15
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).
@@ -100,7 +112,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
100
112
  ### Added
101
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)
102
114
 
103
- [Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.1...HEAD
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
104
117
  [4.0.1]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.0...v4.0.1
105
118
  [4.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v3.0.0...v4.0.0
106
119
  [3.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.2...v3.0.0
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'
@@ -61,7 +61,7 @@ module RailwayIpc
61
61
  # override them. -BN
62
62
  def work_with_params(payload, _delivery_info, metadata)
63
63
  headers = metadata.headers || {}
64
- message_format = headers.fetch('message_format', 'protobuf_binary')
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
- 'protobuf_binary' => RailwayIpc::MessageDecoders::ProtobufBinaryDecoder,
51
- 'protobuf_json' => RailwayIpc::MessageDecoders::ProtobufJsonDecoder
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
@@ -7,16 +7,15 @@ module RailwayIpc
7
7
 
8
8
  validates :uuid, :status, presence: true
9
9
 
10
- def self.store_message(exchange_name, message)
11
- encoded_message = RailwayIpc::Rabbitmq::Payload.encode(message)
10
+ def self.store_message(outgoing_message)
12
11
  create!(
13
- uuid: message.uuid,
14
- message_type: message.class.to_s,
15
- user_uuid: message.user_uuid,
16
- correlation_id: message.correlation_id,
17
- encoded_message: 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: exchange_name
18
+ exchange: outgoing_message.exchange
20
19
  )
21
20
  end
22
21
 
@@ -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
- message.uuid = SecureRandom.uuid if message.uuid.blank?
65
- message.correlation_id = SecureRandom.uuid if message.correlation_id.blank?
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
- stored_message = message_store.store_message(exchange_name, message)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailwayIpc
4
- VERSION = '4.0.1'
4
+ VERSION = '5.0.0'
5
5
  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.0.1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-12 00:00:00.000000000 Z
11
+ date: 2021-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -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