Class: HerokuMongoWatcher::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku_mongo_watcher/cli.rb

Class Method Summary collapse

Class Method Details

.autoscaleObject



80
81
82
83
84
85
86
# File 'lib/heroku_mongo_watcher/cli.rb', line 80

def self.autoscale
  new_dyno_level = autoscaler.scale(@current_row)
  if new_dyno_level
    puts "! Scaling -> #{new_dyno_level}"
    mailer.notify(@current_row, "Scaling to #{new_dyno_level}")
  end
end

.autoscalerObject



18
19
20
# File 'lib/heroku_mongo_watcher/cli.rb', line 18

def self.autoscaler
  HerokuMongoWatcher::Autoscaler.instance
end

.check_and_notifyObject



112
113
114
115
# File 'lib/heroku_mongo_watcher/cli.rb', line 112

def self.check_and_notify
  check_and_notify_locks
  check_and_notify_response_time
end

.configObject



10
11
12
# File 'lib/heroku_mongo_watcher/cli.rb', line 10

def self.config
  HerokuMongoWatcher::Configuration.instance.config
end

.herokuObject



22
23
24
# File 'lib/heroku_mongo_watcher/cli.rb', line 22

def self.heroku
  @heroku || Heroku::Client.new(config[:heroku_username], config[:heroku_password])
end

.mailerObject



14
15
16
# File 'lib/heroku_mongo_watcher/cli.rb', line 14

def self.mailer
  HerokuMongoWatcher::Mailer.instance
end

.periodicly_capture_heroku_psObject



88
89
90
91
92
93
94
95
96
# File 'lib/heroku_mongo_watcher/cli.rb', line 88

def self.periodicly_capture_heroku_ps
  while true do
    results = heroku.ps(config[:heroku_appname])
    dynos = results.select { |ps| ps['process'] =~ /^web./ && ps['state'] == 'up' }.count

    @mutex.synchronize { @current_row.dynos = dynos }
    sleep(30)
  end
end

.tail_and_process_herokuObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/heroku_mongo_watcher/cli.rb', line 98

def self.tail_and_process_heroku
  heroku.read_logs(config[:heroku_appname], ['tail=1']) do |chunk|
    unless chunk.empty?
      chunk.split("\n").each do |line|
        @mutex.synchronize do
          @current_row.process_heroku_router_line(line) if line.include? 'heroku[router]'
          @current_row.process_heroku_web_line(line) if line.include? 'app[web'
        end
      end
    end

  end
end

.watchObject



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
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/heroku_mongo_watcher/cli.rb', line 26

def self.watch
  Thread.abort_on_exception = true

  # lock warnings flags
  @lock_critical_notified = false
  @lock_warning_notified = false

  # average response time warning flags
  @art_critical_notified = false
  @art_warning_notified = false

  @current_row = HerokuMongoWatcher::DataRow.new
  @last_row = HerokuMongoWatcher::DataRow.new

  @mutex = Mutex.new

  # Let people know that its on, and confirm emails are being received
  mailer.notify(@current_row, "Mongo Watcher enabled!")

  # Call Tails heroku logs updates counts
  heroku_watcher = Thread.new('heroku_logs') { tail_and_process_heroku }

  # Call Heroku PS to check the number of up dynos
  heroku_ps = Thread.new('heroku_ps') { periodicly_capture_heroku_ps }

  HerokuMongoWatcher::DataRow.print_header

  IO.popen("mongostat --rowcount 0 #{config[:interval]} --host #{config[:mongo_host]} --username #{config[:mongo_username]} --password #{config[:mongo_password]} --noheaders") do |f|
    while line = f.gets
      next unless line =~ /^/ && !(line =~ /^connected/)
      @mutex.synchronize do
        @current_row.process_mongo_line(line)

        @current_row.print_row

        check_and_notify

        autoscale if config[:autoscale]

        @last_row = @current_row
        @current_row = HerokuMongoWatcher::DataRow.new
        @current_row.dynos = @last_row.dynos

      end
    end
  end

  heroku_watcher.join
  heroku_ps.join

rescue Interrupt
  puts "\nexiting ..."
end