about summary refs log tree commit diff
path: root/lib/terrapin
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2021-05-05 19:44:01 +0200
committerGitHub <noreply@github.com>2021-05-05 19:44:01 +0200
commit036556d3509fac5fa487a0d5ff3cf95767e8d84f (patch)
treef3435a4f1a5cbb999fde3118e9d17e62a889a59d /lib/terrapin
parentdfa002932d660656792a78887264dd00820f2dda (diff)
Fix media processing getting stuck on too much stdin/stderr (#16136)
* Fix media processing getting stuck on too much stdin/stderr

See thoughtbot/terrapin#5

* Remove dependency on paperclip-av-transcoder gem

* Remove dependency on streamio-ffmpeg gem

* Disable stdin on ffmpeg process
Diffstat (limited to 'lib/terrapin')
-rw-r--r--lib/terrapin/multi_pipe_extensions.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/terrapin/multi_pipe_extensions.rb b/lib/terrapin/multi_pipe_extensions.rb
new file mode 100644
index 000000000..51d7de37c
--- /dev/null
+++ b/lib/terrapin/multi_pipe_extensions.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: false
+# Fix adapted from https://github.com/thoughtbot/terrapin/pull/5
+
+module Terrapin
+  module MultiPipeExtensions
+    def read
+      read_streams(@stdout_in, @stderr_in)
+    end
+
+    def close_read
+      begin
+        @stdout_in.close
+      rescue IOError
+        # Do nothing
+      end
+
+      begin
+        @stderr_in.close
+      rescue IOError
+        # Do nothing
+      end
+    end
+
+    def read_streams(output, error)
+      @stdout_output = ''
+      @stderr_output = ''
+
+      read_fds = [output, error]
+
+      until read_fds.empty?
+        to_read, = IO.select(read_fds)
+
+        if to_read.include?(output)
+          @stdout_output << read_stream(output)
+          read_fds.delete(output) if output.closed?
+        end
+
+        if to_read.include?(error)
+          @stderr_output << read_stream(error)
+          read_fds.delete(error) if error.closed?
+        end
+      end
+    end
+
+    def read_stream(io)
+      result = ''
+
+      begin
+        while (partial_result = io.read_nonblock(8192))
+          result << partial_result
+        end
+      rescue EOFError, Errno::EPIPE
+        io.close
+      rescue Errno::EINTR, Errno::EWOULDBLOCK, Errno::EAGAIN
+        # Do nothing
+      end
+
+      result
+    end
+  end
+end
+
+Terrapin::CommandLine::MultiPipe.prepend(Terrapin::MultiPipeExtensions)