railway-ipc 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a647cd13c25cf49400ae1b9bf26623aced2bd095e7e75b7fc85f82d8e58a8d12
4
+ data.tar.gz: 38816dee53b741fa3bf3b3c53ee3f50463c1d858dddb2c739a4b433b969cc7a8
5
+ SHA512:
6
+ metadata.gz: 4646593fe8a95cebf90b703891fa97f0a608ad122d596607e70c275ea44e4e7d8668fef61852c051268faf418556bbb9bf5771a89ebef82c77d84329fa1c6998
7
+ data.tar.gz: 2d59b7af561eb23b85c1196ca5071a30420f4f4cd0eb3a671d6a5b9a6050fcd30e3cb40cbf62b3d524a02faa7e426d15ec54e4428af68bd024d582095e265600
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sophie.debenedetto@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ railway-ipc (0.1.1)
5
+ bunny (~> 2.2.0)
6
+ sneakers (~> 2.3.5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ amq-protocol (2.3.0)
12
+ bunny (2.2.2)
13
+ amq-protocol (>= 2.0.1)
14
+ byebug (9.1.0)
15
+ coderay (1.1.2)
16
+ diff-lcs (1.3)
17
+ google-protobuf (3.9.2)
18
+ method_source (0.9.2)
19
+ pry (0.12.2)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.9.0)
22
+ pry-byebug (3.4.2)
23
+ byebug (~> 9.0)
24
+ pry (~> 0.10)
25
+ rake (13.0.0)
26
+ rspec (3.8.0)
27
+ rspec-core (~> 3.8.0)
28
+ rspec-expectations (~> 3.8.0)
29
+ rspec-mocks (~> 3.8.0)
30
+ rspec-core (3.8.2)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-expectations (3.8.4)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-mocks (3.8.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-support (3.8.2)
39
+ serverengine (1.5.11)
40
+ sigdump (~> 0.2.2)
41
+ sigdump (0.2.4)
42
+ sneakers (2.3.5)
43
+ bunny (~> 2.2.0)
44
+ serverengine (~> 1.5.11)
45
+ thor
46
+ thread (~> 0.1.7)
47
+ thor (0.20.3)
48
+ thread (0.1.7)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ bundler (= 2.0.1)
55
+ google-protobuf (~> 3.9)
56
+ pry-byebug (= 3.4.2)
57
+ railway-ipc!
58
+ rake (>= 10.0.0)
59
+ rspec (~> 3.0)
60
+
61
+ BUNDLED WITH
62
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 sophiedebenedetto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Railway IPC
2
+
3
+ ## Installation
4
+
5
+ Add this line to your `Gemfile`:
6
+
7
+ ```ruby
8
+ gem 'railway-ipc'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Configure the `RailwayIpc` logger in an initializer:
18
+
19
+ ```ruby
20
+ # config/initializers/railway_ipc.rb
21
+
22
+ RailwayIpc.configure(logger: Rails.logger)
23
+ ```
24
+
25
+ Load the rake tasks in your Rakefile
26
+
27
+ ```ruby
28
+ # Rakefile
29
+ require "railway_ipc"
30
+ ```
31
+
32
+ # Publish/Consume
33
+
34
+ Define your consumer, handler and publisher. See the docs [here](https://docs.learn.co/projects/learn-ipc/railway-ipc-gem/) to learn more.
35
+
36
+ Then, run your consumers
37
+
38
+ ```bash
39
+ bundle exec rake railway_ipc:consumers:start CONSUMERS=YourConsumer,YourOtherConsumer
40
+ ```
41
+
42
+ # Request/Response
43
+
44
+ Define your server, client and responder. Docs coming soon.
45
+
46
+ Then, run your servers:
47
+
48
+ ```bash
49
+ bundle exec rake railway_ipc:servers:start SERVERS=YourConsumer,YourOtherConsumer
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sophiedebenedetto/ipc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+
66
+ ## Code of Conduct
67
+
68
+ Everyone interacting in the RailwayIpc project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sophiedebenedetto/ipc/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "railway_ipc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ require "railway_ipc/version"
2
+ require "sneakers"
3
+ require "bunny"
4
+ require "railway_ipc/version"
5
+ require "railway_ipc/logger"
6
+ require "railway_ipc/unhandled_message_error"
7
+ require "railway_ipc/response"
8
+ require "railway_ipc/rabbitmq/payload"
9
+ require "railway_ipc/null_message"
10
+ require "railway_ipc/concerns/message_handling"
11
+ require "railway_ipc/rabbitmq/connection"
12
+ require "railway_ipc/handler"
13
+ require "railway_ipc/consumer"
14
+ require "railway_ipc/publisher"
15
+ require "railway_ipc/null_handler"
16
+ require "railway_ipc/responder"
17
+ require "railway_ipc/client"
18
+ require "railway_ipc/server"
19
+ require "railway_ipc/railtie" if defined?(Rails)
20
+
21
+ module RailwayIpc
22
+ def self.start
23
+ Rake::Task["sneakers:run"].invoke
24
+ end
25
+
26
+ def self.configure(logger: ::Logger.new(STDOUT))
27
+ @logger = RailwayIpc::Logger.new(logger)
28
+ end
29
+
30
+ def self.logger
31
+ @logger || RailwayIpc::Logger.new(::Logger.new(STDOUT))
32
+ end
33
+
34
+ def self.bunny_logger
35
+ logger.logger
36
+ end
37
+
38
+ def self.bunny_connection
39
+ @bunny_connection ||= RailwayIpc::Rabbitmq::Connection.create_bunny_connection
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ require 'railway_ipc'
2
+
3
+ path = File.expand_path(__dir__)
4
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
@@ -0,0 +1,87 @@
1
+ require "railway_ipc/rabbitmq/connection"
2
+ require "railway_ipc/concerns/message_handling"
3
+
4
+ module RailwayIpc
5
+ class Client
6
+ include RailwayIpc::Rabbitmq::Connection
7
+ include RailwayIpc::Concerns::MessageHandling
8
+
9
+ class TimeoutError < StandardError; end
10
+
11
+ attr_accessor :call_id, :response, :lock, :condition, :reply_queue, :request_message
12
+
13
+ def self.request(message)
14
+ new.request(message)
15
+ end
16
+
17
+ def self.publish_to(queue:, exchange:)
18
+ queue(queue)
19
+ exchange(exchange)
20
+ end
21
+
22
+ def initialize(queue=nil, pool=nil, opts={automatic_recovery: false})
23
+ super
24
+ setup_exchange
25
+ end
26
+
27
+ def request(request_message, timeout=10)
28
+ @request_message = request_message
29
+ setup_reply_queue
30
+ publish_message
31
+ poll_for_message(timeout)
32
+ build_timeout_response unless response
33
+ response
34
+ end
35
+
36
+ private
37
+
38
+ def setup_exchange
39
+ @exchange = Bunny::Exchange.new(channel, :fanout, self.class.exchange_name)
40
+ channel.queue(self.class.queue_name, auto_delete: true).bind(exchange)
41
+ end
42
+
43
+ def setup_reply_queue
44
+ @reply_queue = channel.queue('', auto_delete: true, exclusive: true)
45
+ @call_id = request_message.correlation_id
46
+ request_message.reply_to = reply_queue.name
47
+ end
48
+
49
+ def publish_message
50
+ RailwayIpc.logger.info(request_message, "Sending request")
51
+ exchange.publish(RailwayIpc::Rabbitmq::Payload.encode(request_message), routing_key: '')
52
+ end
53
+
54
+ def poll_for_message(timeout)
55
+ count = 0
56
+ until response || count >= timeout do
57
+ delivery_info, properties, payload = reply_queue.pop
58
+ handle_response(payload) if payload
59
+ count+= 1
60
+ sleep(1)
61
+ end
62
+ end
63
+
64
+ def build_timeout_response
65
+ error = TimeoutError.new("Client timed out")
66
+ response_message = rpc_error_adapter.error_message(error, request_message)
67
+ self.response = RailwayIpc::Response.new(response_message, success: false)
68
+ self.stop
69
+ end
70
+
71
+ def handle_response(payload)
72
+ begin
73
+ response_message = work(payload)
74
+ if response_message.correlation_id == self.call_id
75
+ RailwayIpc.logger.info(response_message, "Handling response")
76
+ self.response = RailwayIpc::Response.new(response_message, success: true)
77
+ self.stop
78
+ end
79
+ rescue StandardError => e
80
+ RailwayIpc.logger.error(message, "Error handling response. Error #{e.class}, message: #{e.message}")
81
+ response_message = rpc_error_adapter.error_message(e, message)
82
+ self.response = RailwayIpc::Response.new(response_message, success: false)
83
+ self.stop
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,118 @@
1
+ module RailwayIpc
2
+ module Concerns
3
+ module MessageHandling
4
+
5
+ HANDLERS = {}
6
+
7
+ def self.included(klass)
8
+ klass.extend(ClassMethods)
9
+ end
10
+
11
+ attr_reader :message, :handler
12
+ alias_method :responder, :handler
13
+
14
+ def work(payload)
15
+ decoded_payload = RailwayIpc::Rabbitmq::Payload.decode(payload)
16
+ @handler = handler_class(decoded_payload.type).new if handler_class(decoded_payload.type)
17
+ message_klass = message_class(decoded_payload.type)
18
+ if !message_klass
19
+ @message = self.rpc_error_message.decode(decoded_payload.message)
20
+ raise RailwayIpc::UnhandledMessageError, "#{self.class} does not know how to handle #{decoded_payload.type}"
21
+ else
22
+
23
+ @message = message_klass.decode(decoded_payload.message)
24
+ end
25
+ rescue StandardError => e
26
+ RailwayIpc.logger.log_exception({
27
+ feature: "railway_consumer",
28
+ error: e.class,
29
+ error_message: e.message,
30
+ payload: payload
31
+ })
32
+ raise e
33
+ end
34
+
35
+ def rpc_error_adapter
36
+ self.class.rpc_error_adapter_class
37
+ end
38
+
39
+ def rpc_error_message
40
+ self.class.rpc_error_message_class
41
+ end
42
+
43
+ private
44
+
45
+ def handler_class(message_type)
46
+ HANDLERS
47
+ .fetch(message_type, {})
48
+ .fetch(:handler_class, null_handler)
49
+ end
50
+
51
+ def message_class(message_type)
52
+ HANDLERS
53
+ .fetch(message_type, {})
54
+ .fetch(:message_class, null_message)
55
+ end
56
+
57
+ def null_message
58
+ self.class.ancestors
59
+ .grep(RailwayIpc::Concerns::MessageHandling::ClassMethods)
60
+ .reverse
61
+ .first
62
+ .null_message_class
63
+ end
64
+
65
+
66
+ def null_handler
67
+ self.class.ancestors
68
+ .grep(RailwayIpc::Concerns::MessageHandling::ClassMethods)
69
+ .reverse
70
+ .first
71
+ .null_handler_class
72
+ end
73
+
74
+ module ClassMethods
75
+ def rpc_error_message(rpc_error_message_class)
76
+ @rpc_error_message_class = rpc_error_message_class
77
+ end
78
+
79
+ def rpc_error_message_class
80
+ @rpc_error_message_class
81
+ end
82
+
83
+ def rpc_error_adapter(rpc_error_adapter)
84
+ @rpc_error_adapter_class = rpc_error_adapter
85
+ end
86
+
87
+ def rpc_error_adapter_class
88
+ @rpc_error_adapter_class
89
+ end
90
+
91
+ def handle_null_messages_with(null_handler)
92
+ @null_handler = null_handler
93
+ end
94
+
95
+ def null_handler_class
96
+ @null_handler
97
+ end
98
+
99
+ def handle_unknown_messages_as(null_message)
100
+ @null_message = null_message
101
+ end
102
+
103
+ def null_message_class
104
+ @null_message
105
+ end
106
+
107
+ def handle(message_type, with:nil)
108
+ HANDLERS[message_type.to_s] = {
109
+ handler_class: with,
110
+ message_class: message_type
111
+ }
112
+ end
113
+ alias_method :respond_to, :handle
114
+ alias_method :handle_response, :handle
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+ require 'base64'
3
+ require "railway_ipc/null_handler"
4
+
5
+ module RailwayIpc
6
+ class Consumer
7
+ include Sneakers::Worker
8
+ include RailwayIpc::Concerns::MessageHandling
9
+
10
+ handle_unknown_messages_as RailwayIpc::NullMessage
11
+ handle_null_messages_with RailwayIpc::NullHandler
12
+
13
+ def self.listen_to(queue:, exchange:)
14
+ from_queue queue,
15
+ exchange: exchange,
16
+ durable: true,
17
+ exchange_type: :fanout,
18
+ connection: RailwayIpc.bunny_connection
19
+ end
20
+
21
+ def work(payload)
22
+ super
23
+ handler.handle(message)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module RailwayIpc
2
+ class Handler
3
+ include Sneakers::Worker
4
+ class << self
5
+ attr_reader :block
6
+ end
7
+
8
+ def self.handle(&block)
9
+ @block = block
10
+ end
11
+
12
+ def handle(message)
13
+ RailwayIpc.logger.info(message, "Handling message")
14
+ response = self.class.block.call(message)
15
+ if response.success?
16
+ RailwayIpc.logger.info(message, "Successfully handled message")
17
+ ack!
18
+ else
19
+ RailwayIpc.logger.error(message, "Failed to handle message")
20
+ ack!
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ module RailwayIpc
2
+ class Logger
3
+
4
+ attr_reader :logger
5
+
6
+ def initialize(logger)
7
+ @logger = logger
8
+ end
9
+
10
+ def info(message, statement)
11
+ logger.info("[#{message_header(message)}] #{statement}")
12
+ end
13
+
14
+ def warn(message, statement)
15
+ logger.warn("[#{message_header(message)}] #{statement}")
16
+ end
17
+
18
+ def debug(message, statement)
19
+ logger.debug("[#{message_header(message)}] #{statement}")
20
+ end
21
+
22
+ def error(message, statement)
23
+ logger.error("[#{message_header(message)}] #{statement}")
24
+ end
25
+
26
+ def log_exception(e)
27
+ logger.error(e)
28
+ end
29
+
30
+ def message_header(message)
31
+ "message type: #{message.class}, uuid: #{message.uuid}, correlation_id: #{message.correlation_id}, user_uuid: #{message.user_uuid}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ module RailwayIpc
2
+ class NullHandler < RailwayIpc::Handler
3
+ def handle(message)
4
+ ack!
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module RailwayIpc
2
+ class NullMessage
3
+ def self.decode(message)
4
+ self.new
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ require 'singleton'
2
+ module RailwayIpc
3
+ class Publisher < Sneakers::Publisher
4
+ include ::Singleton
5
+
6
+ def self.exchange(exchange)
7
+ @exchange_name = exchange
8
+ end
9
+
10
+ def self.exchange_name
11
+ raise 'Subclass must set the exchange' unless @exchange_name
12
+ @exchange_name
13
+ end
14
+
15
+
16
+ def initialize
17
+ super(exchange: self.class.exchange_name, exchange_type: :fanout)
18
+ end
19
+
20
+ def publish(message)
21
+ RailwayIpc.logger.info(message, "Publishing message")
22
+ super(RailwayIpc::Rabbitmq::Payload.encode(message))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ require 'bunny'
2
+ module RailwayIpc
3
+ module Rabbitmq
4
+ module Connection
5
+
6
+ def self.included(klass)
7
+ klass.extend(ClassMethods)
8
+ end
9
+
10
+ def self.create_bunny_connection(opts={automatic_recovery: true})
11
+ settings = AMQ::Settings.parse_amqp_url(ENV["RAILWAY_RABBITMQ_CONNECTION_URL"])
12
+ Bunny.new(
13
+ host: settings[:host],
14
+ user: settings[:user],
15
+ pass: settings[:pass],
16
+ port: settings[:port],
17
+ automatic_recovery: opts[:automatic_recovery],
18
+ logger: RailwayIpc.bunny_logger)
19
+ end
20
+
21
+ def initialize(queue=nil, pool=nil, opts={automatic_recovery: true})
22
+ @connection = RailwayIpc::Rabbitmq::Connection.create_bunny_connection(opts)
23
+ connection.start
24
+ @channel = connection.create_channel
25
+ end
26
+
27
+ def stop
28
+ channel.close
29
+ connection.close
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :channel, :exchange, :queue, :connection
35
+
36
+ module ClassMethods
37
+ def queue(queue)
38
+ @queue_name = queue
39
+ end
40
+
41
+ def queue_name
42
+ @queue_name
43
+ end
44
+
45
+ def exchange(exchange)
46
+ @exchange_name = exchange
47
+ end
48
+
49
+ def exchange_name
50
+ @exchange_name
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,32 @@
1
+ module RailwayIpc
2
+ module Rabbitmq
3
+ class Payload
4
+ attr_reader :type, :message
5
+
6
+ def self.encode(message)
7
+ type = message.class.to_s
8
+ message = Base64.encode64(message.class.encode(message))
9
+ new(type, message).to_json
10
+ end
11
+
12
+ def self.decode(message)
13
+ message = JSON.parse(message)
14
+ type = message["type"]
15
+ message = Base64.decode64(message["encoded_message"])
16
+ new(type, message)
17
+ end
18
+
19
+ def initialize(type, message)
20
+ @type = type
21
+ @message = message
22
+ end
23
+
24
+ def to_json
25
+ {
26
+ type: type,
27
+ encoded_message: message
28
+ }.to_json
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module RailwayIpc
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :railway_ipc
4
+
5
+ rake_tasks do
6
+ path = File.expand_path(__dir__)
7
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module RailwayIpc
2
+ class Responder
3
+ def self.respond(&block)
4
+ @block = block
5
+ end
6
+
7
+ def self.block
8
+ @block
9
+ end
10
+
11
+ def respond(request)
12
+ RailwayIpc.logger.info(request, "Responding to request")
13
+ response = self.class.block.call(request)
14
+ raise ResponseTypeError.new(response.class) unless response.is_a?(Google::Protobuf::MessageExts)
15
+ response
16
+ end
17
+
18
+ class ResponseTypeError < StandardError
19
+ def initialize(response_class)
20
+ message = "`respond` block should return a Google Protobuf message that the corresponding client can handle, instead returned #{response_class}"
21
+ super(message)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module RailwayIpc
2
+ class Response
3
+ attr_reader :body, :success
4
+ def initialize(message, success:true)
5
+ @body = message
6
+ @success = success
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ require "railway_ipc/rabbitmq/connection"
2
+ require "railway_ipc/concerns/message_handling"
3
+
4
+ module RailwayIpc
5
+ class Server
6
+ include RailwayIpc::Rabbitmq::Connection
7
+ include RailwayIpc::Concerns::MessageHandling
8
+
9
+ def self.listen_to(queue:)
10
+ queue(queue)
11
+ end
12
+
13
+ def initialize(queue=nil, pool=nil, opts={automatic_recovery: true})
14
+ super
15
+ @exchange = channel.default_exchange
16
+ end
17
+
18
+ def run
19
+ @queue = channel.queue(self.class.queue_name, auto_delete: true)
20
+ subscribe_to_queue
21
+ end
22
+
23
+ def work(payload)
24
+ super
25
+ responder.respond(message)
26
+ end
27
+
28
+ private
29
+
30
+ def subscribe_to_queue
31
+ queue.subscribe do |_delivery_info, metadata, payload|
32
+ handle_request(payload)
33
+ end
34
+ end
35
+
36
+ def handle_request(payload)
37
+ begin
38
+ response = work(payload)
39
+ rescue StandardError => e
40
+ RailwayIpc.logger.error(message, "Error responding to message. Error: #{e.class}, #{e.message}")
41
+ response = self.rpc_error_adapter.error_message(e, message)
42
+ ensure
43
+ exchange.publish(
44
+ RailwayIpc::Rabbitmq::Payload.encode(response),
45
+ routing_key: message.reply_to
46
+ ) if response
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ namespace :railway_ipc do
2
+ namespace :consumers do
3
+ task :start do
4
+ ENV["WORKERS"] = ENV["CONSUMERS"]
5
+ RailwayIpc.start
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ namespace :railway_ipc do
2
+ namespace :servers do
3
+ task :start do
4
+ ENV["WORKERS"] = ENV["SERVERS"]
5
+ RailwayIpc.start
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module RailwayIpc
2
+ class UnhandledMessageError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module RailwayIpc
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "railway_ipc/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "railway-ipc"
7
+ spec.version = RailwayIpc::VERSION
8
+ spec.authors = ""
9
+ spec.email = ""
10
+
11
+ spec.summary = %q{IPC components for Rails}
12
+ spec.description = %q{IPC components for Rails}
13
+ spec.homepage = "http://learn.co"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "rake", ">= 10.0.0"
35
+ spec.add_development_dependency "bundler", "2.0.1"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "pry-byebug", '3.4.2'
38
+ spec.add_development_dependency 'google-protobuf', '~> 3.9'
39
+ spec.add_dependency 'sneakers', '~> 2.3.5'
40
+ spec.add_dependency 'bunny', '~> 2.2.0'
41
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railway-ipc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - ''
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 10.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 10.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: google-protobuf
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sneakers
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.5
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.3.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: bunny
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.2.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.2.0
111
+ description: IPC components for Rails
112
+ email: ''
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - ".travis.yml"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/railway_ipc.rb
129
+ - lib/railway_ipc/Rakefile
130
+ - lib/railway_ipc/client.rb
131
+ - lib/railway_ipc/concerns/message_handling.rb
132
+ - lib/railway_ipc/consumer.rb
133
+ - lib/railway_ipc/handler.rb
134
+ - lib/railway_ipc/logger.rb
135
+ - lib/railway_ipc/null_handler.rb
136
+ - lib/railway_ipc/null_message.rb
137
+ - lib/railway_ipc/publisher.rb
138
+ - lib/railway_ipc/rabbitmq/connection.rb
139
+ - lib/railway_ipc/rabbitmq/payload.rb
140
+ - lib/railway_ipc/railtie.rb
141
+ - lib/railway_ipc/responder.rb
142
+ - lib/railway_ipc/response.rb
143
+ - lib/railway_ipc/server.rb
144
+ - lib/railway_ipc/tasks/start_consumers.rake
145
+ - lib/railway_ipc/tasks/start_servers.rake
146
+ - lib/railway_ipc/unhandled_message_error.rb
147
+ - lib/railway_ipc/version.rb
148
+ - railway_ipc.gemspec
149
+ homepage: http://learn.co
150
+ licenses:
151
+ - MIT
152
+ metadata:
153
+ allowed_push_host: https://rubygems.org/
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.7.7
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: IPC components for Rails
174
+ test_files: []