Class: ProcessBot::ControlSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/process_bot/control_socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, process:) ⇒ ControlSocket

Returns a new instance of ControlSocket.



6
7
8
9
# File 'lib/process_bot/control_socket.rb', line 6

def initialize(options:, process:)
  @options = options
  @process = process
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/process_bot/control_socket.rb', line 4

def options
  @options
end

#processObject (readonly)

Returns the value of attribute process.



4
5
6
# File 'lib/process_bot/control_socket.rb', line 4

def process
  @process
end

#serverObject (readonly)

Returns the value of attribute server.



4
5
6
# File 'lib/process_bot/control_socket.rb', line 4

def server
  @server
end

Instance Method Details

#handle_client(client) ⇒ Object

rubocop:disable Metrics/AbcSize



42
43
44
45
46
47
48
49
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
# File 'lib/process_bot/control_socket.rb', line 42

def handle_client(client) # rubocop:disable Metrics/AbcSize
  loop do
    data = client.gets
    break if data.nil? # Client disconnected

    command = JSON.parse(data)
    command_type = command.fetch("command")

    if command_type == "graceful" || command_type == "stop"
      begin
        command_options = if command["options"]
          symbolize_keys(command.fetch("options"))
        else
          {}
        end

        logger.logs "Command #{command_type} with options #{command_options}"

        process.__send__(command_type, **command_options)
        client.puts(JSON.generate(type: "success"))
      rescue => e # rubocop:disable Style/RescueStandardError
        logger.logs e.message, type: :stderr
        logger.logs e.backtrace, type: :stderr

        client.puts(JSON.generate(type: "error", message: e.message, backtrace: e.backtrace))

        raise e
      end
    else
      client.puts(JSON.generate(type: "error", message: "Unknown command: #{command_type}", backtrace: Thread.current.backtrace))
    end
  end
end

#loggerObject



11
12
13
# File 'lib/process_bot/control_socket.rb', line 11

def logger
  @logger ||= ProcessBot::Logger.new(options: options)
end

#portObject



15
16
17
# File 'lib/process_bot/control_socket.rb', line 15

def port
  options.fetch(:port).to_i
end

#run_client_loopObject



32
33
34
35
36
37
38
39
40
# File 'lib/process_bot/control_socket.rb', line 32

def run_client_loop
  Thread.new do
    client = server.accept

    Thread.new do
      handle_client(client)
    end
  end
end

#startObject



19
20
21
22
23
24
25
26
# File 'lib/process_bot/control_socket.rb', line 19

def start
  @server = TCPServer.new("localhost", port)
  run_client_loop

  logger.logs "TCPServer started"

  options.events.call(:on_socket_opened, port: port)
end

#stopObject



28
29
30
# File 'lib/process_bot/control_socket.rb', line 28

def stop
  server.close
end

#symbolize_keys(hash) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/process_bot/control_socket.rb', line 76

def symbolize_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    next if key == "port"

    new_hash[key.to_sym] = value
  end

  new_hash
end