Module: ImageOptim::Cmd
- Defined in:
- lib/image_optim/cmd.rb
Overview
Helper for running commands
Defined Under Namespace
Classes: TimeoutExceeded
Class Method Summary collapse
-
.capture(cmd) ⇒ Object
Run using backtick Return captured output Will raise SignalException if process was interrupted.
-
.run(*args) ⇒ Object
Run using ‘system` Return success status Will raise SignalException if process was interrupted.
-
.run_with_timeout(timeout, *args) ⇒ Object
Run the specified command, and kill it off if it runs longer than ‘timeout` seconds.
- .support_timeout? ⇒ Boolean
Class Method Details
.capture(cmd) ⇒ Object
Run using backtick Return captured output Will raise SignalException if process was interrupted
63 64 65 66 67 68 69 |
# File 'lib/image_optim/cmd.rb', line 63 def capture(cmd) output = `#{cmd}` check_status! output end |
.run(*args) ⇒ Object
Run using ‘system` Return success status Will raise SignalException if process was interrupted
13 14 15 16 17 18 19 |
# File 'lib/image_optim/cmd.rb', line 13 def run(*args) success = system(*args) check_status! success end |
.run_with_timeout(timeout, *args) ⇒ Object
Run the specified command, and kill it off if it runs longer than ‘timeout` seconds.
Return success status Will raise an error when command timeouts
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 |
# File 'lib/image_optim/cmd.rb', line 34 def run_with_timeout(timeout, *args) return run(*args) unless timeout > 0 && support_timeout? success = false (args) begin stdin, stdout, thread = Open3.popen2(*args) stdin.close stdout.close pid = thread[:pid] if thread.join(timeout).nil? cleanup_process(pid) thread.kill fail TimeoutExceeded else status = thread.value.exitstatus success = status.zero? if status end end success end |
.support_timeout? ⇒ Boolean
21 22 23 24 25 26 27 |
# File 'lib/image_optim/cmd.rb', line 21 def support_timeout? if defined?(JRUBY_VERSION) JRUBY_VERSION >= '9.0.0.0' else RUBY_VERSION >= '1.9' end end |