Class: PushyDaemon::Shouter

Inherits:
BmcDaemonLib::MqEndpoint
  • Object
show all
Includes:
LoggerHelper, NewRelic::Agent::Instrumentation::ControllerInstrumentation
Defined in:
lib/pushyd/shouter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, config_shout) ⇒ 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
# File 'lib/pushyd/shouter.rb', line 16

def initialize(channel, config_shout)
  # Init MqConsumer
  log_pipe :shouter
  super

  # Init
  @shouter_keys = []

  # Check config
  unless config_shout && config_shout.any? && config_shout.is_a?(Enumerable)
    log_error "prepare: empty [shout] section"
    return
  end

  # Extract information
  @shouter_keys = config_shout[:keys] if config_shout[:keys].is_a? Array
  @shouter_topic = config_shout[:topic]
  @shouter_period = config_shout[:period].to_f
  @shouter_period = 1 unless (@shouter_period > 0)

  fail PushyDaemon::EndpointTopicContext unless @shouter_topic

  # Create exchange
  @exchange = @channel.topic(@shouter_topic, durable: true, persistent: true)
  #log_info "channel[#{@channel.id}] created, prefetch[#{AMQP_PREFETCH}]"

  # Start working, now
  log_info "shouter initialized"
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/pushyd/shouter.rb', line 11

def logger
  @logger
end

#tableObject

Class options



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

def table
  @table
end

Instance Method Details

#start_loopObject



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

def start_loop
  log_info "shouter start_loop", { topic: @shouter_topic, period: @shouter_period, keys: @shouter_keys }

  # Prepare exchange
  loop do
    # Generate payload
    payload = {time: Time.now.to_f, host: BmcDaemonLib::Conf.host}
    # payload = nil

    # Shout it !
    exchange_shout @exchange, payload
    sleep @shouter_period
  end
rescue AMQ::Protocol::EmptyResponseError => e
  fail PushyDaemon::ShouterResponseError, "#{e.class} (#{e.inspect})"
rescue Bunny::ChannelAlreadyClosed => e
  fail PushyDaemon::ShouterChannelClosed, "#{e.class} (#{e.inspect})"
rescue Bunny::PreconditionFailed => e
  fail PushyDaemon::ShouterPreconditionFailed, "#{e.class} (#{e.inspect})"
rescue Interrupt => e
  @channel.close
  fail PushyDaemon::ShouterInterrupted, e.class
end