Class: OsCtl::Cli::Top::Tui::ModelThread

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/cli/top/tui/model_thread.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, rate) ⇒ ModelThread

Returns a new instance of ModelThread.



14
15
16
17
18
19
20
21
22
23
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 14

def initialize(model, rate)
  @model = model
  @rate = rate
  @last_measurement = nil
  @generation = 0
  @mode = model.mode
  @data = { containers: [] }
  @queue = OsCtl::Lib::Queue.new
  @mutex = Mutex.new
end

Instance Attribute Details

#generationInteger (readonly)

Returns:

  • (Integer)


9
10
11
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 9

def generation
  @generation
end

#last_measurementTime? (readonly)

Returns:

  • (Time, nil)


6
7
8
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 6

def last_measurement
  @last_measurement
end

#mode:realtime, :cumulative

Returns:

  • (:realtime, :cumulative)


12
13
14
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 12

def mode
  @mode
end

Instance Method Details

#containersObject



49
50
51
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 49

def containers
  @model.containers
end

#get_dataObject



34
35
36
37
38
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 34

def get_data
  @mutex.synchronize do
    [@data, @last_measurement, @generation]
  end
end

#iostat_enabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 45

def iostat_enabled?
  @model.iostat_enabled?
end

#measure(mode: nil) ⇒ Object (protected)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 72

def measure(mode: nil)
  @model.measure

  @mutex.synchronize do
    @last_measurement = Time.now

    if mode
      @model.mode = mode
      @mode = mode
    end

    @data = @model.data
    @generation += 1
  end
end

#startObject



25
26
27
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 25

def start
  @thread = Thread.new { work }
end

#stopObject



29
30
31
32
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 29

def stop
  @queue << [:stop]
  @thread.join
end

#workObject (protected)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osctl/cli/top/tui/model_thread.rb', line 55

def work
  measure

  loop do
    cmd, *args = @queue.pop(timeout: @rate)
    return if cmd == :stop

    kwargs = {}

    if cmd == :mode
      kwargs[:mode] = args[0]
    end

    measure(**kwargs)
  end
end