Class: Daikon::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/daikon/daemon.rb

Constant Summary collapse

INFO_INTERVAL =
ENV["INFO_INTERVAL"] || 10
SUMMARY_INTERVAL =
ENV["SUMMARY_INTERVAL"] || 60

Class Method Summary collapse

Class Method Details

.runObject



18
19
20
# File 'lib/daikon/daemon.rb', line 18

def self.run
  @@run
end

.run=(run) ⇒ Object



14
15
16
# File 'lib/daikon/daemon.rb', line 14

def self.run=(run)
  @@run = run
end

.sleep_timeObject



10
11
12
# File 'lib/daikon/daemon.rb', line 10

def self.sleep_time
  @@sleep_time ||= 1
end

.sleep_time=(sleep_time) ⇒ Object



6
7
8
# File 'lib/daikon/daemon.rb', line 6

def self.sleep_time=(sleep_time)
  @@sleep_time = sleep_time
end

.start(argv, ontop = false) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/daikon/daemon.rb', line 22

def self.start(argv, ontop = false)
  self.run = true
  config = Daikon::Configuration.new(argv)

  if argv.include?("-v") || argv.include?("--version")
    puts "Daikon v#{VERSION}"
    return
  end

  Daemons.run_proc("daikon", :ARGV => argv, :log_output => true, :backtrace => true, :ontop => ontop) do
    if argv.include?("run")
      logger = Logger.new(STDOUT)
    else
      logger = Logger.new("/tmp/radish.log")
    end

    if port = ENV["PORT"]
      logger.info "Port detected, starting server on port #{port}"
      Daikon::Server.start(logger, port)
    end

    rotated_at = reported_at = Time.now
    client = Daikon::Client.new

    client.setup(config, logger)
    client.start_monitor

    while self.run do
      now = Time.now

      if now - reported_at >= sleep_time * INFO_INTERVAL.to_i
        client.report_info
        reported_at = now
      end

      if now - rotated_at >= sleep_time * SUMMARY_INTERVAL.to_i
        client.rotate_monitor(rotated_at, now)
        rotated_at = now
      end

      sleep sleep_time
    end
  end
end