Class: OsCtld::Commands::Event::Subscribe

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/commands/event/subscribe.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctld/commands/event/subscribe.rb', line 9

def execute
  log(:info, :eventd, 'Subscribing client')
  queue = Eventd.subscribe
  client_handler.reply_ok('subscribed')

  opts.delete(:cli)

  loop do
    event = queue.pop(timeout: 0.2)

    if event.nil?
      if @do_stop
        Eventd.unsubscribe(queue)
        return error('osctld is shutting down')
      end
    elsif !filter?(event)
      next
    elsif !client_handler.reply_ok(export_event(event))
      break
    end
  end

  log(:info, :eventd, 'Unsubscribing client')
  Eventd.unsubscribe(queue)
  ok
end

#export_event(e) ⇒ Object (protected)



65
66
67
68
69
70
# File 'lib/osctld/commands/event/subscribe.rb', line 65

def export_event(e)
  {
    type: e.type,
    opts: e.opts
  }
end

#filter?(event) ⇒ Boolean (protected)

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/osctld/commands/event/subscribe.rb', line 42

def filter?(event)
  if opts[:type]
    if opts[:type].is_a?(Array)
      return false if opts[:type].include?(event.type)
    elsif opts[:type] != event.type.to_s
      return false
    end
  end

  if opts[:opts]
    opts[:opts].each do |k, v|
      if v.is_a?(Array)
        return false unless v.include?(event.opts[k])

      elsif v != event.opts[k]
        return false
      end
    end
  end

  true
end

#request_stopObject



36
37
38
# File 'lib/osctld/commands/event/subscribe.rb', line 36

def request_stop
  @do_stop = true
end