Class: Encom::Transport::Stdio
- Inherits:
-
Object
- Object
- Encom::Transport::Stdio
- Defined in:
- lib/encom/transport/stdio.rb
Instance Attribute Summary collapse
-
#process_pid ⇒ Object
readonly
Returns the value of attribute process_pid.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(command:, args: []) ⇒ Stdio
constructor
A new instance of Stdio.
- #on_close(&block) ⇒ Object
- #on_data(&block) ⇒ Object
-
#on_error(&block) ⇒ Object
Register event handlers.
- #send(data) ⇒ Object
- #send_line(data) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(command:, args: []) ⇒ Stdio
Returns a new instance of Stdio.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/encom/transport/stdio.rb', line 13 def initialize( command:, args: [] ) super() @command = command @args = args @process = nil @read_buffer = +'' # Use mutable string @callbacks = { error: [], close: [], data: [] } @mutex = Mutex.new @json_buffer = '' # Buffer for accumulating JSON messages end |
Instance Attribute Details
#process_pid ⇒ Object (readonly)
Returns the value of attribute process_pid.
11 12 13 |
# File 'lib/encom/transport/stdio.rb', line 11 def process_pid @process_pid end |
Instance Method Details
#close ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/encom/transport/stdio.rb', line 90 def close return unless @process begin @stdin.close rescue StandardError nil end begin Timeout.timeout(2) do Process.wait(@process.pid) rescue StandardError nil end rescue Timeout::Error begin Process.kill('TERM', @process.pid) rescue StandardError nil end begin Timeout.timeout(1) do Process.wait(@process.pid) rescue StandardError nil end rescue Timeout::Error begin Process.kill('KILL', @process.pid) rescue StandardError nil end end end # Clean up resources begin @stdout.close rescue StandardError nil end begin @stderr.close rescue StandardError nil end trigger_close @process = nil end |
#on_close(&block) ⇒ Object
37 38 39 40 |
# File 'lib/encom/transport/stdio.rb', line 37 def on_close(&block) @callbacks[:close] << block self end |
#on_data(&block) ⇒ Object
42 43 44 45 |
# File 'lib/encom/transport/stdio.rb', line 42 def on_data(&block) @callbacks[:data] << block self end |
#on_error(&block) ⇒ Object
Register event handlers
32 33 34 35 |
# File 'lib/encom/transport/stdio.rb', line 32 def on_error(&block) @callbacks[:error] << block self end |
#send(data) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/encom/transport/stdio.rb', line 71 def send(data) return false unless @process && @stdin data = "#{data}\n" unless data.end_with?("\n") begin @stdin.write(data) @stdin.flush true rescue IOError, Errno::EPIPE => e trigger_error(e) false end end |
#send_line(data) ⇒ Object
86 87 88 |
# File 'lib/encom/transport/stdio.rb', line 86 def send_line(data) send("#{data}\n") end |
#start ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/encom/transport/stdio.rb', line 47 def start raise 'StdioClientTransport already started!' if @process env = ENV.to_h command = @command args = @args @stdin, @stdout, @stderr, @process = Open3.popen3( env, command, *args # chdir: @server_params[:cwd] ) @process_pid = @process.pid start_stdout_thread start_stderr_thread start_process_monitor_thread self end |