Class: Pkg_noisrev::Spectator

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

Instance Method Summary collapse

Constructor Details

#initialize(thread_pool, total, sec, stream = $stdout) ⇒ Spectator

Returns a new instance of Spectator.



4
5
6
7
8
9
10
11
12
13
# File 'lib/pkg_noisrev/threads.rb', line 4

def initialize(thread_pool, total, sec, stream = $stdout)
  @thread_pool = thread_pool
  @total = total
  @sec = sec
  
  @stream = stream
  @tty = stream.tty? ? true : false

  @alarm = nil
end

Instance Method Details

#alarmObject

Return a tread which will dump a statistics every @sec to a



40
41
42
43
44
45
46
47
48
49
# File 'lib/pkg_noisrev/threads.rb', line 40

def alarm
  @alarm = Thread.new(@thread_pool, @total, @sec) { |tp, total, s|
    Thread.current.abort_on_exception = true
    loop {
      draw tp, total
      break if alldone?
      sleep s
    }
  }
end

#alarm_finishObject



51
52
53
54
55
56
57
# File 'lib/pkg_noisrev/threads.rb', line 51

def alarm_finish
  if @alarm
    @alarm.join
    draw @thread_pool, @total
    @stream.print "\n"
  end
end

#alldone?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
# File 'lib/pkg_noisrev/threads.rb', line 64

def alldone?
#      puts ""
#      @thread_pool.each {|i| puts "#{i}=#{i.status}" }
  @thread_pool.each {|i|
    # thread status: false if terminated normally, nil if with exception
    return false if (i.status != nil && i.status != false)
  }
  true
end

#done_right?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/pkg_noisrev/threads.rb', line 59

def done_right?
  return false if (@thread_pool.inject(0) {|sum, i| sum+i.stat.ok }) == 0
  true
end

#draw(thread_pool, total) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pkg_noisrev/threads.rb', line 20

def draw(thread_pool, total)
  stat = ''
  processed = 0
  okays = 0
  failures = 0
  
  thread_pool.each {|i|
    stat += " | %d: %d/%d" % [i.stat.nthread, i.stat.ok, i.stat.failed]
    okays += i.stat.ok
    failures += i.stat.failed
    processed = okays + failures
  }
  
  # left%/processed/okays/failures
  @stream.print '%d%% %d/%d/%d' % [((processed.to_f/total)*100).round(2), processed, okays, failures] + stat
  flush_line
end

#flush_lineObject



15
16
17
18
# File 'lib/pkg_noisrev/threads.rb', line 15

def flush_line
  @tty ? @stream.print("\r") : @stream.print("\n")
  @stream.flush
end