Class: PushyDaemon::Shouter

Inherits:
Endpoint show all
Defined in:
lib/pushyd/shouter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShouter

Returns a new instance of Shouter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pushyd/shouter.rb', line 16

def initialize
  # Init
  super
  @keys = []

  # Start connexion to RabbitMQ and create channel
  conn = connect Conf.bus
  @channel = conn.create_channel
  info "connected on a channel"

  # Check config
  config_shout = Conf[:shout]
  if (config_shout.is_a? Enumerable) && !config_shout.empty?
    @keys = config_shout[:keys] if config_shout[:keys].is_a? Array
    @topic = config_shout[:topic]

    info "found topic: #{@topic}"
    info "found keys: #{@keys.join(', ')}"
  else
    error "prepare: empty [shout] section"
  end

  # Create exchange
  raise PushyDaemon::EndpointTopicContext unless @topic
  @exchange = @channel.topic(@topic, durable: true, persistent: true)

  # if shout_config.is_a? Hash
  #   shout_keys = shout_config[:keys] if config_shout[:keys].is_a? Array
  # end

rescue Bunny::TCPConnectionFailedForAllHosts => e
  error "ERROR: cannot connect to RabbitMQ hosts (#{e.inspect})"
end

Instance Attribute Details

#tableObject

Returns the value of attribute table.



14
15
16
# File 'lib/pushyd/shouter.rb', line 14

def table
  @table
end

Instance Method Details

#shoutObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pushyd/shouter.rb', line 50

def shout
  # Prepare exchange
  loop do
    if true # shout_exchange
      random_string = SecureRandom.hex
      random_key = @keys.sample || "random"
      channel_shout [:ping, random_key, random_string], {}
    end
    sleep 1
  end
rescue AMQ::Protocol::EmptyResponseError => e
  raise PushyDaemon::ShouterResponseError, "#{e.class} (#{e.inspect})"
rescue Bunny::ChannelAlreadyClosed => e
  raise PushyDaemon::ShouterChannelClosed, "#{e.class} (#{e.inspect})"
rescue Bunny::PreconditionFailed => e
  raise PushyDaemon::ShouterPreconditionFailed, "#{e.class} (#{e.inspect})"

rescue Interrupt => e
  @channel.close
  raise PushyDaemon::ShouterInterrupted, "#{e.class} (#{e.inspect})"
end