Class: Kurchatov::Monitor::Task

Inherits:
Object
  • Object
show all
Includes:
Kurchatov::Mixin::Event
Defined in:
lib/kurchatov/monitor.rb

Constant Summary

Constants included from Kurchatov::Mixin::Event

Kurchatov::Mixin::Event::EVENT_FIELDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Kurchatov::Mixin::Event

#event

Methods included from Kurchatov::Mixin::Queue

#events

Constructor Details

#initialize(plugin) ⇒ Task

Returns a new instance of Task.



9
10
11
12
13
14
15
16
# File 'lib/kurchatov/monitor.rb', line 9

def initialize(plugin)
  @plugin = plugin
  @thread = Thread.new { @plugin.start! }
  @count_errors = 0
  @last_error = nil
  @last_error_at = nil
  @last_error_count = 0
end

Instance Attribute Details

#count_errorsObject (readonly)

Returns the value of attribute count_errors.



7
8
9
# File 'lib/kurchatov/monitor.rb', line 7

def count_errors
  @count_errors
end

#instanceObject

Returns the value of attribute instance.



6
7
8
# File 'lib/kurchatov/monitor.rb', line 6

def instance
  @instance
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



7
8
9
# File 'lib/kurchatov/monitor.rb', line 7

def last_error
  @last_error
end

#last_error_atObject (readonly)

Returns the value of attribute last_error_at.



7
8
9
# File 'lib/kurchatov/monitor.rb', line 7

def last_error_at
  @last_error_at
end

#threadObject

Returns the value of attribute thread.



6
7
8
# File 'lib/kurchatov/monitor.rb', line 6

def thread
  @thread
end

Instance Method Details

#configObject



22
23
24
# File 'lib/kurchatov/monitor.rb', line 22

def config
  @plugin.plugin_config
end

#died?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kurchatov/monitor.rb', line 34

def died?
  if @thread.alive?
    @last_error_count = 0
    return false 
  end
  # thread died, join and extract error
  begin
    @thread.join # call error
  rescue => e
    desc = "Plugin '#{@plugin.name}' died. #{e.class}: #{e}.\n" +
      "Trace:  #{e.backtrace.join("\n")}"
    @count_errors += 1
    @last_error_count += 1
    @last_error = desc
    @last_error_at = Time.now
    Log.error(desc)
    if @plugin.ignore_errors == false || (@plugin.ignore_errors.class == 'Fixnum' && @plugin.ignore_errors > @plugin.last_error_count)
      event(:service => "plugin #{@plugin.name} errors", :desc => desc, :state => 'critical')
    end
  end
  true
end

#nameObject



18
19
20
# File 'lib/kurchatov/monitor.rb', line 18

def name
  @plugin.name
end

#start!Object



57
58
59
# File 'lib/kurchatov/monitor.rb', line 57

def start!
  @thread = Thread.new { @plugin.start! }
end

#stop!Object



26
27
28
# File 'lib/kurchatov/monitor.rb', line 26

def stop!
  Thread.kill(@thread)
end

#stopped?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/kurchatov/monitor.rb', line 30

def stopped?
  @plugin.stopped?
end