Class: Graphomaton::ProcessRunner
- Inherits:
-
Object
- Object
- Graphomaton::ProcessRunner
- Defined in:
- lib/graphomaton/process_runner.rb
Defined Under Namespace
Classes: Error, OutputLimitError, TimeoutError
Constant Summary collapse
- DEFAULT_TIMEOUT =
30- DEFAULT_MAX_STDOUT_BYTES =
64 * 1024 * 1024
- DEFAULT_MAX_STDERR_BYTES =
1024 * 1024
- READ_SIZE =
16 * 1024
Class Method Summary collapse
- .capture3(*command, stdin_data: '', binmode: false, timeout: DEFAULT_TIMEOUT, max_stdout_bytes: DEFAULT_MAX_STDOUT_BYTES, max_stderr_bytes: DEFAULT_MAX_STDERR_BYTES) ⇒ Object
- .which(command, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', '')) ⇒ Object
Class Method Details
.capture3(*command, stdin_data: '', binmode: false, timeout: DEFAULT_TIMEOUT, max_stdout_bytes: DEFAULT_MAX_STDOUT_BYTES, max_stderr_bytes: DEFAULT_MAX_STDERR_BYTES) ⇒ Object
34 35 36 37 38 39 40 41 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/graphomaton/process_runner.rb', line 34 def self.capture3(*command, stdin_data: '', binmode: false, timeout: DEFAULT_TIMEOUT, max_stdout_bytes: DEFAULT_MAX_STDOUT_BYTES, max_stderr_bytes: DEFAULT_MAX_STDERR_BYTES) validate_limit(timeout, 'timeout') validate_limit(max_stdout_bytes, 'max_stdout_bytes') validate_limit(max_stderr_bytes, 'max_stderr_bytes') = Gem.win_platform? ? { new_pgroup: true } : { pgroup: true } stdout_data = nil stderr_data = nil status = nil Open3.popen3(*command, **) do |stdin, stdout, stderr, wait_thread| streams = [stdin, stdout, stderr] streams.each(&:binmode) if binmode writer = input_writer(stdin, stdin_data) stdout_reader = output_reader(stdout, max_stdout_bytes, 'stdout') stderr_reader = output_reader(stderr, max_stderr_bytes, 'stderr') threads = [writer, stdout_reader, stderr_reader] begin Timeout.timeout(timeout, TimeoutError, "Process timed out after #{timeout} seconds") do writer.value stdout_data = stdout_reader.value stderr_data = stderr_reader.value status = wait_thread.value end rescue TimeoutError, OutputLimitError terminate(wait_thread) raise ensure streams.each { |stream| stream.close unless stream.closed? } threads.each do |thread| thread.kill if thread.alive? thread.join end end end [stdout_data, stderr_data, status] end |
.which(command, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', '')) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/graphomaton/process_runner.rb', line 17 def self.which(command, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', '')) name = command.to_s return nil if name.empty? || name.include?("\0") explicit = name.include?(File::SEPARATOR) || (File::ALT_SEPARATOR && name.include?(File::ALT_SEPARATOR)) directories = explicit ? [''] : path.split(File::PATH_SEPARATOR) extensions = executable_extensions(name, pathext) directories.each do |directory| base = explicit ? name : File.join(directory, name) extensions.each do |extension| candidate = "#{base}#{extension}" return File.(candidate) if File.file?(candidate) && File.executable?(candidate) end end nil end |