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.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#handle_client(client) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(options:, process:) ⇒ ControlSocket
constructor
A new instance of ControlSocket.
- #port ⇒ Object
- #run_client_loop ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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 = @process = process 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 |
#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
#handle_client(client) ⇒ Object
rubocop:disable Metrics/AbcSize
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/process_bot/control_socket.rb', line 38 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 process.__send__(command_type) client.puts(JSON.generate(type: "success")) rescue => e # rubocop:disable Style/RescueStandardError client.puts(JSON.generate(type: "error", message: e.)) raise e end else client.puts(JSON.generate(type: "error", message: "Unknown command: #{command_type}")) end end end |
#port ⇒ Object
11 12 13 |
# File 'lib/process_bot/control_socket.rb', line 11 def port .fetch(:port).to_i end |
#run_client_loop ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/process_bot/control_socket.rb', line 28 def run_client_loop Thread.new do client = server.accept Thread.new do handle_client(client) end end end |
#start ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/process_bot/control_socket.rb', line 15 def start @server = TCPServer.new("localhost", port) run_client_loop puts "TCPServer started" .events.call(:on_socket_opened, port: port) end |
#stop ⇒ Object
24 25 26 |
# File 'lib/process_bot/control_socket.rb', line 24 def stop server.close end |