Class: OsCtld::Eventd::Manager
- Inherits:
-
Object
- Object
- OsCtld::Eventd::Manager
- Defined in:
- lib/osctld/eventd/manager.rb
Overview
Event pub/sub server
This class aggregates events from osctld and announces them to subscribed clients.
Subscribing
Clients can subscribe by calling #subscribe, which returns a queue over which the events will be sent. To unsubscribe, call ##unsubscribe with the queue as an argument. An instance of Event is sent over the queue for every reported event.
Announcing events
Events are announced using method #report. Events are classified by a ‘type` and described by `opts`. For possible event types, see below.
Event types
‘:management`
Used for management commands received over the command socket. Options:
{
id: unique internal command identifier,
cmd: management command,
opts: command options,
state: :run | :done | :failed
}
‘:osctld_shutdown`
Sent when osctld is shutting down.
‘:state`
Used to report changes of container states. Options:
{
pool: pool name,
id: container id,
state: new state
}
‘:state_recovery`
Used to report container state when the administrator recovered it. Options:
{
pool: pool name,
id: container id,
state: new state
}
‘:db`
Reports about changes in osctld database. Options:
{
object: pool/user/group/container,
pool: object's pool name,
id: object identificator,
action: add/remove
}
‘:ct_scheduled`
Reports actions by the CPU scheduler Options:
{
pool: pool name,
id: container id,
cpu_package_inuse: package id
}
‘:ct_init_pid`
Reports discovery of container init PID Options:
{
pool: pool name,
id: container id,
init_pid: init PID
}
‘:ct_netif`
Reports about container networking interfaces being added, deleted or coming up and down.
Options:
{
pool: container's pool name,
id: container id,
action: add/remove/rename/up/down,
name: interface name inside the container,
new_name: net interface name when action is rename,
veth: interface name on the host
}
‘:ct_exit`
Sent when container exits from the inside on its own, e.g. when halt/reboot is run.
Options:
{
pool: pool name,
id: container id,
exit_type: halt or reboot
}
Instance Method Summary collapse
- #default_worker_count ⇒ Object protected
- #get_worker ⇒ Object protected
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
-
#report(type, **opts) ⇒ Object
Report an event that should be announced to all subscribers.
- #shutdown ⇒ Object
- #start(num_workers: nil) ⇒ Object
- #stop ⇒ Object
-
#subscribe ⇒ OsCtl::Lib::Queue
Subscribe a client to all events.
-
#unsubscribe(queue) ⇒ Object
Unsubscribe client represented by ‘queue`.
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
113 114 115 |
# File 'lib/osctld/eventd/manager.rb', line 113 def initialize @workers = [] end |
Instance Method Details
#default_worker_count ⇒ Object (protected)
186 187 188 |
# File 'lib/osctld/eventd/manager.rb', line 186 def default_worker_count [Etc.nprocessors / 32, 2].max end |
#get_worker ⇒ Object (protected)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/osctld/eventd/manager.rb', line 168 def get_worker ret = nil min_size = nil @workers.each do |w| w_size = w.size if min_size.nil? || w_size < min_size ret = w min_size = w_size end end raise 'programming error: no worker found' if ret.nil? ret end |
#report(type, **opts) ⇒ Object
Report an event that should be announced to all subscribers
160 161 162 163 164 |
# File 'lib/osctld/eventd/manager.rb', line 160 def report(type, **opts) event = Eventd::Event.new(type, opts) @workers.each { |w| w.report(event) } nil end |
#shutdown ⇒ Object
136 137 138 139 140 |
# File 'lib/osctld/eventd/manager.rb', line 136 def shutdown report(:osctld_shutdown) stop nil end |
#start(num_workers: nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/osctld/eventd/manager.rb', line 118 def start(num_workers: nil) num_workers ||= default_worker_count @workers.clear num_workers.times.each do w = Eventd::Worker.new w.start @workers << w end nil end |
#stop ⇒ Object
131 132 133 134 |
# File 'lib/osctld/eventd/manager.rb', line 131 def stop @workers.each(&:stop) nil end |
#subscribe ⇒ OsCtl::Lib::Queue
Subscribe a client to all events
144 145 146 147 148 |
# File 'lib/osctld/eventd/manager.rb', line 144 def subscribe q = OsCtl::Lib::Queue.new get_worker.subscribe(q) q end |
#unsubscribe(queue) ⇒ Object
Unsubscribe client represented by ‘queue`
152 153 154 155 |
# File 'lib/osctld/eventd/manager.rb', line 152 def unsubscribe(queue) @workers.each { |w| w.unsubscribe(queue) } nil end |