Class: AngieCoreApi::Messaging::AMQPClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/angie-core-api/messaging/amqp_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAMQPClient

Returns a new instance of AMQPClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 16

def initialize
  rabbitmq = AngieCoreApi.configuration.rabbitmq
  return unless rabbitmq&.has_key?(:url)

  @config = rabbitmq.with_indifferent_access
  @connection = Bunny.new(config[:url])
  @connection.start

  @channel = @connection.create_channel

  puts "Connected to #{amqp_url}"
rescue Bunny::TCPConnectionFailed => e
  puts "Connection to #{amqp_url} failed: #{e.message}"
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



10
11
12
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 10

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 10

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 10

def connection
  @connection
end

Class Method Details

.classesObject



31
32
33
34
35
36
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 31

def self.classes
  Dir["app/exchanges/*.rb", "engines/**/app/exchanges/*.rb"].
    map { |file| File.basename(file, ".rb") }.
    map(&:camelize).
    map(&:constantize)
end

.connectObject



38
39
40
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 38

def self.connect
  classes.each { |k| k.new } if instance.connection&.connected?
end

.disconnect!Object



42
43
44
45
46
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 42

def self.disconnect!
  instance.connection.close
  @@instance = nil
  puts "Disconnected from #{instance.amqp_url}"
end

.instanceObject



12
13
14
# File 'lib/angie-core-api/messaging/amqp_client.rb', line 12

def self.instance
  @@instance ||= new
end