Class: Malevich::Monitor
- Inherits:
-
Object
- Object
- Malevich::Monitor
- Defined in:
- lib/malevich/monitor.rb
Constant Summary collapse
- CHECK_ALIVE =
5
Instance Attribute Summary collapse
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
-
#initialize ⇒ Monitor
constructor
A new instance of Monitor.
- #plugins ⇒ Object
- #run! ⇒ Object
- #run_gc? ⇒ Boolean
Constructor Details
#initialize ⇒ Monitor
Returns a new instance of Monitor.
11 12 13 |
# File 'lib/malevich/monitor.rb', line 11 def initialize @tasks = Array.new end |
Instance Attribute Details
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
9 10 11 |
# File 'lib/malevich/monitor.rb', line 9 def tasks @tasks end |
Instance Method Details
#<<(obj) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/malevich/monitor.rb', line 19 def <<(obj) return false unless obj.respond_to?(:name) || obj.respond_to?(:run!) th = Thread.new do obj.run! end log :info, "Add '#{obj.class}(#{obj.name})'" @tasks << [th, obj] end |
#plugins ⇒ Object
15 16 17 |
# File 'lib/malevich/monitor.rb', line 15 def plugins @tasks.select { |t| t[1].is_a?(Malevich::Plugin) }.map { |x| {x[1].name => x[1].settings.to_hash} } end |
#run! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/malevich/monitor.rb', line 34 def run! loop do @tasks.each_with_index do |task, i| next if task[0].alive? # start new thread log :error, "Thread for '#{task[1].class}(#{task[1].name})' is dead, start it" @tasks.delete_at(i) self << task[1] end log :debug, "Check alive #{@tasks.count} threads" ObjectSpace.garbage_collect if run_gc? sleep CHECK_ALIVE end end |
#run_gc? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/malevich/monitor.rb', line 28 def run_gc? @gc_counter ||= 0 @gc_counter += 1 @gc_counter > 100 ? @gc_counter = 0 : false end |