philotic 0.6.0 → 0.7.0

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
2
  SHA1:
3
- metadata.gz: 3ed9395d1fe81863b4c38f92ab7caa95186d7731
4
- data.tar.gz: d550b106f564a798635798e121dcd80d192d9e10
3
+ metadata.gz: 0f73c756665ad98e8a1387acdda9141a8e84e95c
4
+ data.tar.gz: cc3615e57182313cbc80acc792be53cc7bfbdefb
5
5
  SHA512:
6
- metadata.gz: 160ebead89967ffdcd676867166573334df6d8b7a01fe08fa061043a0cb7b23b31a8ac23d2ad9c6cff2876871b0b039ed10f4ab00dab48e8451ce4756a1a0d94
7
- data.tar.gz: 8e40399b956a627bc93ccd1e7bca8e0408efe25e9cba930ba2833139c8a299d6e5fe95ec206bbbd2ca3f8867f125c19688aeeba06399ad8b5bd4887be6d29b81
6
+ metadata.gz: 2e1d40caf53520703dcb80ea912f1fb5c2ebbc1e70496aa2c938273e566fb9dece69a0c00cf0729c483140014fb70b848d56df6e2d98dd142478dbeea7c48853
7
+ data.tar.gz: 8b2ef1f4b04f2d4b6cbce4b8f14c4877504c19574af33da3ab006f9a0e8df7c041af78271efd235cf97c18da9095ad4c8ff433b9144c32c8ee7200132d14b01c
data/README.md CHANGED
@@ -16,7 +16,7 @@ require 'awesome_print'
16
16
 
17
17
  # override the message return handler
18
18
  Philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
19
- Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
19
+ Philotic.logger.warn { "Message returned. reply_text: #{basic_return.reply_text}" }
20
20
  end
21
21
 
22
22
  Philotic.subscribe(header_key: 'header_1') do |metadata, message|
@@ -45,7 +45,7 @@ philotic = Philotic::Connection.new
45
45
 
46
46
  # override the message return handler
47
47
  philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
48
- philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
48
+ philotic.logger.warn { "Message returned. reply_text: #{basic_return.reply_text}" }
49
49
  end
50
50
 
51
51
  philotic.subscribe(header_key: 'header_1') do |metadata, message|
@@ -9,7 +9,7 @@ philotic = Philotic::Connection.new
9
9
 
10
10
  # override the message return handler
11
11
  philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
12
- philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
12
+ philotic.logger.warn { "Message returned. reply_text: #{basic_return.reply_text}" }
13
13
  end
14
14
 
15
15
  philotic.subscribe(header_key: 'header_1') do |metadata, message|
@@ -7,7 +7,7 @@ require 'awesome_print'
7
7
 
8
8
  # override the message return handler
9
9
  Philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
10
- Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
10
+ Philotic.logger.warn { "Message returned. reply_text: #{basic_return.reply_text}" }
11
11
  end
12
12
 
13
13
  Philotic.subscribe(header_key: 'header_1') do |metadata, message|
@@ -39,7 +39,7 @@ module Philotic
39
39
  DEFAULT_APP_ID = nil
40
40
  DEFAULT_TIMESTAMP = nil
41
41
  DEFAULT_EXPIRATION = nil
42
- DEFAULT_CONNECTION_RETRIES = 2
42
+ DEFAULT_CONNECTION_ATTEMPTS = 3
43
43
 
44
44
  attr_accessor :connection
45
45
 
@@ -79,27 +79,28 @@ module Philotic
79
79
  @log_level ||= defaults[:log_level].to_i
80
80
  end
81
81
 
82
- def connection_retries
83
- @connection_retries ||= defaults[:connection_retries].to_i
82
+ def connection_attempts
83
+ @connection_retries ||= defaults[:connection_attempts].to_i
84
84
  end
85
85
 
86
86
  attr_writer :connection_failed_handler, :connection_loss_handler, :message_return_handler
87
87
 
88
88
  def connection_failed_handler
89
89
  @connection_failed_handler ||= lambda do |settings|
90
- logger.error "RabbitMQ connection failure; host:#{rabbit_host}"
90
+ logger.error { "RabbitMQ connection failure; host:#{rabbit_host}" }
91
91
  end
92
92
  end
93
93
 
94
94
  def connection_loss_handler
95
95
  @connection_loss_handler ||= lambda do |conn, settings|
96
- logger.warn "RabbitMQ connection loss; host:#{rabbit_host}"; conn.reconnect(false, 2)
96
+ logger.warn { "RabbitMQ connection loss; host:#{rabbit_host}" }
97
+ conn.reconnect(false, 2)
97
98
  end
98
99
  end
99
100
 
100
101
  def message_return_handler
101
102
  @message_return_handler ||= lambda do |basic_return, metadata, payload|
102
- logger.warn "Philotic message #{JSON.parse payload} was returned! reply_code = #{basic_return.reply_code}, reply_text = #{basic_return.reply_text} headers = #{metadata[:headers]}"
103
+ logger.warn { "Philotic message #{JSON.parse payload} was returned! reply_code = #{basic_return.reply_code}, reply_text = #{basic_return.reply_text} headers = #{metadata[:headers]}" }
103
104
  end
104
105
  end
105
106
 
@@ -124,6 +125,11 @@ module Philotic
124
125
  "#{rabbit_scheme}://#{rabbit_user}:#{rabbit_password}@#{rabbit_host}:#{rabbit_port}/#{CGI.escape rabbit_vhost}"
125
126
  end
126
127
 
128
+ def sanitized_rabbit_url
129
+ parse_rabbit_uri
130
+ rabbit_url.gsub("#{rabbit_user}:#{rabbit_password}", '[USER_REDACTED]:[PASSWORD_REDACTED]')
131
+ end
132
+
127
133
  def load_config(config)
128
134
  config.each do |k, v|
129
135
  mutator = "#{k}="
@@ -10,7 +10,13 @@ require 'philotic/subscriber'
10
10
  module Philotic
11
11
  class Connection
12
12
 
13
- class TCPConnectionFailed < Bunny::TCPConnectionFailed; end
13
+ class TCPConnectionFailed < StandardError
14
+ attr_reader :url
15
+
16
+ def initialize(message, url)
17
+ super("Could not establish TCP connection to #{url}: #{message}")
18
+ end
19
+ end
14
20
 
15
21
  extend Forwardable
16
22
 
@@ -41,11 +47,11 @@ module Philotic
41
47
  start_connection!
42
48
 
43
49
  if connected?
44
- logger.info "connected to RabbitMQ: #{config.rabbit_host}:#{config.rabbit_port}"
50
+ logger.info { "Connected to RabbitMQ: #{config.sanitized_rabbit_url}" }
45
51
  set_exchange_return_handler!
46
52
  true
47
53
  else
48
- logger.error "failed connected to RabbitMQ; host:#{config.rabbit_host}"
54
+ logger.error { "Failed to connect to RabbitMQ: #{config.sanitized_rabbit_url}" }
49
55
  false
50
56
  end
51
57
  end
@@ -54,18 +60,23 @@ module Philotic
54
60
  begin
55
61
  attempt_connection
56
62
  rescue ::Bunny::TCPConnectionFailed => e
57
- if connection_attempts <= config.connection_retries
63
+ if connection_attempts < config.connection_attempts
58
64
  retry
59
65
  else
60
- raise TCPConnectionFailed.new "Failed to connect after #{connection_attempts} attempts", config.rabbit_host, config.rabbit_port
66
+ attempts = connection_attempts
67
+ @connection_attempts = 0
68
+ raise TCPConnectionFailed.new "Failed to connect to RabbitMQ server after #{attempts} attempts", config.sanitized_rabbit_url
61
69
  end
62
70
  end
63
71
  end
64
72
 
65
73
  def attempt_connection
66
74
  @connection_attempts += 1
67
- @connection = Bunny.new(config.rabbit_url, connection_settings)
75
+ logger.warn { "Connecting to RabbitMQ: #{config.sanitized_rabbit_url}. Attempt #{connection_attempts} of #{config.connection_attempts}" } if connection_attempts > 1
76
+
77
+ @connection = Bunny.new(config.rabbit_url, connection_settings)
68
78
  @connection.start
79
+ @connection_attempts = 0
69
80
  end
70
81
 
71
82
  def connection_settings
@@ -77,7 +88,7 @@ module Philotic
77
88
  end
78
89
 
79
90
  def close
80
- logger.info "closing connection to RabbitMQ; host:#{config.rabbit_host}"
91
+ logger.warn { "closing connection to RabbitMQ: #{config.sanitized_rabbit_url}" }
81
92
  connection.close if connected?
82
93
  @channel = nil
83
94
  @exchange = nil
@@ -112,7 +123,7 @@ module Philotic
112
123
 
113
124
  if should_delete_queue
114
125
  channel.queue(queue_name, passive: true).delete
115
- logger.info "deleted old queue. queue: #{queue_name}"
126
+ logger.info { "deleted old queue. queue: #{queue_name}" }
116
127
  end
117
128
 
118
129
  if should_create_queue
@@ -120,7 +131,7 @@ module Philotic
120
131
  queue = queue_from_config(queue_name, config)
121
132
  bind_queue(queue, config)
122
133
  else
123
- logger.warn "Queue #{queue_name} not created; it already exists. self.config.delete_existing_queues must be true to override."
134
+ logger.warn { "Queue #{queue_name} not created; it already exists. self.config.delete_existing_queues must be true to override." }
124
135
  end
125
136
  end
126
137
 
@@ -129,10 +140,10 @@ module Philotic
129
140
  bindings = config[:bindings]
130
141
  bindings.each do |arguments|
131
142
  queue.bind(queue_exchange, {arguments: arguments})
132
- logger.info "Added binding to queue. queue: #{queue.name} binding: #{arguments}"
143
+ logger.info { "Added binding to queue. queue: #{queue.name} binding: #{arguments}" }
133
144
  end
134
145
 
135
- logger.info "Finished adding bindings to queue. queue: #{queue.name}"
146
+ logger.info { "Finished adding bindings to queue. queue: #{queue.name}" }
136
147
  end
137
148
 
138
149
  def exchange_from_config(config)
@@ -144,7 +155,7 @@ module Philotic
144
155
  queue_options.merge!(config[:options] || {})
145
156
 
146
157
  channel.queue(queue_name, queue_options).tap do
147
- logger.info "Created queue. queue:#{queue_name}"
158
+ logger.info { "Created queue. queue:#{queue_name}" }
148
159
  end
149
160
  end
150
161
 
@@ -30,6 +30,16 @@ module Philotic
30
30
  event
31
31
  end
32
32
 
33
+ def normalize_payload_times(payload)
34
+ payload.each do |k, v|
35
+ if v.respond_to?(:utc)
36
+ payload[k] = v.utc
37
+ elsif v.respond_to?(:to_utc)
38
+ payload[k] = v.to_utc
39
+ end
40
+ end
41
+ end
42
+
33
43
  private
34
44
  def _publish(payload, message_metadata = {})
35
45
  if config.disable_publish
@@ -50,16 +60,6 @@ module Philotic
50
60
  true
51
61
  end
52
62
 
53
- def normalize_payload_times(payload)
54
- payload.each do |k, v|
55
- if v.respond_to?(:utc)
56
- payload[k] = v.utc
57
- elsif v.respond_to?(:to_utc)
58
- payload[k] = v.to_utc
59
- end
60
- end
61
- end
62
-
63
63
  def merge_metadata(message_metadata)
64
64
  publish_defaults = {}
65
65
  Philotic::MESSAGE_OPTIONS.each do |key|
@@ -1,3 +1,3 @@
1
1
  module Philotic
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -55,7 +55,7 @@ describe Philotic::Connection do
55
55
  it 'should retry connecting' do
56
56
  expect(Bunny).to receive(:new) do
57
57
  raise connection_error
58
- end.exactly(subject.config.connection_retries + 1).times
58
+ end.exactly(subject.config.connection_attempts).times
59
59
 
60
60
  expect { subject.start_connection! }.to raise_error(Philotic::Connection::TCPConnectionFailed)
61
61
  end
@@ -147,7 +147,6 @@ describe Philotic::Connection do
147
147
  expect(queue).to receive(:delete)
148
148
  expect(channel).to receive(:queue).with(queue_name, queue_options).and_return(queue)
149
149
  expect(channel).to receive(:headers).with(config[:exchange], durable: true) { exchange }
150
- expect(queue).to receive(:name).and_return(queue_name).exactly(2).times
151
150
 
152
151
  config[:bindings].each do |arguments|
153
152
  expect(queue).to receive(:bind).with(exchange, {arguments: arguments})
@@ -30,6 +30,19 @@ module Philotic
30
30
 
31
31
  end
32
32
 
33
+ it 'should accept a block' do
34
+ expect(logger.connection).to receive(:publish) do |event|
35
+ expect(event.message).to eq message
36
+ expect(event.severity).to eq Logger::INFO
37
+
38
+ end
39
+
40
+ expect(device).to receive(:write) do |log_message|
41
+ expect(log_message).to match /#{message}/
42
+ end
43
+ logger.info { message }
44
+ end
45
+
33
46
  it "should not die if it can't log to RabbitMQ" do
34
47
  expect(logger.connection).to receive(:publish) do |event|
35
48
  raise error_message
@@ -43,7 +56,15 @@ module Philotic
43
56
  expect(log_message).to match /#{error_message}/
44
57
  end
45
58
  logger.info message
59
+ end
46
60
 
61
+ it 'should behave not log if the severity is too low' do
62
+
63
+ expect(logger.connection).not_to receive(:publish)
64
+
65
+ expect(device).not_to receive(:write)
66
+ logger.level = Logger::WARN
67
+ logger.info message
47
68
  end
48
69
  end
49
70
  end
@@ -16,8 +16,8 @@ describe Philotic::Publisher do
16
16
  let(:publisher) { Philotic::Connection.new.publisher }
17
17
  subject { publisher }
18
18
 
19
- describe 'publish' do
20
- let(:publish_error) {StandardError.new 'publish error'}
19
+ describe '#publish' do
20
+ let(:publish_error) { StandardError.new 'publish error' }
21
21
  it 'should call _publish with the right values' do
22
22
  Timecop.freeze
23
23
  expect(subject).to receive(:_publish).with(
@@ -55,9 +55,23 @@ describe Philotic::Publisher do
55
55
 
56
56
  end
57
57
 
58
+ context 'when publishing is disabled' do
59
+ before do
60
+ subject.config.disable_publish = true
61
+ end
62
+ it 'should log a warning' do
63
+ expect(subject).to receive(:log_event_published)
64
+
65
+ expect(event).to_not be_published
66
+ subject.publish(event)
67
+ expect(event).to_not be_published
68
+ end
69
+
70
+ end
71
+
58
72
  end
59
73
 
60
- describe 'raw_publish' do
74
+ describe '#raw_publish' do
61
75
 
62
76
  it 'should call exchange.publish with the right values' do
63
77
  Timecop.freeze
@@ -117,4 +131,28 @@ describe Philotic::Publisher do
117
131
  end
118
132
 
119
133
  end
134
+
135
+
136
+ describe '#normalize_payload_times' do
137
+ let(:responds_to_utc) { double }
138
+ let(:responds_to_to_utc) { double }
139
+ let (:payload) { {utc: responds_to_utc, to_utc: responds_to_to_utc} }
140
+ let(:normalized_utc) { 1 }
141
+ let(:normalized_to_utc) { 2 }
142
+ let (:normalized_payload) { {utc: normalized_utc, to_utc: normalized_to_utc} }
143
+ it 'should call .utc on values that respond to it' do
144
+ expect(responds_to_utc).to receive(:respond_to?).with(:utc).and_return(true)
145
+ expect(responds_to_utc).to receive(:utc).and_return(normalized_utc)
146
+
147
+ expect(responds_to_to_utc).to receive(:respond_to?).with(:utc).and_return(false)
148
+ expect(responds_to_to_utc).to receive(:respond_to?).with(:to_utc).and_return(true)
149
+ expect(responds_to_to_utc).to receive(:to_utc).and_return(normalized_to_utc)
150
+
151
+ subject.normalize_payload_times payload
152
+
153
+ expect(payload).to eq normalized_payload
154
+
155
+ end
156
+
157
+ end
120
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philotic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Keyes
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2014-12-24 00:00:00 Z
12
+ date: 2014-12-30 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: codeclimate-test-reporter