railway-ipc 2.2.0 → 4.0.1

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: ade6188ad53835d058964953332583db7cf146f2b02b58c4a25cddb42e7f89cf
4
- data.tar.gz: 4672c6b08a293c1988f42c944063cc9ba67d6656531b8c3a3fd9b00ab908219e
3
+ metadata.gz: 8fa6ce7e4b6f8667fa4939cc334dd66845c39e5f357bd4887eb030b7f4edcf3a
4
+ data.tar.gz: ea8f959e0cf43ed38b71276baac73c4c4e417ee8d4323d47605390a95d573c53
5
5
  SHA512:
6
- metadata.gz: a76acd180339079bc95bd042e6a6c2512edd05d485ca2763e8211278f85adf0677156f1e8d305ce5cc4186c9304ed85e7c8343b0a32b79560225e1261e7801a9
7
- data.tar.gz: ceab6497161f8c640cbb2da715b9380729b1e8dbb40843d11fd879a32214e7f39f5aa130d811eeff66c0f0b59eea743e6cb97aa8fb89bcb2fa62aa06ceffb65f
6
+ metadata.gz: e76cd2483ea799dd04f7acc27cd5f7799a6f7bd6d6e9bc6e64c08e0a621f55ad6e9ba37fb53c685fea4dd8847b78bde3b73bb97091d9f3764d15c3ce5237c84b
7
+ data.tar.gz: 62955dfb46eb250156ff8d9c1c9c1570169bc1c45742ca231dbbceadeb091f71439793645620cad63ba5e0c21e393894aa13d96444ac0da4f440c0b7179076a7
data/.gitignore CHANGED
@@ -4,10 +4,14 @@
4
4
  /coverage/
5
5
  /doc/
6
6
  /pkg/
7
+ /rake
7
8
  /spec/reports/
8
9
  /tmp/
9
10
  Gemfile.lock
10
11
 
12
+ # built gems
13
+ railway-ipc-*.gem
14
+
11
15
  # rspec failure tracking
12
16
  .rspec_status
13
17
 
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -10,6 +10,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
  ### Removed
11
11
  ### Fixed
12
12
 
13
+ ## [4.0.1] - 2021-01-12
14
+ ### Fixed
15
+ * 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).
16
+
17
+ ## [4.0.0] - 2021-01-11
18
+ ### Added
19
+ * 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.
20
+
21
+ ### Changed
22
+ * (Breaking Change) Rename `Consumer#work` to `Consumer#work_with_params`. This was necessary so that we can support specifying different message encodings via metadata in the future. If the message encoding cannot be determined from the message metadata fall back to a default decoder (binary protobufs).
23
+
24
+ ### Fixed
25
+ * `./bin/console` script was broken because Pry wasn't a dependency; added Pry as a development dependency only.
26
+
27
+ ## [3.0.0] - 2020-12-07
28
+ ### Changed
29
+ * Consumers _will no longer crash_ when an exception is raised. Instead, consumers will move the message that caused the exception to a single dead-letter exchange called 'ipc:errors'. Railway will configure the dead-letter exchange automatically.
30
+
31
+ ## [2.2.2] - 2020-11-20
32
+ ### Fixed
33
+ * Fixed Publisher class channel leak. Channels were being created on each instantiation of a Publisher instead of being re-used.
34
+
35
+ ## [2.2.1] - 2020-10-20
36
+ ### Added
37
+ * Logging to indicate when options passed via `listen_to`/`from_queue` are overriding the defaults.
38
+
13
39
  ## [2.2.0] - 2020-10-20
14
40
  ### Added
15
41
  * The ability to configure workers to handle different workloads via `rake railway_ipc::consumers:spawn`
@@ -74,7 +100,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
74
100
  ### Added
75
101
  - 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)
76
102
 
77
- [Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v2.0.3...HEAD
103
+ [Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.1...HEAD
104
+ [4.0.1]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.0...v4.0.1
105
+ [4.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v3.0.0...v4.0.0
106
+ [3.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.2...v3.0.0
107
+ [2.2.2]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.1...v2.2.2
108
+ [2.2.1]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.0...v2.2.1
109
+ [2.2.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.1.0...v2.2.0
110
+ [2.1.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.0.3...v2.1.0
78
111
  [2.0.3]: https://github.com/learn-co/railway_ipc_gem/compare/v2.0.2...v2.0.3
79
112
  [2.0.2]: https://github.com/learn-co/railway_ipc_gem/compare/v2.0.1...v2.0.2
80
113
  [2.0.1]: https://github.com/learn-co/railway_ipc_gem/compare/v2.0.0...v2.0.1
data/README.md CHANGED
@@ -52,6 +52,26 @@ Then, run your consumers
52
52
  bundle exec rake railway_ipc:consumers:start CONSUMERS=YourConsumer,YourOtherConsumer
53
53
  ```
54
54
 
55
+ You may also configure your consumers more granularly using:
56
+
57
+ `./config/sneaker_worker_groups.yml`
58
+ ```yaml
59
+ HighPriority:
60
+ classes: YourConsumer,YourOtherConsumer
61
+ workers: 5
62
+ LowPriority:
63
+ classes: YourThirdConsumer
64
+ workers: 1
65
+ ```
66
+
67
+ ```bash
68
+ bundle exec rake railway_ipc:consumers:spawn
69
+ ```
70
+
71
+ By default, `spawn` will map to `./config/sneaker_worker_groups.yml` but you can override it by using `WORKER_GROUP_CONFIG`.
72
+
73
+ See the [Sneaker Documentation](https://github.com/jondot/sneakers/wiki/Handling-different-workloads) for more information.
74
+
55
75
  # Request/Response
56
76
 
57
77
  Define your server, client and responder. Docs coming soon.
@@ -5,7 +5,7 @@ require 'sneakers'
5
5
  require 'sneakers/spawner'
6
6
  require 'bunny'
7
7
  require 'active_record'
8
- require 'railway_ipc/version'
8
+ require 'singleton'
9
9
  require 'railway_ipc/logger'
10
10
  require 'railway_ipc/unhandled_message_error'
11
11
  require 'railway_ipc/response'
@@ -14,7 +14,9 @@ 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_decoders'
17
18
  require 'railway_ipc/incoming_message'
19
+ require 'railway_ipc/connection_manager'
18
20
  require 'railway_ipc/publisher'
19
21
  require 'railway_ipc/responder'
20
22
  require 'railway_ipc/rpc/rpc'
@@ -34,12 +36,12 @@ module RailwayIpc
34
36
  Sneakers::Spawner.spawn
35
37
  end
36
38
 
37
- def self.configure(log_device=STDOUT, level=::Logger::INFO, log_formatter=nil)
39
+ def self.configure(log_device=$stdout, level=::Logger::INFO, log_formatter=nil)
38
40
  @logger = RailwayIpc::Logger.new(log_device, level, log_formatter)
39
41
  end
40
42
 
41
43
  def self.logger
42
- @logger || RailwayIpc::Logger.new(STDOUT)
44
+ @logger || RailwayIpc::Logger.new($stdout)
43
45
  end
44
46
 
45
47
  def self.bunny_connection
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module RailwayIpc
6
+ # RabbitMQ connection manager. Ensures there is a single RabbitMQ
7
+ # connection and channel per thread, which prevents channel leaks.
8
+ #
9
+ class ConnectionManager
10
+ include Singleton
11
+
12
+ def initialize
13
+ establish_connection
14
+ end
15
+
16
+ def establish_connection
17
+ @connection = Bunny.new(
18
+ host: settings[:host],
19
+ user: settings[:user],
20
+ pass: settings[:pass],
21
+ port: settings[:port],
22
+ vhost: settings[:vhost] || '/',
23
+ logger: RailwayIpc.logger
24
+ )
25
+ @connection.start
26
+ @channel = @connection.create_channel
27
+
28
+ @connection
29
+ end
30
+
31
+ def channel
32
+ return @channel if connected?
33
+
34
+ establish_connection
35
+ @channel
36
+ end
37
+
38
+ def connected?
39
+ @connection&.connected? && @channel&.open?
40
+ end
41
+
42
+ private
43
+
44
+ def amqp_url
45
+ @amqp_url ||= ENV.fetch('RAILWAY_RABBITMQ_CONNECTION_URL', 'amqp://guest:guest@localhost:5672')
46
+ end
47
+
48
+ def settings
49
+ @settings ||= AMQ::Settings.parse_amqp_url(amqp_url)
50
+ end
51
+ end
52
+ end
@@ -5,6 +5,8 @@ module RailwayIpc
5
5
  include Sneakers::Worker
6
6
 
7
7
  def self.inherited(base)
8
+ super
9
+
8
10
  base.instance_eval do
9
11
  def handlers
10
12
  @handlers ||= RailwayIpc::HandlerStore.new
@@ -12,14 +14,27 @@ module RailwayIpc
12
14
  end
13
15
  end
14
16
 
17
+ # rubocop:disable Metrics/MethodLength
15
18
  def self.listen_to(queue:, exchange:, options: {})
19
+ unless options.empty?
20
+ RailwayIpc.logger.info(
21
+ "Overriding configuration for #{queue} with new options",
22
+ feature: 'railway_ipc_consumer',
23
+ options: options
24
+ )
25
+ end
26
+
16
27
  from_queue queue, {
17
28
  exchange: exchange,
18
29
  durable: true,
19
30
  exchange_type: :fanout,
31
+ arguments: {
32
+ 'x-dead-letter-exchange' => 'ipc:errors'
33
+ },
20
34
  connection: RailwayIpc.bunny_connection
21
35
  }.merge(options)
22
36
  end
37
+ # rubocop:enable Metrics/MethodLength
23
38
 
24
39
  def self.handle(message_type, with:)
25
40
  handlers.register(message: message_type, handler: with)
@@ -41,8 +56,14 @@ module RailwayIpc
41
56
  queue.opts[:exchange]
42
57
  end
43
58
 
44
- def work(payload)
45
- message = RailwayIpc::IncomingMessage.new(payload)
59
+ # REFACTOR: Long term we should think about not leaking Sneakers
60
+ # methods as part of Railway's public API since clients can (and do)
61
+ # override them. -BN
62
+ def work_with_params(payload, _delivery_info, metadata)
63
+ headers = metadata.headers || {}
64
+ message_format = headers.fetch('message_format', 'protobuf_binary')
65
+
66
+ message = RailwayIpc::IncomingMessage.new(payload, message_format: message_format)
46
67
  RailwayIpc::ProcessIncomingMessage.call(self, message)
47
68
  ack!
48
69
  rescue StandardError => e
@@ -54,7 +75,7 @@ module RailwayIpc
54
75
  error: e.class,
55
76
  payload: payload
56
77
  )
57
- raise e
78
+ reject!
58
79
  end
59
80
 
60
81
  def get_handler(type)
@@ -2,9 +2,10 @@
2
2
 
3
3
  module RailwayIpc
4
4
  class IncomingMessage
5
- attr_reader :type, :payload, :parsed_payload, :errors
5
+ attr_reader :type, :message_format, :payload, :parsed_payload, :errors
6
6
 
7
- def initialize(payload)
7
+ def initialize(payload, message_format: nil)
8
+ @message_format = message_format
8
9
  @parsed_payload = JSON.parse(payload)
9
10
  @type = parsed_payload['type']
10
11
  @payload = payload
@@ -32,20 +33,23 @@ module RailwayIpc
32
33
  end
33
34
 
34
35
  def decoded
35
- @decoded ||=
36
- begin
37
- protobuf_msg = Base64.decode64(parsed_payload['encoded_message'])
38
- decoder = Kernel.const_get(type)
39
- decoder.decode(protobuf_msg)
40
- rescue Google::Protobuf::ParseError => e
41
- raise RailwayIpc::IncomingMessage::ParserError.new(e)
42
- rescue NameError
43
- RailwayIpc::Messages::Unknown.decode(protobuf_msg)
44
- end
36
+ @decoded ||= \
37
+ get_decoder(message_format).call(type, parsed_payload['encoded_message'])
45
38
  end
46
39
 
47
40
  def stringify_errors
48
41
  errors.values.join(', ')
49
42
  end
43
+
44
+ private
45
+
46
+ DEFAULT_DECODER = RailwayIpc::MessageDecoders::ProtobufBinaryDecoder
47
+
48
+ def get_decoder(name)
49
+ {
50
+ 'protobuf_binary' => RailwayIpc::MessageDecoders::ProtobufBinaryDecoder,
51
+ 'protobuf_json' => RailwayIpc::MessageDecoders::ProtobufJsonDecoder
52
+ }.fetch(name, DEFAULT_DECODER)
53
+ end
50
54
  end
51
55
  end
@@ -21,7 +21,7 @@ module RailwayIpc
21
21
  # logger = RailwayIpc::Logger.new(STDOUT, Logger::INFO, OjFormatter)
22
22
  #
23
23
  class Logger
24
- def initialize(device=STDOUT, level=::Logger::INFO, formatter=nil)
24
+ def initialize(device=$stdout, level=::Logger::INFO, formatter=nil)
25
25
  @logger = ::Logger.new(device)
26
26
  @logger.level = level
27
27
  @logger.formatter = formatter if formatter
@@ -32,7 +32,7 @@ module RailwayIpc
32
32
  data.merge!(feature: 'railway_ipc') unless data.key?(:feature)
33
33
  return logger.send(level, data.merge(message: message)) unless block
34
34
 
35
- data = message.merge(data) if message&.is_a?(Hash)
35
+ data = message.merge(data) if message.is_a?(Hash)
36
36
  data.merge!(message: block.call)
37
37
 
38
38
  # This is for compatability w/ Ruby's Logger. Ruby's Logger class
@@ -41,7 +41,7 @@ module RailwayIpc
41
41
  # is assumed to be the `progname`.
42
42
  #
43
43
  # https://github.com/ruby/logger/blob/master/lib/logger.rb#L471
44
- data.merge!(progname: message) if message&.is_a?(String)
44
+ data.merge!(progname: message) if message.is_a?(String)
45
45
  logger.send(level, data)
46
46
  end
47
47
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailwayIpc
4
+ module MessageDecoders
5
+ ProtobufBinaryDecoder = lambda do |type, encoded_message|
6
+ protobuf_msg = Base64.decode64(encoded_message)
7
+ protobuf_klass = Kernel.const_get(type)
8
+ protobuf_klass.decode(protobuf_msg)
9
+ rescue Google::Protobuf::ParseError => e
10
+ raise RailwayIpc::IncomingMessage::ParserError.new(e)
11
+ rescue NameError
12
+ RailwayIpc::Messages::Unknown.decode(protobuf_msg)
13
+ end
14
+
15
+ ProtobufJsonDecoder = lambda do |type, message_hash|
16
+ protobuf_klass = Kernel.const_get(type)
17
+ protobuf_klass.new(message_hash)
18
+ rescue ArgumentError => e
19
+ raise RailwayIpc::IncomingMessage::ParserError.new(e)
20
+ rescue NameError
21
+ # NOTE: I didn't realize this until I made this ProtobufJsonDecoder, but
22
+ # the ProtobufBinaryDecoder will ignore any unknown keys -- which is
23
+ # probably not what we want. I'm coding this the same way as the binary
24
+ # protobuf version for consistency, but we should re-think how we want to
25
+ # handle this situation. -BN
26
+ RailwayIpc::Messages::Unknown.new(
27
+ user_uuid: message_hash.fetch(:user_uuid, ''),
28
+ correlation_id: message_hash.fetch(:correlation_id, ''),
29
+ uuid: message_hash.fetch(:uuid, ''),
30
+ context: message_hash.fetch(:context, {})
31
+ )
32
+ end
33
+ end
34
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'singleton'
4
-
5
3
  module RailwayIpc
6
4
  class SingletonPublisher < Sneakers::Publisher
7
5
  include ::Singleton
@@ -53,19 +51,12 @@ module RailwayIpc
53
51
  end
54
52
 
55
53
  module RailwayIpc
56
- class Publisher < Sneakers::Publisher
54
+ class Publisher
57
55
  attr_reader :exchange_name, :message_store
58
56
 
59
57
  def initialize(opts={})
60
58
  @exchange_name = opts.fetch(:exchange_name)
61
59
  @message_store = opts.fetch(:message_store, RailwayIpc::PublishedMessage)
62
- connection = opts.fetch(:connection, nil)
63
- options = {
64
- exchange: exchange_name,
65
- connection: connection,
66
- exchange_type: :fanout
67
- }.compact
68
- super(options)
69
60
  end
70
61
 
71
62
  # rubocop:disable Metrics/AbcSize
@@ -75,7 +66,7 @@ module RailwayIpc
75
66
  RailwayIpc.logger.info('Publishing message', log_message_options(message))
76
67
 
77
68
  stored_message = message_store.store_message(exchange_name, message)
78
- super(RailwayIpc::Rabbitmq::Payload.encode(message))
69
+ exchange.publish(RailwayIpc::Rabbitmq::Payload.encode(message))
79
70
  rescue RailwayIpc::InvalidProtobuf => e
80
71
  RailwayIpc.logger.error('Invalid protobuf', log_message_options(message))
81
72
  raise e
@@ -88,8 +79,16 @@ module RailwayIpc
88
79
  end
89
80
  # rubocop:enable Metrics/AbcSize
90
81
 
82
+ def exchange
83
+ @exchange ||= channel.exchange(exchange_name, type: :fanout, durable: true, auto_delete: false, arguments: {})
84
+ end
85
+
91
86
  private
92
87
 
88
+ def channel
89
+ RailwayIpc::ConnectionManager.instance.channel
90
+ end
91
+
93
92
  def log_message_options(message)
94
93
  {
95
94
  feature: 'railway_ipc_publisher',
@@ -16,7 +16,7 @@ module RailwayIpc
16
16
  :port,
17
17
  :user
18
18
 
19
- def initialize(amqp_url: ENV['RAILWAY_RABBITMQ_CONNECTION_URL'], exchange_name:, queue_name: '', options: {})
19
+ def initialize(exchange_name:, amqp_url: ENV['RAILWAY_RABBITMQ_CONNECTION_URL'], queue_name: '', options: {})
20
20
  @queue_name = queue_name
21
21
  @exchange_name = exchange_name
22
22
  settings = AMQ::Settings.parse_amqp_url(amqp_url)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailwayIpc
4
- VERSION = '2.2.0'
4
+ VERSION = '4.0.1'
5
5
  end
@@ -48,6 +48,7 @@ Gem::Specification.new do |spec|
48
48
  spec.add_development_dependency 'database_cleaner', '~> 1.7'
49
49
  spec.add_development_dependency 'listen', '~> 3.0.5'
50
50
  spec.add_development_dependency 'pg', '~> 0.18'
51
+ spec.add_development_dependency 'pry', '~> 0.13'
51
52
  spec.add_development_dependency 'rails', '~> 5.0.7'
52
53
  spec.add_development_dependency 'rspec-rails'
53
54
  spec.add_development_dependency 'shoulda-matchers', '~> 4.2'
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: 2.2.0
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-20 00:00:00.000000000 Z
11
+ date: 2021-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0.18'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.13'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.13'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: rails
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -213,6 +227,7 @@ extensions: []
213
227
  extra_rdoc_files: []
214
228
  files:
215
229
  - ".gitignore"
230
+ - ".ruby-version"
216
231
  - CHANGELOG.md
217
232
  - CODE_OF_CONDUCT.md
218
233
  - Gemfile
@@ -225,6 +240,7 @@ files:
225
240
  - bin/setup
226
241
  - lib/railway_ipc.rb
227
242
  - lib/railway_ipc/Rakefile
243
+ - lib/railway_ipc/connection_manager.rb
228
244
  - lib/railway_ipc/consumer/consumer.rb
229
245
  - lib/railway_ipc/consumer/process_incoming_message.rb
230
246
  - lib/railway_ipc/errors.rb
@@ -232,6 +248,7 @@ files:
232
248
  - lib/railway_ipc/handler_store.rb
233
249
  - lib/railway_ipc/incoming_message.rb
234
250
  - lib/railway_ipc/logger.rb
251
+ - lib/railway_ipc/message_decoders.rb
235
252
  - lib/railway_ipc/models/consumed_message.rb
236
253
  - lib/railway_ipc/models/published_message.rb
237
254
  - lib/railway_ipc/publisher.rb