promiscuous 0.22 → 0.23
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/promiscuous.rb +6 -12
- data/lib/promiscuous/amqp.rb +2 -3
- data/lib/promiscuous/amqp/bunny.rb +23 -27
- data/lib/promiscuous/amqp/null.rb +10 -14
- data/lib/promiscuous/amqp/ruby_amqp.rb +89 -0
- data/lib/promiscuous/autoload.rb +7 -0
- data/lib/promiscuous/cli.rb +0 -2
- data/lib/promiscuous/common.rb +2 -4
- data/lib/promiscuous/common/lint.rb +2 -1
- data/lib/promiscuous/common/lint/base.rb +5 -3
- data/lib/promiscuous/config.rb +18 -20
- data/lib/promiscuous/error.rb +2 -3
- data/lib/promiscuous/loader.rb +16 -18
- data/lib/promiscuous/publisher.rb +3 -14
- data/lib/promiscuous/publisher/lint.rb +13 -7
- data/lib/promiscuous/publisher/lint/class.rb +4 -2
- data/lib/promiscuous/publisher/lint/polymorphic.rb +1 -0
- data/lib/promiscuous/publisher/mongoid.rb +2 -4
- data/lib/promiscuous/railtie.rb +9 -11
- data/lib/promiscuous/subscriber.rb +3 -14
- data/lib/promiscuous/subscriber/lint.rb +12 -6
- data/lib/promiscuous/subscriber/mongoid.rb +2 -2
- data/lib/promiscuous/version.rb +1 -1
- metadata +16 -15
- data/lib/promiscuous/amqp/rubyamqp.rb +0 -93
data/lib/promiscuous.rb
CHANGED
@@ -1,18 +1,12 @@
|
|
1
1
|
require 'active_support/core_ext'
|
2
|
-
require 'promiscuous/config'
|
3
|
-
require 'promiscuous/amqp'
|
4
|
-
require 'promiscuous/loader'
|
5
|
-
require 'promiscuous/railtie' if defined?(Rails)
|
6
2
|
|
7
3
|
module Promiscuous
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
autoload :Worker,
|
13
|
-
|
14
|
-
autoload :CLI, 'promiscuous/cli'
|
15
|
-
autoload :Error, 'promiscuous/error'
|
4
|
+
require 'promiscuous/autoload'
|
5
|
+
require 'promiscuous/railtie' if defined?(Rails)
|
6
|
+
|
7
|
+
extend Promiscuous::Autoload
|
8
|
+
autoload :Common, :Publisher, :Subscriber, :Observer, :Worker, :Ephemeral,
|
9
|
+
:CLI, :Error, :Loader, :AMQP, :Config
|
16
10
|
|
17
11
|
class << self
|
18
12
|
def configure(&block)
|
data/lib/promiscuous/amqp.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Promiscuous::AMQP
|
2
|
-
|
3
|
-
autoload :RubyAMQP,
|
4
|
-
autoload :Null, 'promiscuous/amqp/null'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Bunny, :RubyAMQP, :Null
|
5
4
|
|
6
5
|
EXCHANGE = 'promiscuous'.freeze
|
7
6
|
|
@@ -1,35 +1,31 @@
|
|
1
|
-
module Promiscuous
|
2
|
-
|
3
|
-
module Bunny
|
4
|
-
mattr_accessor :connection
|
1
|
+
module Promiscuous::AMQP:: Bunny
|
2
|
+
mattr_accessor :connection
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
def self.connect
|
5
|
+
require 'bunny'
|
6
|
+
self.connection = ::Bunny.new(Promiscuous::Config.server_uri)
|
7
|
+
self.connection.start
|
8
|
+
end
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def self.disconnect
|
11
|
+
self.connection.stop
|
12
|
+
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def self.connected?
|
15
|
+
!!self.connection.try(:connected?)
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
def self.publish(options={})
|
19
|
+
Promiscuous.info "[publish] (#{options[:exchange_name]}) #{options[:key]} -> #{options[:payload]}"
|
20
|
+
exchange(options[:exchange_name]).
|
21
|
+
publish(options[:payload], :key => options[:key], :persistent => true)
|
22
|
+
end
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
def self.open_queue(options={}, &block)
|
25
|
+
raise "Cannot open queue with bunny"
|
26
|
+
end
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
28
|
+
def self.exchange(name)
|
29
|
+
connection.exchange(name, :type => :topic, :durable => true)
|
34
30
|
end
|
35
31
|
end
|
@@ -1,20 +1,16 @@
|
|
1
|
-
module Promiscuous
|
2
|
-
|
3
|
-
|
4
|
-
def self.connect
|
5
|
-
end
|
1
|
+
module Promiscuous::AMQP::Null
|
2
|
+
def self.connect
|
3
|
+
end
|
6
4
|
|
7
|
-
|
8
|
-
|
5
|
+
def self.disconnect
|
6
|
+
end
|
9
7
|
|
10
|
-
|
11
|
-
|
8
|
+
def self.connected?
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
11
|
+
def self.publish(options={})
|
12
|
+
end
|
15
13
|
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
14
|
+
def self.open_queue(options={}, &block)
|
19
15
|
end
|
20
16
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Promiscuous::AMQP::RubyAMQP
|
2
|
+
mattr_accessor :channel
|
3
|
+
|
4
|
+
def self.connect
|
5
|
+
require 'amqp'
|
6
|
+
|
7
|
+
amqp_options = if Promiscuous::Config.server_uri
|
8
|
+
uri = URI.parse(Promiscuous::Config.server_uri)
|
9
|
+
raise "Please use amqp://user:password@host:port/vhost" if uri.scheme != 'amqp'
|
10
|
+
|
11
|
+
{
|
12
|
+
:host => uri.host,
|
13
|
+
:port => uri.port,
|
14
|
+
:scheme => uri.scheme,
|
15
|
+
:user => uri.user,
|
16
|
+
:pass => uri.password,
|
17
|
+
:vhost => uri.path.empty? ? "/" : uri.path,
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
connection = ::AMQP.connect(amqp_options)
|
22
|
+
self.channel = ::AMQP::Channel.new(connection, :auto_recovery => true, :prefetch => 100)
|
23
|
+
|
24
|
+
connection.on_tcp_connection_loss do |conn|
|
25
|
+
unless conn.reconnecting?
|
26
|
+
Promiscuous.warn "[connection] Lost connection. Reconnecting..."
|
27
|
+
conn.periodically_reconnect(2)
|
28
|
+
|
29
|
+
exception = Promiscuous::Error::Connection.new 'Lost connection'
|
30
|
+
Promiscuous::Worker.stop # TODO XXX This doesn't belong here. hooks ?
|
31
|
+
Promiscuous::Config.error_notifier.try(:call, exception)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
connection.on_recovery do |conn|
|
36
|
+
Promiscuous.warn "[connection] Reconnected"
|
37
|
+
Promiscuous::Worker.resume # TODO XXX This doesn't belong here. hooks ?
|
38
|
+
end
|
39
|
+
|
40
|
+
connection.on_error do |conn, conn_close|
|
41
|
+
# No need to handle CONNECTION_FORCED since on_tcp_connection_loss takes
|
42
|
+
# care of it.
|
43
|
+
Promiscuous.warn "[connection] #{conn_close.reply_text}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.disconnect
|
48
|
+
if self.channel && self.channel.connection.connected?
|
49
|
+
self.channel.connection.close
|
50
|
+
self.channel.close
|
51
|
+
end
|
52
|
+
self.channel = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
# Always disconnect when shutting down to avoid reconnection
|
56
|
+
EM.add_shutdown_hook { Promiscuous::AMQP::RubyAMQP.disconnect }
|
57
|
+
|
58
|
+
def self.connected?
|
59
|
+
!!self.channel.try(:connection).try(:connected?)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.open_queue(options={}, &block)
|
63
|
+
queue_name = options[:queue_name]
|
64
|
+
bindings = options[:bindings]
|
65
|
+
|
66
|
+
queue = self.channel.queue(queue_name, Promiscuous::Config.queue_options)
|
67
|
+
bindings.each do |binding|
|
68
|
+
queue.bind(exchange(options[:exchange_name]), :routing_key => binding)
|
69
|
+
Promiscuous.info "[bind] #{queue_name} -> #{binding}"
|
70
|
+
end
|
71
|
+
block.call(queue) if block
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.publish(options={})
|
75
|
+
info_msg = "(#{options[:exchange_name]}) #{options[:key]} -> #{options[:payload]}"
|
76
|
+
|
77
|
+
unless channel.connection.connected?
|
78
|
+
raise Promiscuous::Error::Connection.new 'Not connected'
|
79
|
+
end
|
80
|
+
|
81
|
+
Promiscuous.info "[publish] #{info_msg}"
|
82
|
+
exchange(options[:exchange_name]).
|
83
|
+
publish(options[:payload], :routing_key => options[:key], :persistent => true)
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.exchange(name)
|
87
|
+
channel.topic(name, :durable => true)
|
88
|
+
end
|
89
|
+
end
|
data/lib/promiscuous/cli.rb
CHANGED
data/lib/promiscuous/common.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
module Promiscuous::Common
|
2
|
-
|
3
|
-
autoload :Lint,
|
4
|
-
autoload :ClassHelpers, 'promiscuous/common/class_helpers'
|
5
|
-
autoload :Worker, 'promiscuous/common/worker'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Options, :Lint, :ClassHelpers, :Worker
|
6
4
|
end
|
@@ -13,9 +13,11 @@ module Promiscuous::Common::Lint::Base
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module ClassMethods
|
16
|
-
def use_option(
|
17
|
-
|
18
|
-
|
16
|
+
def use_option(*attrs)
|
17
|
+
attrs.each do |attr|
|
18
|
+
define_method(attr) do
|
19
|
+
self.options[attr]
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
data/lib/promiscuous/config.rb
CHANGED
@@ -1,28 +1,26 @@
|
|
1
|
-
module Promiscuous
|
2
|
-
|
3
|
-
mattr_accessor :app, :logger, :error_notifier, :backend, :server_uri, :queue_options
|
1
|
+
module Promiscuous::Config
|
2
|
+
mattr_accessor :app, :logger, :error_notifier, :backend, :server_uri, :queue_options
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def self.backend=(value)
|
5
|
+
@@backend = "Promiscuous::AMQP::#{value.to_s.camelize.gsub(/amqp/, 'AMQP')}".constantize unless value.nil?
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
def self.configure(&block)
|
9
|
+
class_variables.each { |var| class_variable_set(var, nil) }
|
11
10
|
|
12
|
-
|
11
|
+
block.call(self)
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
self.backend ||= defined?(EventMachine) && EventMachine.reactor_running? ? :rubyamqp : :bunny
|
14
|
+
self.logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDERR).tap { |l| l.level = Logger::WARN }
|
15
|
+
self.queue_options ||= {:durable => true, :arguments => {'x-ha-policy' => 'all'}}
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
Promiscuous::AMQP.connect
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
20
|
+
# TODO backward compatibility. to be removed.
|
21
|
+
def self.error_handler=(value)
|
22
|
+
ActiveSupport::Deprecation.behavior = :stderr
|
23
|
+
ActiveSupport::Deprecation.warn "'error_handler' is deprecated, please use 'error_notifier'", caller
|
24
|
+
self.error_notifier = value
|
27
25
|
end
|
28
26
|
end
|
data/lib/promiscuous/error.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module Promiscuous::Error
|
2
|
-
|
3
|
-
autoload :Publisher,
|
4
|
-
autoload :Subscriber, 'promiscuous/error/subscriber'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Connection, :Publisher, :Subscriber
|
5
4
|
end
|
data/lib/promiscuous/loader.rb
CHANGED
@@ -1,25 +1,23 @@
|
|
1
|
-
module Promiscuous
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
when :subscribers then %w(subscribers **_subscriber.rb)
|
8
|
-
end
|
9
|
-
|
10
|
-
Dir[Rails.root.join('app', dir, file_matcher)].map do |file|
|
11
|
-
File.basename(file, ".rb").camelize.constantize
|
1
|
+
module Promiscuous::Loader
|
2
|
+
def self.load_descriptors(descriptors=[:publishers, :subscribers])
|
3
|
+
[descriptors].flatten.each do |descriptor|
|
4
|
+
dir, file_matcher = case descriptor
|
5
|
+
when :publishers then %w(publishers **_publisher.rb)
|
6
|
+
when :subscribers then %w(subscribers **_subscriber.rb)
|
12
7
|
end
|
8
|
+
|
9
|
+
Dir[Rails.root.join('app', dir, file_matcher)].map do |file|
|
10
|
+
File.basename(file, ".rb").camelize.constantize
|
13
11
|
end
|
14
12
|
end
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
15
|
+
def self.unload_descriptors(descriptors=[:publishers, :subscribers])
|
16
|
+
[descriptors].flatten.each do |descriptor|
|
17
|
+
dir, file_matcher = case descriptor
|
18
|
+
when :publishers then # TODO Cleanup publishers
|
19
|
+
when :subscribers then Promiscuous::Subscriber::AMQP.subscribers.clear
|
20
|
+
end
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
@@ -1,18 +1,7 @@
|
|
1
1
|
module Promiscuous::Publisher
|
2
|
-
|
3
|
-
autoload :AMQP,
|
4
|
-
|
5
|
-
autoload :Base, 'promiscuous/publisher/base'
|
6
|
-
autoload :Class, 'promiscuous/publisher/class'
|
7
|
-
autoload :Envelope, 'promiscuous/publisher/envelope'
|
8
|
-
autoload :Lint, 'promiscuous/publisher/lint'
|
9
|
-
autoload :Mock, 'promiscuous/publisher/mock'
|
10
|
-
autoload :Model, 'promiscuous/publisher/model'
|
11
|
-
autoload :Mongoid, 'promiscuous/publisher/mongoid'
|
12
|
-
autoload :Polymorphic, 'promiscuous/publisher/polymorphic'
|
13
|
-
autoload :Worker, 'promiscuous/publisher/worker'
|
14
|
-
autoload :Error, 'promiscuous/publisher/error'
|
15
|
-
autoload :Ephemeral, 'promiscuous/publisher/ephemeral'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :ActiveRecord, :AMQP, :Attributes, :Base, :Class, :Envelope, :Lint,
|
4
|
+
:Mock, :Model, :Mongoid, :Polymorphic, :Worker, :Error, :Ephemeral
|
16
5
|
|
17
6
|
def self.lint(*args)
|
18
7
|
Lint.lint(*args)
|
@@ -1,9 +1,6 @@
|
|
1
1
|
module Promiscuous::Publisher::Lint
|
2
|
-
|
3
|
-
autoload :Class,
|
4
|
-
autoload :Attributes, 'promiscuous/publisher/lint/attributes'
|
5
|
-
autoload :Polymorphic, 'promiscuous/publisher/lint/polymorphic'
|
6
|
-
autoload :AMQP, 'promiscuous/publisher/lint/amqp'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Base, :Class, :Attributes, :Polymorphic, :AMQP
|
7
4
|
|
8
5
|
def self.get_publisher(klass)
|
9
6
|
unless klass.respond_to?(:promiscuous_publisher)
|
@@ -13,8 +10,17 @@ module Promiscuous::Publisher::Lint
|
|
13
10
|
klass.promiscuous_publisher
|
14
11
|
end
|
15
12
|
|
16
|
-
def self.lint(
|
17
|
-
|
13
|
+
def self.lint(class_bindings={})
|
14
|
+
if class_bindings.empty?
|
15
|
+
class_bindings = Promiscuous::Publisher::Mongoid::Defer.klasses.values.reduce({}) do |res, klass|
|
16
|
+
res[klass] = klass.promiscuous_publisher.to
|
17
|
+
res
|
18
|
+
end
|
19
|
+
|
20
|
+
raise "No publishers found" if class_bindings.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
class_bindings.each do |klass, to|
|
18
24
|
pub = get_publisher(klass)
|
19
25
|
|
20
26
|
lint = ::Class.new(Base)
|
@@ -5,9 +5,11 @@ module Promiscuous::Publisher::Lint::Class
|
|
5
5
|
super
|
6
6
|
|
7
7
|
if publisher.klass != klass
|
8
|
-
|
8
|
+
msg = "Please define a publisher for #{klass}"
|
9
|
+
msg = "#{msg} because #{parent} is published and you need to cover subclasses" if parent
|
10
|
+
raise msg
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
|
-
included { use_option :klass }
|
14
|
+
included { use_option :klass, :parent }
|
13
15
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
class Promiscuous::Publisher::Mongoid < Promiscuous::Publisher::Base
|
2
|
-
|
3
|
-
autoload :DeferEmbedded,
|
4
|
-
autoload :Defer, 'promiscuous/publisher/mongoid/defer'
|
5
|
-
autoload :EmbeddedMany, 'promiscuous/publisher/mongoid/embedded_many'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Embedded, :DeferEmbedded, :Defer, :EmbeddedMany
|
6
4
|
|
7
5
|
include Promiscuous::Publisher::Class
|
8
6
|
include Promiscuous::Publisher::Attributes
|
data/lib/promiscuous/railtie.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
Promiscuous::Loader.unload_descriptors
|
11
|
-
end
|
1
|
+
class Promiscuous::Railtie < Rails::Railtie
|
2
|
+
initializer 'load promiscuous' do
|
3
|
+
config.after_initialize do
|
4
|
+
Promiscuous::Loader.load_descriptors(:publishers)
|
5
|
+
ActionDispatch::Reloader.to_prepare do
|
6
|
+
Promiscuous::Loader.load_descriptors
|
7
|
+
end
|
8
|
+
ActionDispatch::Reloader.to_cleanup do
|
9
|
+
Promiscuous::Loader.unload_descriptors
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
@@ -1,18 +1,7 @@
|
|
1
1
|
module Promiscuous::Subscriber
|
2
|
-
|
3
|
-
autoload :AMQP,
|
4
|
-
|
5
|
-
autoload :Base, 'promiscuous/subscriber/base'
|
6
|
-
autoload :Class, 'promiscuous/subscriber/class'
|
7
|
-
autoload :Envelope, 'promiscuous/subscriber/envelope'
|
8
|
-
autoload :Error, 'promiscuous/subscriber/error'
|
9
|
-
autoload :Lint, 'promiscuous/subscriber/lint'
|
10
|
-
autoload :Model, 'promiscuous/subscriber/model'
|
11
|
-
autoload :Mongoid, 'promiscuous/subscriber/mongoid'
|
12
|
-
autoload :Polymorphic, 'promiscuous/subscriber/polymorphic'
|
13
|
-
autoload :Upsert, 'promiscuous/subscriber/upsert'
|
14
|
-
autoload :Observer, 'promiscuous/subscriber/observer'
|
15
|
-
autoload :Worker, 'promiscuous/subscriber/worker'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :ActiveRecord, :AMQP, :Attributes, :Base, :Class, :Envelope, :Error,
|
4
|
+
:Lint, :Model, :Mongoid, :Polymorphic, :Upsert, :Observer, :Worker
|
16
5
|
|
17
6
|
def self.lint(*args)
|
18
7
|
Lint.lint(*args)
|
@@ -1,13 +1,19 @@
|
|
1
1
|
module Promiscuous::Subscriber::Lint
|
2
|
-
|
3
|
-
autoload :Class,
|
4
|
-
autoload :Attributes, 'promiscuous/subscriber/lint/attributes'
|
5
|
-
autoload :Polymorphic, 'promiscuous/subscriber/lint/polymorphic'
|
6
|
-
autoload :AMQP, 'promiscuous/subscriber/lint/amqp'
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Base, :Class, :Attributes, :Polymorphic, :AMQP
|
7
4
|
|
8
|
-
def self.lint(binding_classes)
|
5
|
+
def self.lint(binding_classes={})
|
9
6
|
Base.reload_publishers
|
10
7
|
|
8
|
+
if binding_classes.empty?
|
9
|
+
binding_classes = Promiscuous::Subscriber::AMQP.subscribers.reduce({}) do |res, e|
|
10
|
+
from, sub = e
|
11
|
+
res[from] = sub.klass unless from =~ /^__promiscuous__/
|
12
|
+
res
|
13
|
+
end
|
14
|
+
raise "No subscribers found" if binding_classes.empty?
|
15
|
+
end
|
16
|
+
|
11
17
|
binding_classes.each do |from, klass|
|
12
18
|
sub = Promiscuous::Subscriber::AMQP.subscribers[from]
|
13
19
|
raise "#{from} has no subscriber" if sub.nil?
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class Promiscuous::Subscriber::Mongoid < Promiscuous::Subscriber::Base
|
2
|
-
|
3
|
-
autoload :
|
2
|
+
extend Promiscuous::Autoload
|
3
|
+
autoload :Embedded, :Versioning
|
4
4
|
|
5
5
|
include Promiscuous::Subscriber::Class
|
6
6
|
include Promiscuous::Subscriber::Attributes
|
data/lib/promiscuous/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: promiscuous
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.23'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -117,25 +117,24 @@ executables:
|
|
117
117
|
extensions: []
|
118
118
|
extra_rdoc_files: []
|
119
119
|
files:
|
120
|
-
- lib/promiscuous/amqp/null.rb
|
121
120
|
- lib/promiscuous/amqp/bunny.rb
|
122
|
-
- lib/promiscuous/amqp/
|
121
|
+
- lib/promiscuous/amqp/null.rb
|
122
|
+
- lib/promiscuous/amqp/ruby_amqp.rb
|
123
123
|
- lib/promiscuous/common/lint/base.rb
|
124
|
-
- lib/promiscuous/common/lint.rb
|
125
124
|
- lib/promiscuous/common/class_helpers.rb
|
126
125
|
- lib/promiscuous/common/options.rb
|
127
126
|
- lib/promiscuous/common/worker.rb
|
127
|
+
- lib/promiscuous/common/lint.rb
|
128
128
|
- lib/promiscuous/publisher/lint/amqp.rb
|
129
129
|
- lib/promiscuous/publisher/lint/attributes.rb
|
130
130
|
- lib/promiscuous/publisher/lint/base.rb
|
131
|
-
- lib/promiscuous/publisher/lint/class.rb
|
132
131
|
- lib/promiscuous/publisher/lint/polymorphic.rb
|
132
|
+
- lib/promiscuous/publisher/lint/class.rb
|
133
133
|
- lib/promiscuous/publisher/mongoid/embedded.rb
|
134
134
|
- lib/promiscuous/publisher/mongoid/defer_embedded.rb
|
135
135
|
- lib/promiscuous/publisher/mongoid/embedded_many.rb
|
136
136
|
- lib/promiscuous/publisher/mongoid/defer.rb
|
137
137
|
- lib/promiscuous/publisher/envelope.rb
|
138
|
-
- lib/promiscuous/publisher/lint.rb
|
139
138
|
- lib/promiscuous/publisher/polymorphic.rb
|
140
139
|
- lib/promiscuous/publisher/mock.rb
|
141
140
|
- lib/promiscuous/publisher/attributes.rb
|
@@ -144,9 +143,10 @@ files:
|
|
144
143
|
- lib/promiscuous/publisher/base.rb
|
145
144
|
- lib/promiscuous/publisher/class.rb
|
146
145
|
- lib/promiscuous/publisher/ephemeral.rb
|
147
|
-
- lib/promiscuous/publisher/mongoid.rb
|
148
146
|
- lib/promiscuous/publisher/amqp.rb
|
149
147
|
- lib/promiscuous/publisher/worker.rb
|
148
|
+
- lib/promiscuous/publisher/mongoid.rb
|
149
|
+
- lib/promiscuous/publisher/lint.rb
|
150
150
|
- lib/promiscuous/subscriber/lint/amqp.rb
|
151
151
|
- lib/promiscuous/subscriber/lint/base.rb
|
152
152
|
- lib/promiscuous/subscriber/lint/class.rb
|
@@ -157,7 +157,6 @@ files:
|
|
157
157
|
- lib/promiscuous/subscriber/mongoid/versioning.rb
|
158
158
|
- lib/promiscuous/subscriber/active_record.rb
|
159
159
|
- lib/promiscuous/subscriber/envelope.rb
|
160
|
-
- lib/promiscuous/subscriber/lint.rb
|
161
160
|
- lib/promiscuous/subscriber/upsert.rb
|
162
161
|
- lib/promiscuous/subscriber/observer.rb
|
163
162
|
- lib/promiscuous/subscriber/polymorphic.rb
|
@@ -166,23 +165,25 @@ files:
|
|
166
165
|
- lib/promiscuous/subscriber/model.rb
|
167
166
|
- lib/promiscuous/subscriber/base.rb
|
168
167
|
- lib/promiscuous/subscriber/class.rb
|
169
|
-
- lib/promiscuous/subscriber/mongoid.rb
|
170
168
|
- lib/promiscuous/subscriber/worker.rb
|
169
|
+
- lib/promiscuous/subscriber/mongoid.rb
|
170
|
+
- lib/promiscuous/subscriber/lint.rb
|
171
171
|
- lib/promiscuous/error/connection.rb
|
172
172
|
- lib/promiscuous/error/publisher.rb
|
173
173
|
- lib/promiscuous/error/subscriber.rb
|
174
|
-
- lib/promiscuous/loader.rb
|
175
174
|
- lib/promiscuous/observer.rb
|
176
|
-
- lib/promiscuous/common.rb
|
177
175
|
- lib/promiscuous/ephemeral.rb
|
176
|
+
- lib/promiscuous/worker.rb
|
177
|
+
- lib/promiscuous/autoload.rb
|
178
178
|
- lib/promiscuous/subscriber.rb
|
179
179
|
- lib/promiscuous/publisher.rb
|
180
|
-
- lib/promiscuous/railtie.rb
|
181
|
-
- lib/promiscuous/amqp.rb
|
182
180
|
- lib/promiscuous/error.rb
|
183
|
-
- lib/promiscuous/
|
181
|
+
- lib/promiscuous/common.rb
|
182
|
+
- lib/promiscuous/amqp.rb
|
183
|
+
- lib/promiscuous/loader.rb
|
184
|
+
- lib/promiscuous/railtie.rb
|
184
185
|
- lib/promiscuous/cli.rb
|
185
|
-
- lib/promiscuous/
|
186
|
+
- lib/promiscuous/config.rb
|
186
187
|
- lib/promiscuous/version.rb
|
187
188
|
- lib/promiscuous.rb
|
188
189
|
- bin/promiscuous
|
@@ -1,93 +0,0 @@
|
|
1
|
-
module Promiscuous
|
2
|
-
module AMQP
|
3
|
-
module RubyAMQP
|
4
|
-
mattr_accessor :channel
|
5
|
-
|
6
|
-
def self.connect
|
7
|
-
require 'amqp'
|
8
|
-
|
9
|
-
amqp_options = if Promiscuous::Config.server_uri
|
10
|
-
uri = URI.parse(Promiscuous::Config.server_uri)
|
11
|
-
raise "Please use amqp://user:password@host:port/vhost" if uri.scheme != 'amqp'
|
12
|
-
|
13
|
-
{
|
14
|
-
:host => uri.host,
|
15
|
-
:port => uri.port,
|
16
|
-
:scheme => uri.scheme,
|
17
|
-
:user => uri.user,
|
18
|
-
:pass => uri.password,
|
19
|
-
:vhost => uri.path.empty? ? "/" : uri.path,
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
connection = ::AMQP.connect(amqp_options)
|
24
|
-
self.channel = ::AMQP::Channel.new(connection, :auto_recovery => true, :prefetch => 100)
|
25
|
-
|
26
|
-
connection.on_tcp_connection_loss do |conn|
|
27
|
-
unless conn.reconnecting?
|
28
|
-
Promiscuous.warn "[connection] Lost connection. Reconnecting..."
|
29
|
-
conn.periodically_reconnect(2)
|
30
|
-
|
31
|
-
exception = Promiscuous::Error::Connection.new 'Lost connection'
|
32
|
-
Promiscuous::Worker.stop # TODO XXX This doesn't belong here. hooks ?
|
33
|
-
Promiscuous::Config.error_notifier.try(:call, exception)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
connection.on_recovery do |conn|
|
38
|
-
Promiscuous.warn "[connection] Reconnected"
|
39
|
-
Promiscuous::Worker.resume # TODO XXX This doesn't belong here. hooks ?
|
40
|
-
end
|
41
|
-
|
42
|
-
connection.on_error do |conn, conn_close|
|
43
|
-
# No need to handle CONNECTION_FORCED since on_tcp_connection_loss takes
|
44
|
-
# care of it.
|
45
|
-
Promiscuous.warn "[connection] #{conn_close.reply_text}"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.disconnect
|
50
|
-
if self.channel && self.channel.connection.connected?
|
51
|
-
self.channel.connection.close
|
52
|
-
self.channel.close
|
53
|
-
end
|
54
|
-
self.channel = nil
|
55
|
-
end
|
56
|
-
|
57
|
-
# Always disconnect when shutting down to avoid reconnection
|
58
|
-
EM.add_shutdown_hook { Promiscuous::AMQP::RubyAMQP.disconnect }
|
59
|
-
|
60
|
-
def self.connected?
|
61
|
-
!!self.channel.try(:connection).try(:connected?)
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.open_queue(options={}, &block)
|
65
|
-
queue_name = options[:queue_name]
|
66
|
-
bindings = options[:bindings]
|
67
|
-
|
68
|
-
queue = self.channel.queue(queue_name, Promiscuous::Config.queue_options)
|
69
|
-
bindings.each do |binding|
|
70
|
-
queue.bind(exchange(options[:exchange_name]), :routing_key => binding)
|
71
|
-
Promiscuous.info "[bind] #{queue_name} -> #{binding}"
|
72
|
-
end
|
73
|
-
block.call(queue) if block
|
74
|
-
end
|
75
|
-
|
76
|
-
def self.publish(options={})
|
77
|
-
info_msg = "(#{options[:exchange_name]}) #{options[:key]} -> #{options[:payload]}"
|
78
|
-
|
79
|
-
unless channel.connection.connected?
|
80
|
-
raise Promiscuous::Error::Connection.new 'Not connected'
|
81
|
-
end
|
82
|
-
|
83
|
-
Promiscuous.info "[publish] #{info_msg}"
|
84
|
-
exchange(options[:exchange_name]).
|
85
|
-
publish(options[:payload], :routing_key => options[:key], :persistent => true)
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.exchange(name)
|
89
|
-
channel.topic(name, :durable => true)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|