Class: Nodectl::Stream::Websocket

Inherits:
Object
  • Object
show all
Defined in:
lib/nodectl/stream/websocket.rb

Constant Summary collapse

URI_LOG =
/\A\/logs\/(?<service_name>[\w_-]+)\/(?<log_name>\w+)\Z/
URI_ACTION =
/\A\/actions\/(?<action_id>\w+)\Z/
URI_EVENTS =
/\A\/events\Z/

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Websocket

Returns a new instance of Websocket.



6
7
8
9
10
11
12
13
# File 'lib/nodectl/stream/websocket.rb', line 6

def initialize(host, port)
  EM::WebSocket.run(host: host, port: port) do |ws|
    Nodectl.logger.info("websocket: socket created")
    ws.onopen { |handshake| connection_open(ws, handshake) }
  end

  callbacks_init
end

Instance Method Details

#callbacks_initObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nodectl/stream/websocket.rb', line 30

def callbacks_init
  EM.add_periodic_timer(0.5, method(:tick))

  Nodectl::Instance.onadd do |instance|
    Nodectl::Stream::EventsSession.publish('instance', instance.pid, 'started')
  end

  Nodectl::Instance.ondelete do |instance, options|
    Nodectl::Stream::EventsSession.publish('instance', instance.pid, 'stopped', options)
  end

  Nodectl::Action.onadd do |action|
    Nodectl::Stream::EventsSession.publish('action', action.id, 'started')
  end

  Nodectl::Action.onkill do |action, options|
    Nodectl::Stream::EventsSession.publish('action', action.id, 'stopped', options)
  end
end

#connection_open(ws, handshake) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nodectl/stream/websocket.rb', line 15

def connection_open(ws, handshake)
  Nodectl.logger.info("websocket: connection opened with url '#{handshake.path}'")

  case handshake.path
  when URI_LOG
    create_log_session(ws, handshake)
  when URI_ACTION
    create_action_session(ws, handshake)
  when URI_EVENTS
    create_events_session(ws, handshake)
  else
    ws.close
  end
end