railway-ipc 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +8 -0
- data/.tool-versions +1 -0
- data/Gemfile.lock +125 -1
- data/README.md +5 -0
- data/lib/railway_ipc.rb +28 -21
- data/lib/railway_ipc/base_message.pb.rb +21 -0
- data/lib/railway_ipc/consumer/consumer.rb +112 -0
- data/lib/railway_ipc/consumer/consumer_response_handlers.rb +14 -0
- data/lib/railway_ipc/errors.rb +1 -0
- data/lib/railway_ipc/handler.rb +2 -0
- data/lib/railway_ipc/handler_manifest.rb +10 -0
- data/lib/railway_ipc/handler_store.rb +21 -0
- data/lib/railway_ipc/models/consumed_message.rb +48 -0
- data/lib/railway_ipc/models/published_message.rb +27 -0
- data/lib/railway_ipc/null_message.rb +1 -1
- data/lib/railway_ipc/publisher.rb +9 -4
- data/lib/railway_ipc/rabbitmq/adapter.rb +93 -0
- data/lib/railway_ipc/rabbitmq/payload.rb +7 -3
- data/lib/railway_ipc/rpc/client/client.rb +104 -0
- data/lib/railway_ipc/rpc/client/client_response_handlers.rb +25 -0
- data/lib/railway_ipc/rpc/client/errors/timeout_error.rb +5 -0
- data/lib/railway_ipc/rpc/concerns/error_adapter_configurable.rb +13 -0
- data/lib/railway_ipc/rpc/concerns/message_observation_configurable.rb +18 -0
- data/lib/railway_ipc/rpc/concerns/publish_location_configurable.rb +13 -0
- data/lib/railway_ipc/rpc/rpc.rb +2 -0
- data/lib/railway_ipc/rpc/server/server.rb +89 -0
- data/lib/railway_ipc/rpc/server/server_response_handlers.rb +17 -0
- data/lib/railway_ipc/tasks/generate_migrations.rake +26 -0
- data/lib/railway_ipc/version.rb +1 -1
- data/priv/migrations/add_railway_ipc_consumed_messages.rb +19 -0
- data/priv/migrations/add_railway_ipc_published_messages.rb +18 -0
- data/railway_ipc.gemspec +25 -16
- metadata +123 -8
- data/lib/railway_ipc/client.rb +0 -87
- data/lib/railway_ipc/concerns/message_handling.rb +0 -118
- data/lib/railway_ipc/consumer.rb +0 -26
- data/lib/railway_ipc/rabbitmq/connection.rb +0 -55
- data/lib/railway_ipc/server.rb +0 -50
@@ -1,118 +0,0 @@
|
|
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
|
data/lib/railway_ipc/consumer.rb
DELETED
@@ -1,26 +0,0 @@
|
|
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
|
@@ -1,55 +0,0 @@
|
|
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
|
data/lib/railway_ipc/server.rb
DELETED
@@ -1,50 +0,0 @@
|
|
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, durable: 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
|