Module: EventHub::Helper

Overview

Helper module

Instance Method Summary collapse

Instance Method Details

#bunny_connection_optionsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/eventhub/helper.rb', line 25

def bunny_connection_options
  server = EventHub::Configuration.server

  protocol = "amqp"
  connection_properties = {}
  connection_properties[:user] = server[:user]
  connection_properties[:pass] = server[:password]
  connection_properties[:vhost] = server[:vhost]

  # inject bunny logs on request
  unless server[:show_bunny_logs]
    connection_properties[:logger] = Logger.new(File::NULL)
  end

  # Bunny's network recovery: re-establish connection, re-open channels,
  # re-declare queues, and re-register consumers transparently after a
  # broker disconnect (heartbeat miss, broker restart, LB drop).
  connection_properties[:automatically_recover] = true
  connection_properties[:network_recovery_interval] = 5
  connection_properties[:recovery_attempts] = nil
  connection_properties[:continuation_timeout] = 15_000

  # Belt-and-suspenders: if recovery_attempts is ever capped, escalate to
  # a Celluloid actor restart instead of going silent.
  connection_properties[:recovery_attempts_exhausted] = lambda do
    EventHub.logger.error("Bunny recovery attempts exhausted - actor will restart")
    actor = Celluloid::Actor[:actor_listener_amqp]
    actor&.async&.restart
  end

  # do we do tls?
  if server[:tls]
    protocol = "amqps"
    connection_properties[:tls] = server[:tls]
    connection_properties[:tls_cert] = server[:tls_cert]
    connection_properties[:tls_key] = server[:tls_key]
    connection_properties[:tls_ca_certificates] = server[:tls_ca_certificates]
    connection_properties[:verify_peer] = server[:verify_peer]
  end

  connection_string = "#{protocol}://#{server[:host]}:#{server[:port]}"
  [connection_string, connection_properties]
end

#create_bunny_connectionObject



20
21
22
23
# File 'lib/eventhub/helper.rb', line 20

def create_bunny_connection
  connection_string, connection_properties = bunny_connection_options
  Bunny.new(connection_string, connection_properties)
end

#get_name_from_class(instance) ⇒ Object

Examples: EventHub::Namespace::Demo => namespace.demo EventHub::NameSpace::Demo => name_space.demo EventHub::NameSpace::DemoProcessor => name_space.demo_processor NameSpace::Demo => name_space.demo



13
14
15
16
17
18
# File 'lib/eventhub/helper.rb', line 13

def get_name_from_class(instance)
  instance.class.to_s.split("::").map { |element|
    next if element == "EventHub"
    element.split(/(?=[A-Z])/).join("_").downcase
  }.compact.join(".")
end

#now_stamp(now = nil) ⇒ Object

Formats stamp into UTC format



70
71
72
73
# File 'lib/eventhub/helper.rb', line 70

def now_stamp(now = nil)
  now ||= Time.now
  now.utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ")
end

#stringify_keys(h) ⇒ Object

stringify hash keys



76
77
78
# File 'lib/eventhub/helper.rb', line 76

def stringify_keys(h)
  JSON.parse(h.to_json)
end