mimi-messaging 0.1.1
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +11 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/mimi/messaging.rb +121 -0
- data/lib/mimi/messaging/connection.rb +181 -0
- data/lib/mimi/messaging/errors.rb +19 -0
- data/lib/mimi/messaging/listener.rb +72 -0
- data/lib/mimi/messaging/message.rb +66 -0
- data/lib/mimi/messaging/model.rb +27 -0
- data/lib/mimi/messaging/model_provider.rb +96 -0
- data/lib/mimi/messaging/notification.rb +31 -0
- data/lib/mimi/messaging/packer.rb +92 -0
- data/lib/mimi/messaging/provider.rb +48 -0
- data/lib/mimi/messaging/request.rb +56 -0
- data/lib/mimi/messaging/request_processor.rb +195 -0
- data/lib/mimi/messaging/request_processor/context.rb +39 -0
- data/lib/mimi/messaging/request_processor/dsl.rb +121 -0
- data/lib/mimi/messaging/version.rb +5 -0
- data/mimi-messaging.gemspec +38 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f372a9f357c460683c5be1e6a4da12e17c628a8
|
4
|
+
data.tar.gz: 4949e060f16f06515a14db225fb629d6e1b7a05d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f03dd1c4f51d39abcaeaa6edf11f4a3d00a71f27cf504eab966e33eda20f47869f6724a23a11d7f9c708b2cb0ad3de49f49806b00ecc4b97ba9bcb9b0d27008
|
7
|
+
data.tar.gz: db39cd12d196d7932e1a8f8fbb79b4bd12c949fa06b30393edeffcc0f8244992f566ffc2c79c1002cb59b1d2014ab16609d6d71dcaae62f0cfc54b27f98f4777
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at alex@kukushk.in. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alex Kukushkin
|
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
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'mimi/core'
|
2
|
+
require 'mimi/logger'
|
3
|
+
|
4
|
+
module Mimi
|
5
|
+
module Messaging
|
6
|
+
include Mimi::Core::Module
|
7
|
+
include Mimi::Logger::Instance
|
8
|
+
|
9
|
+
default_options(
|
10
|
+
require_files: 'app/messaging/**/*.rb',
|
11
|
+
mq_host: 'localhost',
|
12
|
+
mq_port: 5672,
|
13
|
+
mq_username: nil,
|
14
|
+
mq_password: nil,
|
15
|
+
mq_vhost: nil
|
16
|
+
)
|
17
|
+
|
18
|
+
def self.module_path
|
19
|
+
Pathname.new(__dir__).join('..').join('..').expand_path
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.module_manifest
|
23
|
+
{
|
24
|
+
mq_host: {
|
25
|
+
desc: 'RabbitMQ host',
|
26
|
+
default: 'localhost'
|
27
|
+
},
|
28
|
+
mq_port: {
|
29
|
+
desc: 'RabbitMQ port',
|
30
|
+
default: 5672
|
31
|
+
},
|
32
|
+
mq_username: {
|
33
|
+
desc: 'RabbitMQ username',
|
34
|
+
default: nil
|
35
|
+
},
|
36
|
+
mq_password: {
|
37
|
+
desc: 'RabbitMQ password',
|
38
|
+
default: nil
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.configure(*)
|
44
|
+
super
|
45
|
+
connections << Mimi::Messaging::Connection.new(module_options)
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [Array<Mimi::Messaging::Connection>]
|
49
|
+
#
|
50
|
+
def self.connections
|
51
|
+
@connections ||= []
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [Array<Class < Mimi::Messaging::RequestProcessor>]
|
55
|
+
#
|
56
|
+
def self.request_processor_classes
|
57
|
+
@request_processor_classes ||= []
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param [Class < Mimi::Messaging::RequestProcessor]
|
61
|
+
#
|
62
|
+
def self.register_request_processor_class(request_processor_class)
|
63
|
+
request_processor_classes << request_processor_class
|
64
|
+
end
|
65
|
+
|
66
|
+
# Selects the connection to be used for sending/receiving messages from/to given queue
|
67
|
+
#
|
68
|
+
# @param queue_name [String]
|
69
|
+
# @return [Mimi::Messaging::Connection]
|
70
|
+
#
|
71
|
+
def self.connection_for(queue_name)
|
72
|
+
connection_for_queue = connections.select do |c|
|
73
|
+
c.queue_prefix && (
|
74
|
+
(c.queue_prefix.is_a?(String) && queue_name.start_with?(c.queue_prefix)) ||
|
75
|
+
(c.queue_prefix.is_a?(Array) && c.queue_prefix.any? { |qp| queue_name.start_with?(qp) })
|
76
|
+
)
|
77
|
+
end.first
|
78
|
+
return connection_for_queue if connection_for_queue
|
79
|
+
connections.select { |c| c.queue_prefix.nil? }.first
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.post(queue_name, raw_message, params = {})
|
83
|
+
connection_for(queue_name).post(queue_name, raw_message, params)
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.get(queue_name, raw_message, params = {})
|
87
|
+
connection_for(queue_name).get(queue_name, raw_message, params)
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.broadcast(queue_name, raw_message, params = {})
|
91
|
+
connection_for(queue_name).broadcast(queue_name, raw_message, params)
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.start
|
95
|
+
Mimi.require_files(module_options[:require_files]) if module_options[:require_files]
|
96
|
+
connections.each(&:start)
|
97
|
+
request_processor_classes.each(&:start)
|
98
|
+
super
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.stop
|
102
|
+
request_processor_classes.each(&:stop)
|
103
|
+
connections.each(&:stop)
|
104
|
+
super
|
105
|
+
end
|
106
|
+
end # module Messaging
|
107
|
+
end # module Mimi
|
108
|
+
|
109
|
+
require_relative 'messaging/version'
|
110
|
+
require_relative 'messaging/packer'
|
111
|
+
|
112
|
+
require_relative 'messaging/errors'
|
113
|
+
require_relative 'messaging/connection'
|
114
|
+
require_relative 'messaging/message'
|
115
|
+
require_relative 'messaging/request'
|
116
|
+
require_relative 'messaging/request_processor'
|
117
|
+
require_relative 'messaging/provider'
|
118
|
+
require_relative 'messaging/model'
|
119
|
+
require_relative 'messaging/model_provider'
|
120
|
+
require_relative 'messaging/notification'
|
121
|
+
require_relative 'messaging/listener'
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module Mimi
|
4
|
+
module Messaging
|
5
|
+
class Connection
|
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 = Bunny.new(bunny_params)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Starts the connection, opening actual connection to RabbitMQ
|
29
|
+
#
|
30
|
+
def start
|
31
|
+
@connection.start
|
32
|
+
end
|
33
|
+
|
34
|
+
# Stops the connection
|
35
|
+
#
|
36
|
+
def stop
|
37
|
+
@connection.close
|
38
|
+
@channel_pool = {}
|
39
|
+
end
|
40
|
+
|
41
|
+
def started?
|
42
|
+
@connection.status == :open
|
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
|
+
Channel.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 Channel
|
72
|
+
attr_reader :options, :connection
|
73
|
+
|
74
|
+
DEFAULT_OPTIONS = {
|
75
|
+
concurrency: 1
|
76
|
+
}
|
77
|
+
DEFAULT_GET_TIMEOUT = 60 # seconds
|
78
|
+
|
79
|
+
def initialize(connection, opts = {})
|
80
|
+
@connection = connection
|
81
|
+
@options = DEFAULT_OPTIONS.merge(opts)
|
82
|
+
@channel = @connection.create_channel(nil, options[:concurrency])
|
83
|
+
@mutex = Mutex.new
|
84
|
+
end
|
85
|
+
|
86
|
+
def create_queue(name, opts = {})
|
87
|
+
@channel.queue(name, opts)
|
88
|
+
end
|
89
|
+
|
90
|
+
def reply_queue
|
91
|
+
@reply_queue ||= create_queue('', exclusive: true)
|
92
|
+
end
|
93
|
+
|
94
|
+
def ack(tag)
|
95
|
+
@channel.ack(tag)
|
96
|
+
end
|
97
|
+
|
98
|
+
def fanout(name)
|
99
|
+
@channel.fanout(name)
|
100
|
+
end
|
101
|
+
|
102
|
+
def active?
|
103
|
+
@channel && @channel.active
|
104
|
+
end
|
105
|
+
|
106
|
+
# Sends a raw RabbitMQ message to a given direct exchange
|
107
|
+
#
|
108
|
+
# @param queue_name [String] Queue name to send the message to
|
109
|
+
# @param raw_message [String]
|
110
|
+
# @param params [Hash] Message params (metadata)
|
111
|
+
#
|
112
|
+
def post(queue_name, raw_message, params = {})
|
113
|
+
x = @channel.default_exchange
|
114
|
+
params = { routing_key: queue_name }.merge(params.dup)
|
115
|
+
publish(x, raw_message, params)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Sends a raw RabbitMQ message to a given direct exchange and listens for response
|
119
|
+
#
|
120
|
+
# @param queue_name [String] Queue name to send the message to
|
121
|
+
# @param raw_message [String]
|
122
|
+
# @param params [Hash] Message params (metadata)
|
123
|
+
#
|
124
|
+
# @param params[:timeout] [Integer] (optional) Timeout in seconds
|
125
|
+
#
|
126
|
+
# @return [nil,Array]
|
127
|
+
#
|
128
|
+
def get(queue_name, raw_message, params = {})
|
129
|
+
correlation_id = Time.now.utc.to_f.to_s
|
130
|
+
params = params.dup.merge(
|
131
|
+
reply_to: reply_queue.name,
|
132
|
+
correlation_id: correlation_id
|
133
|
+
)
|
134
|
+
post(queue_name, raw_message, params)
|
135
|
+
response = nil
|
136
|
+
begin
|
137
|
+
Timeout.timeout(params[:timeout] || DEFAULT_GET_TIMEOUT) do
|
138
|
+
loop do
|
139
|
+
d, m, p = reply_queue.pop
|
140
|
+
next if d && m.correlation_id != correlation_id
|
141
|
+
response = [d, m, p] if d
|
142
|
+
break if response
|
143
|
+
sleep 0.001 # s
|
144
|
+
end
|
145
|
+
end
|
146
|
+
rescue Timeout::Error
|
147
|
+
# respond with nil
|
148
|
+
end
|
149
|
+
response
|
150
|
+
end
|
151
|
+
|
152
|
+
# Sends a raw RabbitMQ message to a given fanout exchange
|
153
|
+
#
|
154
|
+
# @param fanout_name [String] Fanout exchange name to send the message to
|
155
|
+
# @param raw_message [String]
|
156
|
+
# @param params [Hash] Message params (metadata)
|
157
|
+
#
|
158
|
+
def broadcast(fanout_name, raw_message, params = {})
|
159
|
+
x = @channel.fanout(fanout_name)
|
160
|
+
publish(x, raw_message, params)
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def publish(exchange, raw_message, params = {})
|
166
|
+
# HACK: Connection-level mutex reduces throughoutput, hopefully improves stability (ku)
|
167
|
+
@mutex.synchronize do
|
168
|
+
# TODO: may be make publishing an atomic operation using a separate thread? (ku)
|
169
|
+
exchange.publish(raw_message, params)
|
170
|
+
end
|
171
|
+
rescue StandardError => e
|
172
|
+
# Raising fatal error:
|
173
|
+
unless Thread.main == Thread.current
|
174
|
+
Thread.main.raise ConnectionError, "failed to publish message in a child thread: #{e}"
|
175
|
+
end
|
176
|
+
raise ConnectionError, "failed to publish message: #{e}"
|
177
|
+
end
|
178
|
+
end # class Channel
|
179
|
+
end # class Connection
|
180
|
+
end # module Messaging
|
181
|
+
end # module Mimi
|