Class: OsCtl::Cli::Top::Monitor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Monitor

Returns a new instance of Monitor.



3
4
5
6
7
# File 'lib/osctl/cli/top/monitor.rb', line 3

def initialize(model)
  @model = model
  @started = false
  @buffer = []
end

Instance Attribute Details

#client ⇒ Object (readonly, protected)

Returns the value of attribute client.



39
40
41
# File 'lib/osctl/cli/top/monitor.rb', line 39

def client
  @client
end

#model ⇒ Object (readonly, protected)

Returns the value of attribute model.



39
40
41
# File 'lib/osctl/cli/top/monitor.rb', line 39

def model
  @model
end

#thread ⇒ Object (readonly, protected)

Returns the value of attribute thread.



39
40
41
# File 'lib/osctl/cli/top/monitor.rb', line 39

def thread
  @thread
end

Instance Method Details

#monitor_loop ⇒ Object (protected)



41
42
43
44
45
46
47
48
# File 'lib/osctl/cli/top/monitor.rb', line 41

def monitor_loop
  loop do
    resp = client.response!
    return if resp.data.nil?

    return if yield(resp.data) == :stop
  end
end

#process_event(type, opts) ⇒ Object (protected)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/osctl/cli/top/monitor.rb', line 50

def process_event(type, opts)
  return if type == :ct_netif && !model.netif_enabled?

  case type
  when :state
    model.sync do
      ct = model.find_ct(opts[:pool], opts[:id])
      next unless ct

      st = opts[:state].to_sym

      ct.state = st
      ct.init_pid = opts[:init_pid] if st == :running
      model.changed!
    end

  when :db
    if opts[:object] == 'pool'
      case opts[:action].to_sym
      when :add
        unless model.has_pool?(opts[:pool])
          model.add_pool(opts[:pool])
        end

      when :remove
        model.remove_pool(opts[:pool])
      end

    elsif opts[:object] == 'container'
      case opts[:action].to_sym
      when :add
        unless model.has_ct?(opts[:pool], opts[:id])
          model.add_ct(opts[:pool], opts[:id])
        end

      when :remove
        ct = model.find_ct(opts[:pool], opts[:id])
        model.remove_ct(ct) if ct
      end
    end

  when :ct_scheduled
    model.sync do
      ct = model.find_ct(opts[:pool], opts[:id])
      next unless ct

      ct.cpu_package_inuse = opts[:cpu_package_inuse]
      model.changed!
    end

  when :ct_init_pid
    model.sync do
      ct = model.find_ct(opts[:pool], opts[:id])
      next unless ct

      ct.init_pid = opts[:init_pid]
      model.changed!
    end

  when :ct_netif
    if opts[:action] == 'add'
      ct = model.find_ct(opts[:pool], opts[:id])
      model.add_ct_netif(ct, opts[:name]) if ct && !ct.has_netif?(opts[:name])
      return
    end
    model.sync do
      ct = model.find_ct(opts[:pool], opts[:id])
      next unless ct

      case opts[:action].to_sym
      when :remove
        ct.netif_rm(opts[:name]) if ct.has_netif?(opts[:name])

      when :rename
        if ct.has_netif?(opts[:name])
          ct.netif_rename(opts[:name], opts[:new_name])
        end

      when :up
        if ct.has_netif?(opts[:name])
          ct.netif_up(opts[:name], opts[:veth])
        end

      when :down
        if ct.has_netif?(opts[:name])
          ct.netif_down(opts[:name])
        end
      end
      model.changed!
    end
  end
end

#process_saved_events ⇒ Object (protected)



149
150
151
152
153
154
# File 'lib/osctl/cli/top/monitor.rb', line 149

def process_saved_events
  return if @buffer.empty?

  @buffer.each { |event| process_event(event[:type].to_sym, event[:opts]) }
  @buffer.clear
end

#save_event(event) ⇒ Object (protected)



143
144
145
146
147
# File 'lib/osctl/cli/top/monitor.rb', line 143

def save_event(event)
  return if event[:type].to_s == 'ct_netif' && !model.netif_enabled?

  @buffer << event
end

#start ⇒ Object



28
29
30
# File 'lib/osctl/cli/top/monitor.rb', line 28

def start
  @started = true
end

#started? ⇒ Boolean (protected)

Returns:

  • (Boolean)


156
157
158
# File 'lib/osctl/cli/top/monitor.rb', line 156

def started?
  @started
end

#stop ⇒ Object



32
33
34
35
# File 'lib/osctl/cli/top/monitor.rb', line 32

def stop
  client.close
  thread.join
end

#subscribe ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osctl/cli/top/monitor.rb', line 9

def subscribe
  @client = OsCtl::Client.new
  client.open

  ret = client.cmd_data!(:event_subscribe)
  return if ret != 'subscribed'

  @thread = Thread.new do
    monitor_loop do |event|
      if started?
        process_saved_events
        process_event(event[:type].to_sym, event[:opts])
      else
        save_event(event)
      end
    end
  end
end