Class: Anynines::RabbitMQ::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/a9s_rabbitmq/utility.rb

Class Method Summary collapse

Class Method Details

.amqp_hash(additional_options = {}) ⇒ Object

returns an amqp gem compatible hash takes an optional hash for specifying additional options like: :ssl (Hash) — TLS (SSL) parameters to use. :heartbeat (Fixnum) — default: 0 — Connection heartbeat, in seconds. 0 means no heartbeat. Can also be configured server-side starting with RabbitMQ 3.0. :on_tcp_connection_failure (#call) — A callable object that will be run if connection to server fails :on_possible_authentication_failure (#call) — A callable object that will be run if authentication fails (see Authentication failure section)

Parameters:

  • additional_options (Hash) (defaults to: {})
    • additional options hash



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/a9s_rabbitmq/utility.rb', line 38

def self.amqp_hash(additional_options = {})
  creds = credentials_hash
  
  hash = {
    host: creds[:host],
    port: creds[:port],
    vhost: creds[:vhost],
    username: creds[:username],
    password: creds[:password]
  }
  
  hash = additional_options.merge hash
  return hash
end

.bunny_hash(additional_options = {}) ⇒ Object

returns a bunny compatible hash takes an optional hash for specifying additional options like: :heartbeat or :heartbeat_interval, in seconds, default is 0 (no heartbeats). :server means “use the value from RabbitMQ config” :log_level (symbol or integer, default: Logger::WARN): log level to use. :log_file (string or IO): log file or IO object to use. Defaults to STDOUT. :automatically_recover (boolean, default: true): when false, will disable automatic network failure recovery :network_recovery_interval (number, default: “): interval between reconnection attempts :threaded (boolean): switches to single-threaded connections when set to false. Only recommended for apps that only publish messages. :continuation_timeout (integer): timeout for client operations that expect a response (e.g. Bunny::Queue#get), in milliseconds. Default is 4000 ms.

Parameters:

  • additional_options (Hash) (defaults to: {})
    • additional options hash



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/a9s_rabbitmq/utility.rb', line 66

def self.bunny_hash(additional_options = {})
  creds = credentials_hash
  
  hash = {
    host: creds[:host],
    port: creds[:port],
    vhost: creds[:vhost],
    username: creds[:username],
    password: creds[:password],
  }
  
  hash = additional_options.merge hash
  return hash
end

.credentials_hashObject

Returns a credentials hash for the rabbitmq service



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/a9s_rabbitmq/utility.rb', line 7

def self.credentials_hash
  # parse the VCAP_SERVICES environment variable
  services = JSON.parse(ENV["VCAP_SERVICES"])  
  raise "Couldn't find the VCAP_SERVICE env variable! Are you running within an anynines environment?" if services.nil?
  raise "Couldn't access the a9s rabbitmq service credentials from env! Have you bound a rabbitmq service instance to the application?" if services["rabbitmq-2.8"].nil?
  rabbit_service = services["rabbitmq-2.8"].first
  
  return {
    name: rabbit_service["credentials"]["name"],
    hostname: rabbit_service["credentials"]["hostname"],
    host: rabbit_service["credentials"]["host"],
    port: rabbit_service["credentials"]["port"],
    admin_port: rabbit_service["credentials"]["admin_port"],
    vhost: rabbit_service["credentials"]["vhost"],
    username: rabbit_service["credentials"]["username"],
    user: rabbit_service["credentials"]["user"],
    password: rabbit_service["credentials"]["password"],
    pass: rabbit_service["credentials"]["pass"],
    url: rabbit_service["credentials"]["url"]
  }
end