Class: ProcessBot::ControlSocket
- Inherits:
-
Object
- Object
- ProcessBot::ControlSocket
- Defined in:
- lib/process_bot/control_socket.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #actually_start_tcp_server(host, port) ⇒ Object
-
#handle_client(client) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(options:, process:) ⇒ ControlSocket
constructor
A new instance of ControlSocket.
- #logger ⇒ Object
- #run_client_loop ⇒ Object
- #start ⇒ Object
- #start_tcp_server ⇒ Object
- #stop ⇒ Object
- #symbolize_keys(hash) ⇒ Object
Constructor Details
#initialize(options:, process:) ⇒ ControlSocket
Returns a new instance of ControlSocket.
6 7 8 9 10 |
# File 'lib/process_bot/control_socket.rb', line 6 def initialize(options:, process:) @options = @process = process @port = .fetch(:port).to_i end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/process_bot/control_socket.rb', line 4 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/process_bot/control_socket.rb', line 4 def port @port end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
4 5 6 |
# File 'lib/process_bot/control_socket.rb', line 4 def process @process end |
#server ⇒ Object (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
#actually_start_tcp_server(host, port) ⇒ Object
36 37 38 |
# File 'lib/process_bot/control_socket.rb', line 36 def actually_start_tcp_server(host, port) TCPServer.new(host, port) end |
#handle_client(client) ⇒ Object
rubocop:disable Metrics/AbcSize
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 |
# File 'lib/process_bot/control_socket.rb', line 54 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 = if command["options"] symbolize_keys(command.fetch("options")) else {} end logger.logs "Command #{command_type} with options #{}" process.__send__(command_type, **) client.puts(JSON.generate(type: "success")) rescue => e # rubocop:disable Style/RescueStandardError logger.error e. logger.error e.backtrace client.puts(JSON.generate(type: "error", message: e., 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 |
#logger ⇒ Object
12 13 14 |
# File 'lib/process_bot/control_socket.rb', line 12 def logger @logger ||= ProcessBot::Logger.new(options: ) end |
#run_client_loop ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/process_bot/control_socket.rb', line 44 def run_client_loop Thread.new do client = server.accept Thread.new do handle_client(client) end end end |
#start ⇒ Object
16 17 18 19 20 21 |
# File 'lib/process_bot/control_socket.rb', line 16 def start start_tcp_server run_client_loop logger.logs "TCPServer started" .events.call(:on_socket_opened, port: @port) end |
#start_tcp_server ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/process_bot/control_socket.rb', line 23 def start_tcp_server tries ||= 0 tries += 1 @server = actually_start_tcp_server("localhost", @port) rescue Errno::EADDRINUSE, Errno::EADDRNOTAVAIL => e if tries <= 100 @port += 1 retry else raise e end end |
#stop ⇒ Object
40 41 42 |
# File 'lib/process_bot/control_socket.rb', line 40 def stop server&.close end |
#symbolize_keys(hash) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/process_bot/control_socket.rb', line 88 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 |