Module: Watership
- Defined in:
- lib/watership.rb,
lib/watership/version.rb,
lib/watership/consumer.rb,
lib/watership/subscriber.rb
Defined Under Namespace
Modules: Subscriber
Classes: Consumer
Constant Summary
collapse
- CONNECTION_EXCEPTIONS =
[
Bunny::ClientTimeout,
Bunny::NetworkFailure,
Bunny::PossibleAuthenticationFailureError,
Bunny::TCPConnectionFailed
]
- VERSION =
'0.3.6'
Class Method Summary
collapse
Class Method Details
.channel ⇒ Object
46
47
48
49
50
51
|
# File 'lib/watership.rb', line 46
def channel
$channel ||= connection.create_channel
rescue *CONNECTION_EXCEPTIONS => exception
notify(exception)
$channel = nil
end
|
.config=(uri) ⇒ Object
18
19
20
|
# File 'lib/watership.rb', line 18
def config=(uri)
@config = uri
end
|
.connect_with_queue(name, options = {}) ⇒ Object
36
37
38
|
# File 'lib/watership.rb', line 36
def connect_with_queue(name, options = {})
channel.queue(name, { durable: true }.merge(options)) if channel
end
|
.connection ⇒ Object
53
54
55
|
# File 'lib/watership.rb', line 53
def connection
Bunny.new(@config).tap { |bunny| bunny.start }
end
|
.enqueue(options = {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/watership.rb', line 22
def enqueue(options = {})
options = options.dup
message = options.delete(:message)
name = options.delete(:name)
fallback = options.delete(:fallback)
queue = connect_with_queue(name, options)
queue.publish(JSON.generate(message))
rescue StandardError => exception
fallback.call if fallback
notify(exception)
logger.error(exception.class.name)
end
|
.environment=(env) ⇒ Object
14
15
16
|
# File 'lib/watership.rb', line 14
def environment=(env)
@env = env
end
|
.logger ⇒ Object
65
66
67
|
# File 'lib/watership.rb', line 65
def logger
@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end
|
.logger=(logger) ⇒ Object
61
62
63
|
# File 'lib/watership.rb', line 61
def logger=(logger)
@logger = logger
end
|
.notify(exception) ⇒ Object
57
58
59
|
# File 'lib/watership.rb', line 57
def notify(exception)
Airbrake.notify_or_ignore(exception) if defined?(Airbrake) && @env == 'production'
end
|
.reconnect ⇒ Object
40
41
42
43
44
|
# File 'lib/watership.rb', line 40
def reconnect
$channel = nil
channel
true
end
|