mimi-messaging 0.1.9 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +66 -0
  4. data/README.md +68 -3
  5. data/TODO.md +8 -0
  6. data/docs/Messaging_Layer_Properties.md +141 -0
  7. data/docs/Why_HTTP_is_a_bad_choice.md +20 -0
  8. data/docs/diagrams/Pattern -- Command.drawio +1 -0
  9. data/docs/diagrams/Pattern -- Event direct.drawio +1 -0
  10. data/docs/diagrams/Pattern -- Event with Queue.drawio +1 -0
  11. data/docs/diagrams/Pattern -- Event.drawio +1 -0
  12. data/docs/diagrams/Pattern -- Query.drawio +1 -0
  13. data/docs/img/pattern--command.png +0 -0
  14. data/docs/img/pattern--event-direct.png +0 -0
  15. data/docs/img/pattern--event-using-queue.png +0 -0
  16. data/docs/img/pattern--event.png +0 -0
  17. data/docs/img/pattern--query.png +0 -0
  18. data/examples/basic_event_listener.rb +35 -0
  19. data/examples/basic_request_processor.rb +38 -0
  20. data/examples/using_messaging_low.rb +59 -0
  21. data/examples/using_pure_adapter.rb +62 -0
  22. data/lib/mimi/messaging.rb +429 -92
  23. data/lib/mimi/messaging/adapters.rb +22 -0
  24. data/lib/mimi/messaging/adapters/base.rb +233 -0
  25. data/lib/mimi/messaging/adapters/memory.rb +119 -0
  26. data/lib/mimi/messaging/adapters/test.rb +50 -0
  27. data/lib/mimi/messaging/errors.rb +24 -12
  28. data/lib/mimi/messaging/json_serializer.rb +45 -0
  29. data/lib/mimi/messaging/message.rb +25 -65
  30. data/lib/mimi/messaging/version.rb +3 -1
  31. data/mimi-messaging.gemspec +25 -23
  32. metadata +34 -77
  33. data/lib/mimi/messaging/connection.rb +0 -181
  34. data/lib/mimi/messaging/listener.rb +0 -72
  35. data/lib/mimi/messaging/mock.rb +0 -13
  36. data/lib/mimi/messaging/mock/connection.rb +0 -153
  37. data/lib/mimi/messaging/mock/request.rb +0 -18
  38. data/lib/mimi/messaging/mock/request_processor.rb +0 -92
  39. data/lib/mimi/messaging/model.rb +0 -27
  40. data/lib/mimi/messaging/model_provider.rb +0 -100
  41. data/lib/mimi/messaging/msgpack/msgpack_ext.rb +0 -14
  42. data/lib/mimi/messaging/msgpack/type_packer.rb +0 -104
  43. data/lib/mimi/messaging/notification.rb +0 -35
  44. data/lib/mimi/messaging/provider.rb +0 -48
  45. data/lib/mimi/messaging/request.rb +0 -56
  46. data/lib/mimi/messaging/request_processor.rb +0 -216
  47. data/lib/mimi/messaging/request_processor/context.rb +0 -39
  48. data/lib/mimi/messaging/request_processor/dsl.rb +0 -121
  49. data/lib/tasks/console_ext.rake +0 -6
  50. data/lib/tasks/console_helpers.rb +0 -116
@@ -1,72 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class Listener < RequestProcessor
4
- DEFAULT_DURABLE_QUEUE_OPTIONS = {
5
- exclusive: false,
6
- durable: true,
7
- auto_delete: false
8
- }
9
-
10
- DEFAULT_TEMPORARY_QUEUE_OPTIONS = {
11
- exclusive: true,
12
- durable: false,
13
- auto_delete: true
14
- }
15
-
16
- abstract!
17
-
18
- queue_options DEFAULT_TEMPORARY_QUEUE_OPTIONS
19
-
20
- def self.queue(name = nil, opts = {})
21
- return super unless name
22
- queue_name(name)
23
- if name && name != ''
24
- queue_options(DEFAULT_DURABLE_QUEUE_OPTIONS.merge(opts))
25
- else
26
- queue_options(DEFAULT_TEMPORARY_QUEUE_OPTIONS.merge(opts))
27
- end
28
- end
29
-
30
- def self.default_queue_name
31
- ''
32
- end
33
-
34
- # Sets or gets notification resource name
35
- #
36
- def self.notification(name = nil, _opts = {})
37
- notification_name name
38
- true
39
- end
40
-
41
- # Sets or gets queue name
42
- #
43
- def self.notification_name(name = nil)
44
- if name && @notification_name
45
- raise "#{self} has already registered '#{@notification_name}' as notification name"
46
- end
47
- (@notification_name ||= name) || default_notification_name
48
- end
49
-
50
- # Default (inferred) notification name
51
- #
52
- def self.default_notification_name
53
- class_name_to_resource_name(name, 'listener')
54
- end
55
-
56
- def self.resource_name
57
- notification_name
58
- end
59
-
60
- def self.request_type(_d, _m, _p)
61
- :broadcast
62
- end
63
-
64
- def self.construct_queue
65
- exchange = channel.fanout(resource_name)
66
- q = channel.create_queue(queue_name, queue_options)
67
- q.bind(exchange)
68
- q
69
- end
70
- end # class Listener
71
- end # module Messaging
72
- end # module Mimi
@@ -1,13 +0,0 @@
1
- #
2
- # Replaces real Connection and Channel with MockConnection and MockChannel
3
- # that stubs connections to RabbitMQ.
4
- #
5
- # Replaces RequestProcessor with MockRequestProcessor
6
- #
7
-
8
- require_relative 'mock/connection'
9
- require_relative 'mock/request_processor'
10
- require_relative 'mock/request'
11
-
12
- Mimi::Messaging.send :remove_const, :Connection
13
- Mimi::Messaging::Connection = Mimi::Messaging::MockConnection
@@ -1,153 +0,0 @@
1
- require 'bunny'
2
-
3
- module Mimi
4
- module Messaging
5
- class MockConnection
6
- attr_reader :queue_prefix
7
-
8
- # Creates a Connection with given connection params
9
- #
10
- # @param params [Hash] Connection params as accepted by Bunny
11
- # @param params[:queue_prefix] [String] (optional) Use this connection for all communication
12
- # related to queues, having names starting with given
13
- # prefix
14
- #
15
- def initialize(params = {})
16
- @queue_prefix = params[:queue_prefix]
17
- @channel_pool = {}
18
- bunny_params = {
19
- host: params[:mq_host],
20
- port: params[:mq_port],
21
- username: params[:mq_username],
22
- password: params[:mq_password],
23
- vhost: params[:mq_vhost]
24
- }
25
- @connection = { mock_connection: bunny_params }
26
- end
27
-
28
- # Starts the connection, opening actual connection to RabbitMQ
29
- #
30
- def start
31
- @started = true
32
- end
33
-
34
- # Stops the connection
35
- #
36
- def stop
37
- @channel_pool = {}
38
- @started = false
39
- end
40
-
41
- def started?
42
- @started
43
- end
44
-
45
- def channel
46
- raise ConnectionError unless started?
47
- @channel_pool[Thread.current.object_id] ||= create_channel
48
- end
49
-
50
- def create_channel(opts = {})
51
- MockChannel.new(@connection, opts)
52
- end
53
-
54
- def reply_queue
55
- raise ConnectionError unless started?
56
- channel.reply_queue
57
- end
58
-
59
- def post(queue_name, raw_message, params = {})
60
- channel.post(queue_name, raw_message, params)
61
- end
62
-
63
- def get(queue_name, raw_message, params = {})
64
- channel.get(queue_name, raw_message, params)
65
- end
66
-
67
- def broadcast(queue_name, raw_message, params = {})
68
- channel.broadcast(queue_name, raw_message, params)
69
- end
70
-
71
- class MockQueue
72
- def initialize(*)
73
- end
74
- end
75
-
76
- class MockChannel
77
- attr_reader :options, :connection
78
-
79
- DEFAULT_OPTIONS = {
80
- concurrency: 1
81
- }
82
- DEFAULT_GET_TIMEOUT = 60 # seconds
83
-
84
- def initialize(connection, opts = {})
85
- @connection = connection
86
- @options = DEFAULT_OPTIONS.merge(opts)
87
- @channel = { mock_channel: @connection, opts: options[:concurrency] }
88
- @mutex = Mutex.new
89
- end
90
-
91
- def create_queue(name, opts = {})
92
- MockQueue.new(name, opts)
93
- end
94
-
95
- def reply_queue
96
- @reply_queue ||= create_queue('', exclusive: true)
97
- end
98
-
99
- def ack(tag)
100
- raise "Not implemented"
101
- end
102
-
103
- def fanout(name)
104
- raise "Not implemented"
105
- end
106
-
107
- def active?
108
- true
109
- end
110
-
111
- # Sends a raw RabbitMQ message to a given direct exchange
112
- #
113
- # @param queue_name [String] Queue name to send the message to
114
- # @param raw_message [String]
115
- # @param params [Hash] Message params (metadata)
116
- #
117
- def post(queue_name, raw_message, params = {})
118
- true
119
- end
120
-
121
- # Sends a raw RabbitMQ message to a given direct exchange and listens for response
122
- #
123
- # @param queue_name [String] Queue name to send the message to
124
- # @param raw_message [String]
125
- # @param params [Hash] Message params (metadata)
126
- #
127
- # @param params[:timeout] [Integer] (optional) Timeout in seconds
128
- #
129
- # @return [nil,Array]
130
- #
131
- def get(queue_name, raw_message, params = {})
132
- nil
133
- end
134
-
135
- # Sends a raw RabbitMQ message to a given fanout exchange
136
- #
137
- # @param fanout_name [String] Fanout exchange name to send the message to
138
- # @param raw_message [String]
139
- # @param params [Hash] Message params (metadata)
140
- #
141
- def broadcast(fanout_name, raw_message, params = {})
142
- true
143
- end
144
-
145
- private
146
-
147
- def publish(exchange, raw_message, params = {})
148
- true
149
- end
150
- end # class Channel
151
- end # class Connection
152
- end # module Messaging
153
- end # module Mimi
@@ -1,18 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class Request
4
- attr_reader :response
5
-
6
- def send_response(data = {})
7
- return if !get? || replied?
8
- raise ArgumentError, 'Invalid response format, Hash is expected' unless data.is_a?(Hash)
9
- raw_message = Mimi::Messaging::Message.encode(data)
10
- @response = Mimi::Messaging::Message.decode(raw_message)
11
- request_processor.connection.post(
12
- reply_to_queue_name, raw_message, correlation_id: metadata[:correlation_id]
13
- )
14
- @replied = true
15
- end
16
- end # class Request
17
- end # module Messaging
18
- end # module Mimi
@@ -1,92 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class RequestProcessor
4
- attr_reader :result
5
-
6
- def self.started?
7
- !@consumer.nil?
8
- end
9
-
10
- # Mock start
11
- #
12
- def self.start
13
- return if abstract?
14
- raise "#{name} already started" if started?
15
- logger.debug "#{self} starting to serve '#{resource_name}' (#{exposed_methods})"
16
- @queue = construct_queue
17
- @consumer_mutex = Mutex.new
18
- @consumer_mutex.synchronize do
19
- @consumer = true
20
- end
21
- # consumer created, mutex released
22
- end
23
-
24
- # Mock stop
25
- #
26
- def self.stop
27
- return if abstract?
28
- raise "#{name} already stopped" unless started?
29
- @consumer_mutex.synchronize do
30
- @consumer = nil
31
- end
32
- @consumer = nil
33
- @queue = nil
34
- @channel = nil
35
- @connection = nil
36
- end
37
-
38
- # MockRequestProcessor helper GET methods
39
- #
40
- # @param method_name [Symbol]
41
- # @param message [Hash]
42
- #
43
- # @return [Mimi::Messaging::Message]
44
- #
45
- def self.get(method_name, message)
46
- metadata = Mimi::Messaging::Message.new(
47
- correlation_id: 1,
48
- reply_to: 'mock_client',
49
- headers: {
50
- 'method_name' => method_name.to_s,
51
- 'c' => 'mock-context'
52
- }
53
- )
54
- d = Mimi::Messaging::Message.new()
55
- raw_message = Mimi::Messaging::Message.new(message).to_msgpack
56
- request_processor = new(d, metadata, raw_message)
57
- request_processor.result
58
- end
59
-
60
- # MockRequestProcessor helper POST method
61
- #
62
- # @param method_name [Symbol]
63
- # @param message [Hash]
64
- #
65
- # @return [Mimi::Messaging::Message]
66
- #
67
- def self.post(method_name, message)
68
- metadata = Mimi::Messaging::Message.new(
69
- headers: {
70
- 'method_name' => method_name.to_s,
71
- 'c' => 'context-id'
72
- }
73
- )
74
- d = Mimi::Messaging::Message.new()
75
- raw_message = Mimi::Messaging::Message.new(message)
76
- request_processor = new(d, metadata, raw_message)
77
- nil
78
- end
79
-
80
- # MockRequestProcessor helper BROADCAST method
81
- #
82
- # @param method_name [Symbol]
83
- # @param message [Hash]
84
- #
85
- # @return [Mimi::Messaging::Message]
86
- #
87
- def self.broadcast(method_name, message)
88
- post(method_name, message)
89
- end
90
- end # class RequestProcessor
91
- end # module Messaging
92
- end # module Mimi
@@ -1,27 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class Model < Mimi::Messaging::Message
4
- def self.default_queue_name
5
- Mimi::Messaging::RequestProcessor.class_name_to_resource_name(self, 'model')
6
- end
7
-
8
- methods :create, :update, :show, :destroy, :list
9
-
10
- def update(params = {})
11
- self.replace(self.class.update(params.merge(id: id)))
12
- end
13
-
14
- def save
15
- update(self)
16
- end
17
-
18
- def self.find(id)
19
- show(id: id)
20
- end
21
-
22
- def self.all
23
- list.list
24
- end
25
- end
26
- end # module Messaging
27
- end # module Mimi
@@ -1,100 +0,0 @@
1
- module Mimi
2
- module Messaging
3
- class ModelProvider < Mimi::Messaging::Provider
4
- abstract!
5
-
6
- def self.model(model_class, options = {})
7
- raise "#{self} already serves model #{@model_class}" if @model_class
8
- @model_class = model_class
9
- @model_options = options
10
- @model_methods_only = options[:only] ? [*(options[:only])] : nil
11
- @model_methods_except = options[:except] ? [*(options[:except])] : nil
12
- end
13
-
14
- def self.exposed_methods
15
- model_methods = Mimi::Messaging::ModelProvider.public_instance_methods(false).dup
16
- methods_hidden = []
17
- methods_hidden = model_methods - model_methods.only(*@model_methods_only) if @model_methods_only
18
- methods_hidden = model_methods.only(*@model_methods_except) if @model_methods_except
19
- m = super
20
- m - methods_hidden
21
- end
22
-
23
- def self.model_class
24
- @model_class || raise("#{self} has no defined model")
25
- end
26
-
27
- def self.serialize(method_name = nil, &block)
28
- if method_name && block_given?
29
- raise "Only one of method_name or block is accepted by #{self}.serialize"
30
- end
31
- @serialize = method_name.to_sym if method_name
32
- @serialize = block if block_given?
33
- @serialize || parent_property(:serialize) || :as_json
34
- end
35
-
36
- def self.scope(&block)
37
- @scope = block if block_given?
38
- @scope || -> (r) { r }
39
- end
40
-
41
- def self.permitted_params(*names)
42
- @permitted_params = names.map(&:to_sym) if names.size > 0
43
- @permitted_params || model_class.attribute_names.map(&:to_sym)
44
- end
45
-
46
- def create
47
- serialize model_class_scoped.create!(permitted_params)
48
- end
49
-
50
- def update(id:)
51
- model = model_class_scoped.find(id)
52
- model.update!(permitted_params)
53
- serialize model
54
- end
55
-
56
- def show(id:)
57
- serialize model_class_scoped.find(id)
58
- end
59
-
60
- def find
61
- serialize model_class_scoped.find_by!(params)
62
- end
63
-
64
- def destroy(id:)
65
- model_class_scoped.find(id)
66
- raise "#{self.class}#destroy is not implemented"
67
- end
68
-
69
- def list
70
- reply list: model_class_scoped.all.map { |v| serialize v }
71
- end
72
-
73
- private
74
-
75
- def permitted_params
76
- params.except('id').only(*self.class.permitted_params.map(&:to_s)).to_hash
77
- end
78
-
79
- def model_class
80
- self.class.model_class
81
- end
82
-
83
- def model_class_scoped
84
- scope_block = self.class.scope
85
- __execute(model_class, &scope_block)
86
- end
87
-
88
- def serialize(model_instance, opts = nil)
89
- opts ||= params
90
- if self.class.serialize.is_a?(Symbol)
91
- model_instance.send(self.class.serialize, opts)
92
- elsif self.class.serialize.is_a?(Proc)
93
- self.class.serialize.call(model_instance, opts)
94
- else
95
- raise "#{self.class}#serialize is neither a Symbol or Proc"
96
- end
97
- end
98
- end # class ModelProvider
99
- end # module Messaging
100
- end # module Mimi