Class: Bender::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bender/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



36
37
38
39
# File 'lib/bender/client.rb', line 36

def initialize(config)
  @config = config
  initialize_watchers
end

Class Method Details

.keep_running?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/bender/client.rb', line 19

def keep_running?
  @@keep_running
end

.queue_prefixObject



23
24
25
# File 'lib/bender/client.rb', line 23

def queue_prefix
  @@queue_prefix ||= "#{ENV['QUEUE_PREFIX']}-#{Socket.gethostname}"
end

.sqsObject



27
28
29
30
31
32
33
# File 'lib/bender/client.rb', line 27

def sqs
  @@sqs ||= AWS::SQS.new({
    :access_key_id => ENV['AWS_ACCESS_KEY'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS'],
    :region => ENV['AWS_REGION']
  })
end

Instance Method Details

#configObject



82
83
84
# File 'lib/bender/client.rb', line 82

def config
  @config
end

#publish(watcher, message, ack = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bender/client.rb', line 69

def publish(watcher, message, ack = false)
  sent = @watchers.select{|w| w.class.to_s.underscore == watcher}.collect do |watcher|
    if ack
      with_confirmation do |cq|
        watcher.publish(message, cq)
      end
    else
      watcher.publish(message)
    end
  end
  Bender.logger.info("Sent #{sent.size} message(s)")
end

#start_watchersObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bender/client.rb', line 50

def start_watchers
  Bender.logger.info("Starting Watchers - Press Ctrl+C to stop watchers...")
  @@keep_running = true
  @threads = []
  @watchers.each do |watcher|
    @threads << Thread.new do
      watcher.start
    end
  end
  trap_signals
  ThreadsWait.all_waits(*@threads)
end

#stop_watchersObject



63
64
65
66
67
# File 'lib/bender/client.rb', line 63

def stop_watchers
  puts "Stopping watchers..."
  @@keep_running = false
  ThreadsWait.all_waits(*@threads)
end

#trap_signalsObject



41
42
43
44
45
46
47
48
# File 'lib/bender/client.rb', line 41

def trap_signals
  %w{INT TERM}.each do |signal|
    Signal.trap(signal) {
      stop_watchers
      exit
    }
  end
end

#watchersObject



86
87
88
# File 'lib/bender/client.rb', line 86

def watchers
  @watchers
end