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



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

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

Instance Attribute Details

#clientObject (readonly, protected)

Returns the value of attribute client



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

def client
  @client
end

#modelObject (readonly, protected)

Returns the value of attribute model



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

def model
  @model
end

#threadObject (readonly, protected)

Returns the value of attribute thread



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

def thread
  @thread
end

Instance Method Details

#monitor_loopObject (protected)



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

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)



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

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

      ct.state = opts[:state].to_sym
    end

  when :db
    return if opts[:object] != 'container'

    model.sync do
      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_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)



112
113
114
115
116
# File 'lib/osctl/cli/top/monitor.rb', line 112

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)



108
109
110
# File 'lib/osctl/cli/top/monitor.rb', line 108

def save_event(event)
  @buffer << event
end

#startObject



30
31
32
# File 'lib/osctl/cli/top/monitor.rb', line 30

def start
  @started = true
end

#started?Boolean (protected)

Returns:

  • (Boolean)


118
119
120
# File 'lib/osctl/cli/top/monitor.rb', line 118

def started?
  @started
end

#stopObject



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

def stop
  client.close
  thread.join
end

#subscribeObject



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

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