mimi-messaging 0.1.6 → 0.1.7

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
- SHA1:
3
- metadata.gz: 1be22f48791e6d899580bb5b822b1042de26660a
4
- data.tar.gz: 3c09e549007a840afb82e9406891546ab30cd3b2
2
+ SHA256:
3
+ metadata.gz: cfa2d2cc7cda8ea79f956fd6325a43f873948cf88b9979516f5b6a72b3e3f364
4
+ data.tar.gz: c52eea14384c0d239fdda08571abf6086dd9059d8a81e8c6b688a9bf3cce88b6
5
5
  SHA512:
6
- metadata.gz: a402e020ba4a665965fe3be91f3f8ab4dcff20c27851219f827e8217cb8eaf1ca6e37fe42bc39c9988e76c7fdc291d319a9bbdd7835daa1761304535870a6ac6
7
- data.tar.gz: 8e451b8e41fcfaa998427d51b8551ab486612c1c15b555ed6c0c3ea21d7c8df993db2fd51049876af6ab930da668d9d79bc001418e53f9ae2747b00c9129906d
6
+ metadata.gz: 70c5318b5c1b4859715e778246a42571c4a3d97895719b688093189eac1a57b40814494a93f4b24a7e1663b39e4892fd53098e45ca9f9972079fcbf02c06faae
7
+ data.tar.gz: ca52430fd386ee9cd88c94c0acd3c3336f0323392ed44506bb5ac89d566d6093c8c269ad4fef356dfaaa2ee77fdd16e939d22fb6ca25546a5455a01eea3dde27
@@ -6,6 +6,9 @@ module Mimi
6
6
  include Mimi::Core::Module
7
7
  include Mimi::Logger::Instance
8
8
 
9
+ # key in the message headers that is used to pass context id
10
+ CONTEXT_ID_KEY = 'c'.freeze
11
+
9
12
  default_options(
10
13
  require_files: 'app/messaging/**/*.rb',
11
14
  mq_host: 'localhost',
@@ -17,8 +17,12 @@ module Mimi
17
17
  end
18
18
 
19
19
  def self.get(name, data = {}, opts = {})
20
- d, m, response = Mimi::Messaging.get(
21
- queue_name, encode(data), opts.deep_merge(headers: { method_name: name.to_s })
20
+ headers = {
21
+ method_name: name.to_s,
22
+ Mimi::Messaging::CONTEXT_ID_KEY => Mimi::Messaging.logger.context_id
23
+ }
24
+ _d, _m, response = Mimi::Messaging.get(
25
+ queue_name, encode(data), opts.deep_merge(headers: headers)
22
26
  )
23
27
  raise Timeout::Error unless response
24
28
  message = new(decode(response))
@@ -27,8 +31,12 @@ module Mimi
27
31
  end
28
32
 
29
33
  def self.post(name, data = {}, opts = {})
34
+ headers = {
35
+ method_name: name.to_s,
36
+ Mimi::Messaging::CONTEXT_ID_KEY => Mimi::Messaging.logger.context_id
37
+ }
30
38
  Mimi::Messaging.post(
31
- queue_name, encode(data), opts.deep_merge(headers: { method_name: name.to_s })
39
+ queue_name, encode(data), opts.deep_merge(headers: headers)
32
40
  )
33
41
  end
34
42
 
@@ -14,8 +14,12 @@ module Mimi
14
14
  end
15
15
 
16
16
  def self.broadcast(name, data = {}, opts = {})
17
+ headers = {
18
+ method_name: name.to_s,
19
+ Mimi::Messaging::CONTEXT_ID_KEY => Mimi::Messaging.logger.context_id
20
+ }
17
21
  Mimi::Messaging.broadcast(
18
- notification_name, Message.encode(data), opts.merge(headers: { method_name: name.to_s })
22
+ notification_name, Message.encode(data), opts.merge(headers: headers)
19
23
  )
20
24
  end
21
25
 
@@ -77,6 +77,7 @@ module Mimi
77
77
  end
78
78
 
79
79
  def initialize(d, m, p)
80
+ initialize_logging_context!(m.headers)
80
81
  @request = Mimi::Messaging::Request.new(self.class, d, m, p)
81
82
  @result = nil
82
83
  method_name = request.method_name
@@ -89,6 +90,18 @@ module Mimi
89
90
  end
90
91
  end
91
92
 
93
+ # Initializes logging context.
94
+ #
95
+ # Starts a new logging contenxt or inherits a context id from the message headers.
96
+ #
97
+ # @param headers [Hash,nil] message headers
98
+ #
99
+ def initialize_logging_context!(headers)
100
+ context_id = (headers || {})[Mimi::Messaging::CONTEXT_ID_KEY]
101
+ return logger.new_context! unless context_id
102
+ logger.context_id = context_id
103
+ end
104
+
92
105
  # Request logger
93
106
  #
94
107
  # Usage:
@@ -1,5 +1,5 @@
1
1
  module Mimi
2
2
  module Messaging
3
- VERSION = '0.1.6'.freeze
3
+ VERSION = '0.1.7'.freeze
4
4
  end
5
5
  end
@@ -11,33 +11,92 @@ def message_class_for(queue_name)
11
11
  message_class
12
12
  end
13
13
 
14
- def get(queue_and_method_name, params = {})
14
+ def get_timeout(value = nil)
15
+ return @get_current_timeout = value if value
16
+ help_get_timeout
17
+ puts
18
+ puts "Curret GET timeout is set at #{get_current_timeout}s"
19
+ end
20
+
21
+ def help_get_timeout
22
+ puts 'Usage: get_timeout <seconds>'
23
+ puts
24
+ puts 'Sets a timeout for GET requests.'
25
+ puts
26
+ puts 'Example:'
27
+ puts " > get_timeout 15"
28
+ end
29
+
30
+ def get_current_timeout
31
+ @get_current_timeout || 5 # seconds
32
+ end
33
+
34
+ def get(queue_and_method_name = nil, params = {})
35
+ return help_get unless queue_and_method_name
15
36
  queue_name, method_name = queue_and_method_name.split('/')
16
37
  message_class = message_class_for(queue_name)
17
38
  puts "GET #{queue_name}/#{method_name}: #{params}"
18
39
  ts = Time.now
19
- result = message_class.get(method_name, params)
40
+ result = message_class.get(method_name, params, timeout: get_current_timeout)
20
41
  puts 'Completed in %.1fms' % ((Time.now - ts) * 1000.0)
21
42
  result
22
43
  end
23
44
 
24
- def post(queue_and_method_name, params = {})
45
+ def help_get
46
+ puts "Usage: get '<queue_name>/<method_name>' [, params]"
47
+ puts
48
+ puts 'Issues a GET request to a given queue/method and sends the message'
49
+ puts "constructed from params Hash. Awaits for a response for at most 'get_timeout' seconds."
50
+ puts
51
+ puts 'Example:'
52
+ puts " > get 'accounts/list', page: 2"
53
+ end
54
+
55
+ def post(queue_and_method_name = nil, params = {})
56
+ return help_post unless queue_and_method_name
25
57
  queue_name, method_name = queue_and_method_name.split('/')
26
58
  message_class = message_class_for(queue_name)
27
59
  puts "POST #{queue_name}/#{method_name}: #{params}"
28
60
  message_class.post(method_name, params)
61
+ nil
62
+ end
63
+
64
+ def help_post
65
+ puts "Usage: post '<queue_name>/<method_name>' [, params]"
66
+ puts
67
+ puts 'Issues a POST request to a given queue/method and sends the message'
68
+ puts 'constructed from params Hash. Returns immediately'
69
+ puts
70
+ puts 'Example:'
71
+ puts " > post 'accounts/create', name: 'Primary account', currency: 'BTC'"
29
72
  end
30
73
 
31
- def listen(notification_name)
74
+ def listen(notification_name = nil, *method_names)
75
+ return help_listen unless notification_name
32
76
  listener_class = Class.new(Mimi::Messaging::Listener)
33
77
  listener_class.notification(notification_name)
34
- listener_class.before do
35
- logger.info "** #{request.canonical_name}: #{params}"
78
+ method_names.each do |method_name|
79
+ listener_class.send :define_method, method_name.to_sym do
80
+ puts "LISTEN #{listener_class.resource_name}/#{request.method_name}: #{params}"
81
+ end
82
+ puts "Listener for '#{notification_name}/#{method_name}' registered"
36
83
  end
37
- puts "Listener for '#{notification_name}' installed"
84
+ listener_class.start
85
+ puts "Listener for '#{notification_name}' started"
86
+ nil
38
87
  end
39
88
 
40
- def broadcast(notification_and_method_name, params = {})
89
+ def help_listen
90
+ puts "Usage: listen '<notification_name>' [, <method_name> ...]"
91
+ puts
92
+ puts 'Sets up a listener for notifications specified by notification and method names.'
93
+ puts
94
+ puts 'Example:'
95
+ puts " > listen 'accounts', :created, :updated"
96
+ end
97
+
98
+ def broadcast(notification_and_method_name = nil, params = {})
99
+ return help_broadcast unless notification_and_method_name
41
100
  notification_name, method_name = notification_and_method_name.split('/')
42
101
  notification_class = Class.new(Mimi::Messaging::Notification)
43
102
  notification_class.notification(notification_name)
@@ -45,3 +104,13 @@ def broadcast(notification_and_method_name, params = {})
45
104
  notification_class.broadcast(method_name, params)
46
105
  nil
47
106
  end
107
+
108
+ def help_broadcast
109
+ puts "Usage: broadcast '<notification_name>/<method_name>' [, params]"
110
+ puts
111
+ puts 'Broadcasts a notification message with a given name and method constructed'
112
+ puts 'from params Hash.'
113
+ puts
114
+ puts 'Example:'
115
+ puts " > broadcast 'accounts/updated', id: 213, name: 'Secondary account', currency: 'BCH'"
116
+ end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  if spec.respond_to?(:metadata)
20
20
  spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
23
  end
24
24
 
25
25
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -27,13 +27,13 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
29
 
30
- spec.add_dependency 'mimi-core', '~> 0.1'
31
- spec.add_dependency 'mimi-logger', '~> 0.1'
32
30
  spec.add_dependency 'bunny', '~> 2.9'
31
+ spec.add_dependency 'mimi-core', '~> 0.1'
32
+ spec.add_dependency 'mimi-logger', '~> 0.2', '>= 0.2.3'
33
33
  spec.add_dependency 'msgpack', '~> 1.2'
34
34
 
35
35
  spec.add_development_dependency 'bundler', '~> 1.11'
36
+ spec.add_development_dependency 'pry', '~> 0.10'
36
37
  spec.add_development_dependency 'rake', '~> 10.0'
37
38
  spec.add_development_dependency 'rspec', '~> 3.0'
38
- spec.add_development_dependency 'pry', '~> 0.10'
39
39
  end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mimi-messaging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: mimi-core
14
+ name: bunny
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '2.9'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '2.9'
27
27
  - !ruby/object:Gem::Dependency
28
- name: mimi-logger
28
+ name: mimi-core
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
@@ -39,19 +39,25 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bunny
42
+ name: mimi-logger
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2.9'
47
+ version: '0.2'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 0.2.3
48
51
  type: :runtime
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
51
54
  requirements:
52
55
  - - "~>"
53
56
  - !ruby/object:Gem::Version
54
- version: '2.9'
57
+ version: '0.2'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 0.2.3
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: msgpack
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -81,47 +87,47 @@ dependencies:
81
87
  - !ruby/object:Gem::Version
82
88
  version: '1.11'
83
89
  - !ruby/object:Gem::Dependency
84
- name: rake
90
+ name: pry
85
91
  requirement: !ruby/object:Gem::Requirement
86
92
  requirements:
87
93
  - - "~>"
88
94
  - !ruby/object:Gem::Version
89
- version: '10.0'
95
+ version: '0.10'
90
96
  type: :development
91
97
  prerelease: false
92
98
  version_requirements: !ruby/object:Gem::Requirement
93
99
  requirements:
94
100
  - - "~>"
95
101
  - !ruby/object:Gem::Version
96
- version: '10.0'
102
+ version: '0.10'
97
103
  - !ruby/object:Gem::Dependency
98
- name: rspec
104
+ name: rake
99
105
  requirement: !ruby/object:Gem::Requirement
100
106
  requirements:
101
107
  - - "~>"
102
108
  - !ruby/object:Gem::Version
103
- version: '3.0'
109
+ version: '10.0'
104
110
  type: :development
105
111
  prerelease: false
106
112
  version_requirements: !ruby/object:Gem::Requirement
107
113
  requirements:
108
114
  - - "~>"
109
115
  - !ruby/object:Gem::Version
110
- version: '3.0'
116
+ version: '10.0'
111
117
  - !ruby/object:Gem::Dependency
112
- name: pry
118
+ name: rspec
113
119
  requirement: !ruby/object:Gem::Requirement
114
120
  requirements:
115
121
  - - "~>"
116
122
  - !ruby/object:Gem::Version
117
- version: '0.10'
123
+ version: '3.0'
118
124
  type: :development
119
125
  prerelease: false
120
126
  version_requirements: !ruby/object:Gem::Requirement
121
127
  requirements:
122
128
  - - "~>"
123
129
  - !ruby/object:Gem::Version
124
- version: '0.10'
130
+ version: '3.0'
125
131
  description: Communications via RabbitMQ for mimi
126
132
  email:
127
133
  - alex@kukushk.in
@@ -179,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
185
  version: '0'
180
186
  requirements: []
181
187
  rubyforge_project:
182
- rubygems_version: 2.5.1
188
+ rubygems_version: 2.7.6
183
189
  signing_key:
184
190
  specification_version: 4
185
191
  summary: Communications via RabbitMQ for mimi