philotic 0.5.0 → 0.6.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: b72b8c6dc5ab1be2da45998cf70ebe908ad3099d
4
- data.tar.gz: 8b6473eb588bdbe69d4b401b4b149cdcecb416b9
3
+ metadata.gz: 3ed9395d1fe81863b4c38f92ab7caa95186d7731
4
+ data.tar.gz: d550b106f564a798635798e121dcd80d192d9e10
5
5
  SHA512:
6
- metadata.gz: 5a95fc1ea9e1729aff8752d68b50168e41d8ed621271c2a00809ec3e714ef68813defcb137febb57b4978df085a22da3b1a39e67b38e878f2de99d41b72e52eb
7
- data.tar.gz: 96fb09160928634ec14693512aadd19c99237993d7e608bcf850ced33006a75ca8216ed60da6e6571e2c29b2a6e3c9363b649979578f8584e082b4a2c1358596
6
+ metadata.gz: 160ebead89967ffdcd676867166573334df6d8b7a01fe08fa061043a0cb7b23b31a8ac23d2ad9c6cff2876871b0b039ed10f4ab00dab48e8451ce4756a1a0d94
7
+ data.tar.gz: 8e40399b956a627bc93ccd1e7bca8e0408efe25e9cba930ba2833139c8a299d6e5fe95ec206bbbd2ca3f8867f125c19688aeeba06399ad8b5bd4887be6d29b81
data/README.md CHANGED
@@ -19,7 +19,7 @@ Philotic.config.message_return_handler = lambda do |basic_return, metadata, mess
19
19
  Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
20
20
  end
21
21
 
22
- Philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
22
+ Philotic.subscribe(header_key: 'header_1') do |metadata, message|
23
23
  ap message[:attributes]
24
24
  end
25
25
 
@@ -48,7 +48,7 @@ philotic.config.message_return_handler = lambda do |basic_return, metadata, mess
48
48
  philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
49
49
  end
50
50
 
51
- philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
51
+ philotic.subscribe(header_key: 'header_1') do |metadata, message|
52
52
  ap message[:attributes]
53
53
  end
54
54
 
@@ -12,7 +12,7 @@ philotic.config.message_return_handler = lambda do |basic_return, metadata, mess
12
12
  philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
13
13
  end
14
14
 
15
- philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
15
+ philotic.subscribe(header_key: 'header_1') do |metadata, message|
16
16
  ap message[:attributes]
17
17
  end
18
18
 
@@ -10,7 +10,7 @@ Philotic.config.message_return_handler = lambda do |basic_return, metadata, mess
10
10
  Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
11
11
  end
12
12
 
13
- Philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
13
+ Philotic.subscribe(header_key: 'header_1') do |metadata, message|
14
14
  ap message[:attributes]
15
15
  end
16
16
 
@@ -6,20 +6,20 @@ require 'philotic'
6
6
  require 'awesome_print'
7
7
 
8
8
  # sometimes ack
9
- Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
9
+ Philotic.subscribe('flaky_queue', ack: true) do |metadata, message|
10
10
  ap message[:attributes]
11
- [true, false].sample ? Philotic.subscriber.acknowledge(message) : Philotic.subscriber.reject(message)
11
+ [true, false].sample ? acknowledge(message) : reject(message)
12
12
  end
13
13
 
14
14
  # always ack
15
- Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
15
+ Philotic.subscribe('flaky_queue', ack: true) do |metadata, message|
16
16
  ap message[:attributes]
17
- Philotic.subscriber.acknowledge(message, true)
17
+ acknowledge(message, true)
18
18
  end
19
19
 
20
20
  # always reject
21
- Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
21
+ Philotic.subscribe('flaky_queue', ack: true) do |metadata, message|
22
22
  ap message[:attributes]
23
- Philotic.subscriber.reject message
23
+ reject message
24
24
  end
25
25
  Philotic.subscriber.endure
@@ -7,7 +7,7 @@ require 'awesome_print'
7
7
 
8
8
  Philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'Philotic.yml.example'))
9
9
 
10
- Philotic.subscriber.subscribe(philotic_firehose: true) do |metadata, message|
10
+ Philotic.subscribe(philotic_firehose: true) do |metadata, message|
11
11
  ap message[:attributes]
12
12
  end
13
13
 
@@ -7,11 +7,11 @@ require 'awesome_print'
7
7
 
8
8
  Philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'Philotic.yml.example'))
9
9
 
10
- Philotic.subscriber.subscribe('female_queue') do |metadata, message|
10
+ Philotic.subscribe('female_queue') do |metadata, message|
11
11
  ap message[:attributes]
12
12
  end
13
13
 
14
- Philotic.subscriber.subscribe('male_queue') do |metadata, message|
14
+ Philotic.subscribe('male_queue') do |metadata, message|
15
15
  ap message[:attributes]
16
16
  end
17
17
 
@@ -5,7 +5,7 @@ $stdout.sync = true
5
5
  require 'philotic'
6
6
  require 'awesome_print'
7
7
 
8
- Philotic.subscriber.subscribe('test_queue') do |metadata, message|
8
+ Philotic.subscribe('test_queue') do |metadata, message|
9
9
  ap message[:attributes]
10
10
  end
11
11
 
@@ -39,6 +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
43
 
43
44
  attr_accessor :connection
44
45
 
@@ -77,6 +78,11 @@ module Philotic
77
78
  def log_level
78
79
  @log_level ||= defaults[:log_level].to_i
79
80
  end
81
+
82
+ def connection_retries
83
+ @connection_retries ||= defaults[:connection_retries].to_i
84
+ end
85
+
80
86
  attr_writer :connection_failed_handler, :connection_loss_handler, :message_return_handler
81
87
 
82
88
  def connection_failed_handler
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
  require 'bunny'
3
3
  require 'logger'
4
-
4
+ require 'bunny'
5
5
  require 'philotic/constants'
6
6
  require 'philotic/config'
7
7
  require 'philotic/publisher'
@@ -9,13 +9,20 @@ require 'philotic/subscriber'
9
9
 
10
10
  module Philotic
11
11
  class Connection
12
+
13
+ class TCPConnectionFailed < Bunny::TCPConnectionFailed; end
14
+
12
15
  extend Forwardable
13
16
 
14
- attr_reader :connection
17
+ attr_reader :connection, :connection_attempts
15
18
  attr_accessor :logger
16
19
 
17
20
  attr_writer :publisher, :subscriber
18
21
 
22
+ def initialize
23
+ @connection_attempts = 0
24
+ end
25
+
19
26
  def publisher
20
27
  @publisher ||= Philotic::Publisher.new self
21
28
  end
@@ -44,7 +51,20 @@ module Philotic
44
51
  end
45
52
 
46
53
  def start_connection!
47
- @connection = Bunny.new(config.rabbit_url, connection_settings)
54
+ begin
55
+ attempt_connection
56
+ rescue ::Bunny::TCPConnectionFailed => e
57
+ if connection_attempts <= config.connection_retries
58
+ retry
59
+ else
60
+ raise TCPConnectionFailed.new "Failed to connect after #{connection_attempts} attempts", config.rabbit_host, config.rabbit_port
61
+ end
62
+ end
63
+ end
64
+
65
+ def attempt_connection
66
+ @connection_attempts += 1
67
+ @connection = Bunny.new(config.rabbit_url, connection_settings)
48
68
  @connection.start
49
69
  end
50
70
 
@@ -130,7 +150,7 @@ module Philotic
130
150
 
131
151
  def logger
132
152
  unless @logger
133
- @logger = Logger.new(STDOUT)
153
+ @logger = Logger.new(STDOUT)
134
154
  @logger.level = config.log_level
135
155
  end
136
156
  @logger
@@ -6,7 +6,8 @@ module Philotic
6
6
  class Event
7
7
  include Philotic::Routable
8
8
 
9
- attr_accessor :connection
9
+ attr_accessor :connection, :publish_error
10
+ attr_writer :published
10
11
 
11
12
  def initialize(routables={}, payloads={}, connection=nil)
12
13
  self.timestamp = Time.now.to_i
@@ -19,6 +20,8 @@ module Philotic
19
20
  # over into the bus
20
21
  _set_routables_or_payloads(:routable, routables)
21
22
  _set_routables_or_payloads(:payload, payloads)
23
+
24
+ @published = false
22
25
  end
23
26
 
24
27
  def self.inherited(sub)
@@ -48,6 +51,10 @@ module Philotic
48
51
  @connection ||= Philotic.connection
49
52
  end
50
53
 
54
+ def published?
55
+ !!@published
56
+ end
57
+
51
58
  def publish
52
59
  connection.publish self
53
60
  end
@@ -21,19 +21,25 @@ module Philotic
21
21
  def publish(event)
22
22
  message_metadata = {headers: event.headers}
23
23
  message_metadata.merge!(event.message_metadata) if event.message_metadata
24
- _publish(event.payload, message_metadata)
24
+ begin
25
+ event.published = _publish(event.payload, message_metadata)
26
+ rescue => e
27
+ event.publish_error = e
28
+ logger.error e.message
29
+ end
30
+ event
25
31
  end
26
32
 
27
33
  private
28
34
  def _publish(payload, message_metadata = {})
29
35
  if config.disable_publish
30
- log_event_published(:warn, message_metadata, payload, 'attempted to publish a message when publishing is disabled.')
36
+ log_event_published(:warn, message_metadata, payload, 'attempted to publish a message when publishing is disabled.')
31
37
  return false
32
38
  end
33
39
  connection.connect!
34
40
  unless connection.connected?
35
41
  log_event_published(:error, message_metadata, payload, 'unable to publish event, not connected to RabbitMQ')
36
- return
42
+ return false
37
43
  end
38
44
  message_metadata = merge_metadata(message_metadata)
39
45
 
@@ -41,6 +47,7 @@ module Philotic
41
47
 
42
48
  connection.exchange.publish(payload.to_json, message_metadata)
43
49
  log_event_published(:debug, message_metadata, payload, 'published event')
50
+ true
44
51
  end
45
52
 
46
53
  def normalize_payload_times(payload)
@@ -20,7 +20,7 @@ module Philotic
20
20
  connection.logger
21
21
  end
22
22
 
23
- def subscription_callback
23
+ def subscription_callback(&block)
24
24
  lambda do |delivery_info, metadata, payload|
25
25
  hash_payload = JSON.parse payload
26
26
 
@@ -30,7 +30,8 @@ module Philotic
30
30
  delivery_info: delivery_info,
31
31
  attributes: metadata[:headers] ? hash_payload.merge(metadata[:headers]) : hash_payload
32
32
  }
33
- yield(Metadata.new(metadata), event)
33
+
34
+ instance_exec(Metadata.new(metadata), event, &block)
34
35
  end
35
36
  end
36
37
 
@@ -1,3 +1,3 @@
1
1
  module Philotic
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -43,6 +43,7 @@ describe Philotic::Connection do
43
43
 
44
44
  describe '#start_connection!' do
45
45
  let(:connection) { double }
46
+ let(:connection_error) {Bunny::TCPConnectionFailed.new 'connection failed', 'localhost', '5672'}
46
47
  subject { Philotic::Connection.new }
47
48
  specify do
48
49
  expect(Bunny).to receive(:new).with(subject.config.rabbit_url, subject.connection_settings).and_return(connection)
@@ -50,6 +51,15 @@ describe Philotic::Connection do
50
51
 
51
52
  subject.start_connection!
52
53
  end
54
+
55
+ it 'should retry connecting' do
56
+ expect(Bunny).to receive(:new) do
57
+ raise connection_error
58
+ end.exactly(subject.config.connection_retries + 1).times
59
+
60
+ expect { subject.start_connection! }.to raise_error(Philotic::Connection::TCPConnectionFailed)
61
+ end
62
+
53
63
  end
54
64
 
55
65
  describe '#close' do
@@ -17,6 +17,7 @@ describe Philotic::Publisher do
17
17
  subject { publisher }
18
18
 
19
19
  describe 'publish' do
20
+ let(:publish_error) {StandardError.new 'publish error'}
20
21
  it 'should call _publish with the right values' do
21
22
  Timecop.freeze
22
23
  expect(subject).to receive(:_publish).with(
@@ -36,7 +37,22 @@ describe Philotic::Publisher do
36
37
  timestamp: Time.now.to_i
37
38
  }
38
39
  )
40
+ expect(event).to_not be_published
39
41
  subject.publish(event)
42
+ expect(event).to_not be_published # not connected
43
+ end
44
+
45
+ it 'should fail gracefully' do
46
+ expect(subject).to receive(:_publish) do
47
+ raise publish_error
48
+ end
49
+
50
+ expect(subject.logger).to receive(:error).with(publish_error.message)
51
+ expect(event).to_not be_published
52
+ subject.publish(event)
53
+ expect(event).to_not be_published
54
+ expect(event.publish_error).to eq publish_error
55
+
40
56
  end
41
57
 
42
58
  end
@@ -83,7 +99,9 @@ describe Philotic::Publisher do
83
99
  }.to_json,
84
100
  metadata
85
101
  )
102
+ expect(event).to_not be_published
86
103
  subject.publish(event)
104
+ expect(event).to be_published
87
105
  end
88
106
 
89
107
  it 'should log an error when there is no connection' do
@@ -93,7 +111,9 @@ describe Philotic::Publisher do
93
111
  expect(subject.connection).to receive(:connected?).once.and_return(false)
94
112
 
95
113
  expect(subject.logger).to receive(:error)
114
+ expect(event).to_not be_published
96
115
  subject.publish(event)
116
+ expect(event).to_not be_published
97
117
  end
98
118
 
99
119
  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.5.0
4
+ version: 0.6.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-18 00:00:00 Z
12
+ date: 2014-12-24 00:00:00 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: codeclimate-test-reporter