aboutsummaryrefslogtreecommitdiff
path: root/lib/canary.rb
blob: afe3a16d6bd0775b8ed64de52f5ffdff8661a9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
# encoding: UTF-8
require 'logger'

# A little canary class to make sure that threads are are overloaded.
class Canary

  # Accessor.
  attr_reader :sleep_time

  # Accessor.
  attr_reader :threshold

  # Default constructor.
  def initialize (st=1, log=nil)
    if Float != st.class and Fixnum != st.class
      raise ArgumentError.new(
        "Expected either Fixnum or Float for time to sleep, got #{st.class}.") 
    end
    @sleep_time = st
    @threshold = (0.05 * @sleep_time) + @sleep_time
    @logger = log
  end

  # Runs the check.
  def run
    loop do
      self.do_test()
    end
  end

  def do_test
    time_start = Time.now
    sleep(@sleep_time)
    time_end = Time.now
    time_elapsed = (time_end - time_start).abs
    if @threshold < time_elapsed
      @logger.fatal("Time elapsed is #{time_elapsed} > #{@threshold} therefore Canary is dead.")
      return false
    else
      @logger.debug("Time elapsed is #{time_elapsed} < #{@threshold} therefore Canary is alive.")
      return true
    end
  end

  # Starts a canary in a thread.
  def self.start (st=1, log=nil)
    #Thread.abort_on_exception = true
    thr = Thread.new() do
      Thread.current[:name] = "Canary Thread"
      twiti = Canary.new(st, log)
      twiti.run()
    end
  end

end