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



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

def initialize(config)
  @config = config
  initialize_watchers
end

Class Method Details

.keep_running?Boolean



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

def keep_running?
  @@keep_running
end

.queue_prefixObject



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

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

Instance Method Details

#configObject



67
68
69
# File 'lib/bender/client.rb', line 67

def config
  @config
end

#publish(watcher, message) ⇒ Object



60
61
62
63
64
65
# File 'lib/bender/client.rb', line 60

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

#start_watchersObject



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

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



54
55
56
57
58
# File 'lib/bender/client.rb', line 54

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

#trap_signalsObject



32
33
34
35
36
37
38
39
# File 'lib/bender/client.rb', line 32

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

#watchersObject



71
72
73
# File 'lib/bender/client.rb', line 71

def watchers
  @watchers
end