Class: AlertMachine::RunTask

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

Defined Under Namespace

Classes: AssertionFailure

Instance Method Summary collapse

Constructor Details

#initialize(opts, block, caller) ⇒ RunTask

Returns a new instance of RunTask.



4
5
6
7
8
# File 'lib/run_task.rb', line 4

def initialize(opts, block, caller)
  @opts, @block, @caller = opts, block, caller
  @errors = []
  @alert_state = false
end

Instance Method Details

#alert_state(firing) ⇒ Object

Is the alert firing?



64
65
66
67
68
69
70
# File 'lib/run_task.rb', line 64

def alert_state(firing)
  if firing != @alert_state
    mail unless @last_mailed && @last_mailed > Time.now - 60*10 && firing
    @last_mailed = Time.now
  end
  @alert_state = firing
end

#assert(condition, msg, caller) ⇒ Object



52
53
54
55
# File 'lib/run_task.rb', line 52

def assert(condition, msg, caller)
  return if condition
  assert_failed(msg, caller)
end

#assert_failed(msg, caller) ⇒ Object



57
58
59
60
61
# File 'lib/run_task.rb', line 57

def assert_failed(msg, caller)
  fail = AssertionFailure.new(msg, caller)
  puts fail.log
  raise fail
end

#configObject



94
# File 'lib/run_task.rb', line 94

def config; AlertMachine.config; end

#dont_check_long_processes?Boolean

Returns:

  • (Boolean)


92
# File 'lib/run_task.rb', line 92

def dont_check_long_processes?; opts(:dont_check_long_processes, false).to_s == "true"; end

#intervalObject



86
# File 'lib/run_task.rb', line 86

def interval; opts(:interval, 5 * 60).to_f; end

#interval_errorObject



88
# File 'lib/run_task.rb', line 88

def interval_error; opts(:interval_error) { interval / 5.0 }.to_f; end

#mailObject



72
73
74
75
76
77
78
79
80
# File 'lib/run_task.rb', line 72

def mail
  last = @errors[-1]
  ActionMailer::Base.mail(
    :from => opts(:from),
    :to => opts(:to),
    :subject => "AlertMachine Failed: #{last.msg || last.parsed_caller.file_line}",
    :body => @errors.collect {|e| e.log}.join("\n=============\n")
  ).deliver
end

#opts(key, defaults = nil) ⇒ Object



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

def opts(key, defaults = nil)
  @opts[key] || config[key.to_s] || defaults || block_given? && yield
end

#retriesObject



90
# File 'lib/run_task.rb', line 90

def retries; opts(:retries, 1).to_i; end

#scheduleObject



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
# File 'lib/run_task.rb', line 10

def schedule
  @timer = EM::PeriodicTimer.new(interval) do
    with_task do
      start = Time.now
      begin
        # The main call to the user-defined watcher function.
        @block.call(*@opts[:args])

        assert(Time.now - start < interval / 5.0,
          "Task ran for too long. Invoked every #{
          interval}s. Ran for #{Time.now - start}s.", @caller) unless
          dont_check_long_processes?

        # Things finished successfully.
        @timer.interval = interval if !@errors.empty?
        @errors = []

        alert_state(false)

      rescue Exception => af
        unless af.is_a?(AssertionFailure)
          puts "Task Exception: #{af.to_s}"
          puts "#{af.backtrace.join("\n")}"
          af = AssertionFailure.new(af.to_s, af.backtrace)
        end

        @timer.interval = interval_error if @errors.empty?
        @errors << af

        alert_state(true) if @errors.length > retries
      end
    end
  end
end

#with_taskObject



45
46
47
48
49
50
# File 'lib/run_task.rb', line 45

def with_task
  AlertMachine.current_task = self
  yield
ensure
  AlertMachine.current_task = nil
end