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

#clientObject (readonly, protected)

Returns the value of attribute client.



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

def client
  @client
end

#modelObject (readonly, protected)

Returns the value of attribute model.



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

def model
  @model
end

#threadObject (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_loopObject (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
# File 'lib/osctl/cli/top/monitor.rb', line 50

def process_event(type, opts)
  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
    end

  when :db
    model.sync do
      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
    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]
    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]
    end

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

      case opts[:action].to_sym
      when :add
        model.add_ct_netif(ct, opts[:name]) unless ct.has_netif?(opts[:name])

      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
    end
  end
end

#process_saved_eventsObject (protected)



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

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)



137
138
139
# File 'lib/osctl/cli/top/monitor.rb', line 137

def save_event(event)
  @buffer << event
end

#startObject



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

def start
  @started = true
end

#started?Boolean (protected)

Returns:

  • (Boolean)


148
149
150
# File 'lib/osctl/cli/top/monitor.rb', line 148

def started?
  @started
end

#stopObject



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

def stop
  client.close
  thread.join
end

#subscribeObject



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