philotic 0.2.4 → 0.3.0
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 -7
- data/.travis.yml +2 -0
- data/README.md +8 -6
- data/examples/creating_named_queues/manually.rb +9 -6
- data/examples/creating_named_queues/with_rake.rb +2 -2
- data/examples/publishing/publish.rb +4 -2
- data/examples/simple_combined_example.rb +8 -6
- data/examples/subscribing/acks.rb +10 -7
- data/examples/subscribing/anonymous_queue.rb +5 -3
- data/examples/subscribing/multiple_named_queues.rb +6 -4
- data/examples/subscribing/named_queue.rb +3 -2
- data/lib/philotic.rb +4 -85
- data/lib/philotic/config.rb +27 -14
- data/lib/philotic/connection.rb +79 -8
- data/lib/philotic/logging/logger.rb +3 -1
- data/lib/philotic/publisher.rb +31 -9
- data/lib/philotic/routable.rb +0 -8
- data/lib/philotic/subscriber.rb +23 -13
- data/lib/philotic/tasks/init_queues.rb +8 -5
- data/lib/philotic/version.rb +1 -1
- data/spec/philotic/config_spec.rb +24 -21
- data/spec/philotic/connection_spec.rb +95 -13
- data/spec/philotic/logging/logger_spec.rb +13 -7
- data/spec/philotic/publisher_spec.rb +9 -14
- data/spec/philotic/routable_spec.rb +3 -8
- data/spec/philotic/subscriber_spec.rb +27 -20
- data/spec/philotic_spec.rb +4 -79
- data/spec/spec_helper.rb +2 -1
- metadata +180 -126
- data/examples/README.md +0 -13
- data/examples/creating_named_queues/philotic_named_queues.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
5
|
-
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dd9e68a219e5e261cff5c93874204ee23ac50eff
|
4
|
+
data.tar.gz: 76474a8e73d1306e885161c8639043b1ad80d15f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cd304f11017c7e646a0ce22d7bf620613cdb73a5fff3e0cfc2e78d2c63750dd7ed28ec8cf216f59f273eeda92a77881c94894d9a0e07edbee49c37958be336c
|
7
|
+
data.tar.gz: c7db1f0ac754a5e76c0266957443b13cc1e25e52f9ce2aec2fe47723465881262214ac67342ee8cc5aa18e4f330cd1ab6cb843ccaca92c44efdc5766439a36d4
|
data/.travis.yml
CHANGED
@@ -18,3 +18,5 @@ notifications:
|
|
18
18
|
hipchat:
|
19
19
|
rooms:
|
20
20
|
secure: akLY832/pt0+9Y2TFKpzW08bagPMY//AcQJ5JGzc0yd3hZbXTlkW4S8bMEclBeHBqLGCV6vaWeN90PiYwIBs4IWlxopPAop4BEWeqRvD8+B4fxarXYwejt54vH3v4iCnHhSNBpFJDbykyfL0f0g9fhvM48eHzhBXTxPZfpQ9nXs=
|
21
|
+
template:
|
22
|
+
- '%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}'
|
data/README.md
CHANGED
@@ -14,24 +14,26 @@ Check out the [examples](https://github.com/nkeyes/philotic/tree/master/examples
|
|
14
14
|
require 'philotic'
|
15
15
|
require 'awesome_print'
|
16
16
|
|
17
|
+
philotic = Philotic::Connection.new
|
18
|
+
|
17
19
|
# override the message return handler
|
18
|
-
|
19
|
-
|
20
|
+
philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
|
21
|
+
philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
|
20
22
|
end
|
21
23
|
|
22
|
-
|
24
|
+
philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
|
23
25
|
ap message[:attributes]
|
24
26
|
end
|
25
27
|
|
26
28
|
# normally we'd do:
|
27
29
|
#
|
28
|
-
#
|
30
|
+
# philotic.subscriber.endure
|
29
31
|
#
|
30
32
|
# to keep the parent thread alive while the subscribers do their thing
|
31
33
|
# but this infinite publish loop takes care of that
|
32
34
|
loop do
|
33
|
-
Philotic::Event.
|
34
|
-
|
35
|
+
event = Philotic::Event.new({header_key: "header_#{[1, 2].sample}"}, {payload_key: 'payload_value'})
|
36
|
+
philotic.publish event
|
35
37
|
# only send a message every two seconds so we can see whats going on
|
36
38
|
sleep 2
|
37
39
|
end
|
@@ -4,10 +4,13 @@ $stdout.sync = true
|
|
4
4
|
|
5
5
|
require 'philotic'
|
6
6
|
|
7
|
+
philotic = Philotic::Connection.new
|
8
|
+
|
7
9
|
# explicitly create named queues for this example
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
# philotic.config.initialize_named_queues must be truthy to run Philotic.initialize_named_queue!
|
11
|
+
philotic.config.initialize_named_queues = true
|
12
|
+
|
13
|
+
philotic.initialize_named_queue!('male_queue', bindings: [{:'x-match' => 'all', gender: :M, available: true}])
|
14
|
+
philotic.initialize_named_queue!('female_queue', bindings: [{:'x-match' => 'all', gender: :F, available: true}])
|
15
|
+
philotic.initialize_named_queue!('test_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
|
16
|
+
philotic.initialize_named_queue!('flaky_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
|
@@ -7,5 +7,5 @@ require 'rake'
|
|
7
7
|
|
8
8
|
require 'philotic/tasks'
|
9
9
|
# equivelant of:
|
10
|
-
# rake
|
11
|
-
Rake::Task[
|
10
|
+
# rake philotic:init_queues[examples/creating_named_queues/philotic_queues.yml]
|
11
|
+
Rake::Task['philotic:init_queues'].invoke(File.join(File.dirname(__FILE__), 'philotic_queues.yml'))
|
@@ -5,7 +5,9 @@ $stdout.sync = true
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'philotic/dummy_event'
|
7
7
|
|
8
|
-
|
8
|
+
@philotic = Philotic::Connection.new
|
9
|
+
|
10
|
+
@philotic.publisher.logger.level = Logger::WARN
|
9
11
|
|
10
12
|
@event = Philotic::DummyEvent.new
|
11
13
|
|
@@ -24,7 +26,7 @@ def send_message number
|
|
24
26
|
@event.gender = [:F, :M].sample
|
25
27
|
@event.message = "Message #{number}: Hey #{@event.gender == :M ? 'dude' : 'dudette'}"
|
26
28
|
|
27
|
-
@
|
29
|
+
@philotic.publish @event
|
28
30
|
|
29
31
|
end
|
30
32
|
|
@@ -5,24 +5,26 @@ $stdout.sync = true
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'awesome_print'
|
7
7
|
|
8
|
+
philotic = Philotic::Connection.new
|
9
|
+
|
8
10
|
# override the message return handler
|
9
|
-
|
10
|
-
|
11
|
+
philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
|
12
|
+
philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
|
14
16
|
ap message[:attributes]
|
15
17
|
end
|
16
18
|
|
17
19
|
# normally we'd do:
|
18
20
|
#
|
19
|
-
#
|
21
|
+
# philotic.subscriber.endure
|
20
22
|
#
|
21
23
|
# to keep the parent thread alive while the subscribers do their thing
|
22
24
|
# but this infinite publish loop takes care of that
|
23
25
|
loop do
|
24
|
-
Philotic::Event.
|
25
|
-
|
26
|
+
event = Philotic::Event.new({header_key: "header_#{[1, 2].sample}"}, {payload_key: 'payload_value'})
|
27
|
+
philotic.publish event
|
26
28
|
# only send a message every two seconds so we can see whats going on
|
27
29
|
sleep 2
|
28
30
|
end
|
@@ -5,21 +5,24 @@ $stdout.sync = true
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'awesome_print'
|
7
7
|
|
8
|
+
philotic = Philotic::Connection.new
|
9
|
+
|
10
|
+
|
8
11
|
# sometimes ack
|
9
|
-
|
12
|
+
philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
|
10
13
|
ap message[:attributes]
|
11
|
-
[true, false].sample ?
|
14
|
+
[true, false].sample ? philotic.subscriber.acknowledge(message) : philotic.subscriber.reject(message)
|
12
15
|
end
|
13
16
|
|
14
17
|
# always ack
|
15
|
-
|
18
|
+
philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
|
16
19
|
ap message[:attributes]
|
17
|
-
|
20
|
+
philotic.subscriber.acknowledge(message, true)
|
18
21
|
end
|
19
22
|
|
20
23
|
# always reject
|
21
|
-
|
24
|
+
philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
|
22
25
|
ap message[:attributes]
|
23
|
-
|
26
|
+
philotic.subscriber.reject message
|
24
27
|
end
|
25
|
-
|
28
|
+
philotic.subscriber.endure
|
@@ -5,11 +5,13 @@ $stdout.sync = true
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'awesome_print'
|
7
7
|
|
8
|
-
Philotic::
|
8
|
+
philotic = Philotic::Connection.new
|
9
9
|
|
10
|
+
philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'philotic.yml.example'))
|
10
11
|
|
11
|
-
|
12
|
+
|
13
|
+
philotic.subscriber.subscribe(philotic_firehose: true) do |metadata, message|
|
12
14
|
ap message[:attributes]
|
13
15
|
end
|
14
16
|
|
15
|
-
|
17
|
+
philotic.subscriber.endure
|
@@ -4,15 +4,17 @@ $stdout.sync = true
|
|
4
4
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'awesome_print'
|
7
|
+
philotic = Philotic::Connection.new
|
7
8
|
|
8
|
-
Philotic::Config.load_file(File.join(File.dirname(__FILE__), '../../', 'philotic.yml.example'))
|
9
9
|
|
10
|
-
|
10
|
+
philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'philotic.yml.example'))
|
11
|
+
|
12
|
+
philotic.subscriber.subscribe('female_queue') do |metadata, message|
|
11
13
|
ap message[:attributes]
|
12
14
|
end
|
13
15
|
|
14
|
-
|
16
|
+
philotic.subscriber.subscribe('male_queue') do |metadata, message|
|
15
17
|
ap message[:attributes]
|
16
18
|
end
|
17
19
|
|
18
|
-
|
20
|
+
philotic.subscriber.endure
|
@@ -5,9 +5,10 @@ $stdout.sync = true
|
|
5
5
|
require 'philotic'
|
6
6
|
require 'awesome_print'
|
7
7
|
|
8
|
+
philotic = Philotic::Connection.new
|
8
9
|
|
9
|
-
|
10
|
+
philotic.subscriber.subscribe('test_queue') do |metadata, message|
|
10
11
|
ap message[:attributes]
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
+
philotic.subscriber.endure
|
data/lib/philotic.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
require 'pathname'
|
3
|
-
require 'logger'
|
4
3
|
|
5
4
|
require 'philotic/constants'
|
5
|
+
require 'philotic/connection'
|
6
|
+
|
6
7
|
|
7
8
|
module Philotic
|
8
|
-
mattr_accessor :logger
|
9
|
-
mattr_accessor :log_event_handler
|
10
9
|
|
11
10
|
def self.root
|
12
11
|
::Pathname.new File.expand_path('../../', __FILE__)
|
@@ -16,92 +15,12 @@ module Philotic
|
|
16
15
|
ENV['SERVICE_ENV'] || 'development'
|
17
16
|
end
|
18
17
|
|
19
|
-
def self.
|
20
|
-
Philotic::Connection.
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.initialize_named_queue!(queue_name, config)
|
24
|
-
raise 'Philotic::Config.initialize_named_queues must be true to run Philotic.initialize_named_queue!' unless Philotic::Config.initialize_named_queues
|
25
|
-
|
26
|
-
Philotic.connect!
|
27
|
-
queue_exists = Philotic::Connection.connection.queue_exists? queue_name
|
28
|
-
|
29
|
-
should_delete_queue = queue_exists && Philotic::Config.delete_existing_queues
|
30
|
-
should_create_queue = !queue_exists || Philotic::Config.delete_existing_queues
|
31
|
-
|
32
|
-
if should_delete_queue
|
33
|
-
Philotic::Connection.channel.queue(queue_name, passive: true).delete
|
34
|
-
Philotic.logger.info "deleted old queue. queue: #{queue_name}"
|
35
|
-
end
|
36
|
-
|
37
|
-
if should_create_queue
|
38
|
-
config = config.deep_symbolize_keys
|
39
|
-
queue = queue_from_config(queue_name, config)
|
40
|
-
bind_queue(queue, config)
|
41
|
-
else
|
42
|
-
Philotic.logger.warn "Queue #{queue_name} not created; it already exists. Philotic::Config.delete_existing_queues must be true to override."
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.bind_queue(queue, config)
|
47
|
-
queue_exchange = exchange_from_config(config)
|
48
|
-
bindings = config[:bindings]
|
49
|
-
bindings.each do |arguments|
|
50
|
-
queue.bind(queue_exchange, {arguments: arguments})
|
51
|
-
Philotic.logger.info "Added binding to queue. queue: #{queue.name} binding: #{arguments}"
|
52
|
-
end
|
53
|
-
|
54
|
-
Philotic.logger.info "Finished adding bindings to queue. queue: #{queue.name}"
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.exchange_from_config(config)
|
58
|
-
config[:exchange] ? Philotic::Connection.channel.headers(config[:exchange], durable: true) : exchange
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.queue_from_config(queue_name, config)
|
62
|
-
queue_options = DEFAULT_NAMED_QUEUE_OPTIONS.dup
|
63
|
-
queue_options.merge!(config[:options] || {})
|
64
|
-
|
65
|
-
Philotic::Connection.channel.queue(queue_name, queue_options).tap do
|
66
|
-
Philotic.logger.info "Created queue. queue:#{queue_name}"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.logger
|
71
|
-
@@logger ||= init_logger
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.init_logger
|
75
|
-
Logger.new(STDOUT)
|
76
|
-
end
|
77
|
-
|
78
|
-
def self.on_publish_event(&block)
|
79
|
-
@@log_event_handler = block
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.log_event_published(severity, metadata, payload, message)
|
83
|
-
if @@log_event_handler
|
84
|
-
@@log_event_handler.call(severity, metadata, payload, message)
|
85
|
-
else
|
86
|
-
logger.send(severity, "#{message}; message_metadata:#{metadata}, payload:#{payload.to_json}")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.connected?
|
91
|
-
Philotic::Connection.connected?
|
92
|
-
end
|
93
|
-
|
94
|
-
def self.connect!
|
95
|
-
Philotic::Connection.connect!
|
96
|
-
end
|
97
|
-
|
98
|
-
def self.close
|
99
|
-
Philotic::Connection.close
|
18
|
+
def self.connection
|
19
|
+
@connection ||= Philotic::Connection.new
|
100
20
|
end
|
101
21
|
end
|
102
22
|
|
103
23
|
require 'philotic/version'
|
104
|
-
require 'philotic/connection'
|
105
24
|
require 'philotic/config'
|
106
25
|
require 'philotic/routable'
|
107
26
|
require 'philotic/event'
|
data/lib/philotic/config.rb
CHANGED
@@ -4,19 +4,20 @@ require 'singleton'
|
|
4
4
|
require 'forwardable'
|
5
5
|
require 'cgi'
|
6
6
|
require 'bunny/session'
|
7
|
+
require 'logger'
|
7
8
|
|
8
9
|
module Philotic
|
9
|
-
|
10
|
-
extend self
|
10
|
+
class Config
|
11
11
|
|
12
12
|
ENV_PREFIX = 'PHILOTIC'
|
13
13
|
|
14
14
|
DEFAULT_DISABLE_PUBLISH = false
|
15
15
|
DEFAULT_INITIALIZE_NAMED_QUEUES = false
|
16
16
|
DEFAULT_DELETE_EXISTING_QUEUES = false
|
17
|
-
|
17
|
+
DEFAULT_LOG_LEVEL = Logger::DEBUG
|
18
|
+
DEFAULT_RABBIT_SCHEME = 'amqp'
|
18
19
|
DEFAULT_RABBIT_HOST = 'localhost'
|
19
|
-
DEFAULT_RABBIT_PORT =
|
20
|
+
DEFAULT_RABBIT_PORT = 5672
|
20
21
|
DEFAULT_RABBIT_USER = 'guest'
|
21
22
|
DEFAULT_RABBIT_PASSWORD = 'guest'
|
22
23
|
DEFAULT_RABBIT_VHOST = '%2f' # '/'
|
@@ -39,6 +40,17 @@ module Philotic
|
|
39
40
|
DEFAULT_TIMESTAMP = nil
|
40
41
|
DEFAULT_EXPIRATION = nil
|
41
42
|
|
43
|
+
attr_accessor :connection
|
44
|
+
|
45
|
+
def initialize(connection, config={})
|
46
|
+
load_config config
|
47
|
+
@connection = connection
|
48
|
+
end
|
49
|
+
|
50
|
+
def logger
|
51
|
+
connection.logger
|
52
|
+
end
|
53
|
+
|
42
54
|
def defaults
|
43
55
|
@defaults ||= Hash[Config.constants.select { |c| c.to_s.start_with? 'DEFAULT_' }.collect do |c|
|
44
56
|
key = c.slice(8..-1).downcase.to_sym
|
@@ -62,27 +74,30 @@ module Philotic
|
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
77
|
+
def log_level
|
78
|
+
@log_level ||= defaults[:log_level].to_i
|
79
|
+
end
|
65
80
|
attr_writer :connection_failed_handler, :connection_loss_handler, :message_return_handler
|
66
81
|
|
67
82
|
def connection_failed_handler
|
68
83
|
@connection_failed_handler ||= lambda do |settings|
|
69
|
-
|
84
|
+
logger.error "RabbitMQ connection failure; host:#{rabbit_host}"
|
70
85
|
end
|
71
86
|
end
|
72
87
|
|
73
88
|
def connection_loss_handler
|
74
89
|
@connection_loss_handler ||= lambda do |conn, settings|
|
75
|
-
|
90
|
+
logger.warn "RabbitMQ connection loss; host:#{rabbit_host}"; conn.reconnect(false, 2)
|
76
91
|
end
|
77
92
|
end
|
78
93
|
|
79
94
|
def message_return_handler
|
80
95
|
@message_return_handler ||= lambda do |basic_return, metadata, payload|
|
81
|
-
|
96
|
+
logger.warn "Philotic message #{JSON.parse payload} was returned! reply_code = #{basic_return.reply_code}, reply_text = #{basic_return.reply_text} headers = #{metadata[:headers]}"
|
82
97
|
end
|
83
98
|
end
|
84
99
|
|
85
|
-
def
|
100
|
+
def parse_rabbit_uri
|
86
101
|
settings = Bunny::Session.parse_uri(@rabbit_url || defaults[:rabbit_url])
|
87
102
|
settings[:password] = settings.delete(:pass)
|
88
103
|
|
@@ -91,21 +106,19 @@ module Philotic
|
|
91
106
|
current_value = send("rabbit_#{setting}")
|
92
107
|
|
93
108
|
# only use the value from the URI if the existing value is nil or the default
|
94
|
-
if settings[setting] && [const_get("default_rabbit_#{setting}".upcase), nil].include?(current_value)
|
109
|
+
if settings[setting] && [self.class.const_get("default_rabbit_#{setting}".upcase), nil].include?(current_value)
|
95
110
|
send("rabbit_#{setting}=", settings[setting])
|
96
111
|
end
|
97
112
|
end
|
98
113
|
|
99
114
|
end
|
100
115
|
|
101
|
-
def
|
102
|
-
|
116
|
+
def rabbit_url
|
117
|
+
parse_rabbit_uri
|
103
118
|
"#{rabbit_scheme}://#{rabbit_user}:#{rabbit_password}@#{rabbit_host}:#{rabbit_port}/#{CGI.escape rabbit_vhost}"
|
104
119
|
end
|
105
120
|
|
106
|
-
def
|
107
|
-
Philotic.logger # ensure the logger can be created, so we crash early if it can't
|
108
|
-
|
121
|
+
def load_config(config)
|
109
122
|
config.each do |k, v|
|
110
123
|
mutator = "#{k}="
|
111
124
|
send(mutator, v) if respond_to? mutator
|
data/lib/philotic/connection.rb
CHANGED
@@ -1,16 +1,28 @@
|
|
1
|
-
require 'singleton'
|
2
1
|
require 'json'
|
3
2
|
require 'bunny'
|
3
|
+
require 'logger'
|
4
4
|
|
5
5
|
require 'philotic/config'
|
6
|
+
require 'philotic/publisher'
|
7
|
+
require 'philotic/subscriber'
|
6
8
|
|
7
9
|
module Philotic
|
8
|
-
|
9
|
-
extend self
|
10
|
+
class Connection
|
10
11
|
attr_reader :connection
|
12
|
+
attr_accessor :logger
|
13
|
+
|
14
|
+
attr_writer :publisher, :subscriber
|
15
|
+
|
16
|
+
def publisher
|
17
|
+
@publisher ||= Philotic::Publisher.new self
|
18
|
+
end
|
19
|
+
|
20
|
+
def subscriber
|
21
|
+
@subscriber ||= Philotic::Subscriber.new self
|
22
|
+
end
|
11
23
|
|
12
24
|
def config
|
13
|
-
Philotic::Config
|
25
|
+
@config ||= Philotic::Config.new self
|
14
26
|
end
|
15
27
|
|
16
28
|
def connect!
|
@@ -19,11 +31,11 @@ module Philotic
|
|
19
31
|
start_connection!
|
20
32
|
|
21
33
|
if connected?
|
22
|
-
|
34
|
+
logger.info "connected to RabbitMQ: #{config.rabbit_host}:#{config.rabbit_port}"
|
23
35
|
set_exchange_return_handler!
|
24
36
|
true
|
25
37
|
else
|
26
|
-
|
38
|
+
logger.error "failed connected to RabbitMQ; host:#{config.rabbit_host}"
|
27
39
|
false
|
28
40
|
end
|
29
41
|
end
|
@@ -42,9 +54,9 @@ module Philotic
|
|
42
54
|
end
|
43
55
|
|
44
56
|
def close
|
45
|
-
|
57
|
+
logger.info "closing connection to RabbitMQ; host:#{config.rabbit_host}"
|
46
58
|
connection.close if connected?
|
47
|
-
@channel
|
59
|
+
@channel = nil
|
48
60
|
@exchange = nil
|
49
61
|
end
|
50
62
|
|
@@ -65,5 +77,64 @@ module Philotic
|
|
65
77
|
config.message_return_handler.call(basic_return, metadata, payload)
|
66
78
|
end
|
67
79
|
end
|
80
|
+
|
81
|
+
def initialize_named_queue!(queue_name, config)
|
82
|
+
raise 'Philotic.config.initialize_named_queues must be true to run Philotic.initialize_named_queue!' unless self.config.initialize_named_queues
|
83
|
+
|
84
|
+
connect!
|
85
|
+
queue_exists = connection.queue_exists? queue_name
|
86
|
+
|
87
|
+
should_delete_queue = queue_exists && self.config.delete_existing_queues
|
88
|
+
should_create_queue = !queue_exists || self.config.delete_existing_queues
|
89
|
+
|
90
|
+
if should_delete_queue
|
91
|
+
channel.queue(queue_name, passive: true).delete
|
92
|
+
logger.info "deleted old queue. queue: #{queue_name}"
|
93
|
+
end
|
94
|
+
|
95
|
+
if should_create_queue
|
96
|
+
config = config.deep_symbolize_keys
|
97
|
+
queue = queue_from_config(queue_name, config)
|
98
|
+
bind_queue(queue, config)
|
99
|
+
else
|
100
|
+
logger.warn "Queue #{queue_name} not created; it already exists. self.config.delete_existing_queues must be true to override."
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def bind_queue(queue, config)
|
105
|
+
queue_exchange = exchange_from_config(config)
|
106
|
+
bindings = config[:bindings]
|
107
|
+
bindings.each do |arguments|
|
108
|
+
queue.bind(queue_exchange, {arguments: arguments})
|
109
|
+
logger.info "Added binding to queue. queue: #{queue.name} binding: #{arguments}"
|
110
|
+
end
|
111
|
+
|
112
|
+
logger.info "Finished adding bindings to queue. queue: #{queue.name}"
|
113
|
+
end
|
114
|
+
|
115
|
+
def exchange_from_config(config)
|
116
|
+
config[:exchange] ? channel.headers(config[:exchange], durable: true) : exchange
|
117
|
+
end
|
118
|
+
|
119
|
+
def queue_from_config(queue_name, config)
|
120
|
+
queue_options = DEFAULT_NAMED_QUEUE_OPTIONS.dup
|
121
|
+
queue_options.merge!(config[:options] || {})
|
122
|
+
|
123
|
+
channel.queue(queue_name, queue_options).tap do
|
124
|
+
logger.info "Created queue. queue:#{queue_name}"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def logger
|
129
|
+
unless @logger
|
130
|
+
@logger = Logger.new(STDOUT)
|
131
|
+
@logger.level = config.log_level
|
132
|
+
end
|
133
|
+
@logger
|
134
|
+
end
|
135
|
+
|
136
|
+
def publish(event)
|
137
|
+
publisher.publish(event)
|
138
|
+
end
|
68
139
|
end
|
69
140
|
end
|