Class: LoggerPipe::Runner
- Inherits:
-
Object
- Object
- LoggerPipe::Runner
- Defined in:
- lib/logger_pipe/runner.rb
Instance Attribute Summary collapse
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#logging ⇒ Object
Returns the value of attribute logging.
-
#returns ⇒ Object
Returns the value of attribute returns.
-
#timeout(&block) ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(logger, cmd, options = {}) ⇒ Runner
constructor
A new instance of Runner.
- #logging_subfile(f) ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(logger, cmd, options = {}) ⇒ Runner
Returns a new instance of Runner.
21 22 23 24 25 26 27 |
# File 'lib/logger_pipe/runner.rb', line 21 def initialize(logger, cmd, = {}) @logger, @cmd = logger, cmd @timeout = [:timeout] @dry_run = [:dry_run] @returns = [:returns] || :stdout # :none, :stdout, :stderr, :both @logging = [:logging] || :both # :none, :stdout, :stderr, :both end |
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd.
18 19 20 |
# File 'lib/logger_pipe/runner.rb', line 18 def cmd @cmd end |
#logger ⇒ Object
Returns the value of attribute logger.
18 19 20 |
# File 'lib/logger_pipe/runner.rb', line 18 def logger @logger end |
#logging ⇒ Object
Returns the value of attribute logging.
19 20 21 |
# File 'lib/logger_pipe/runner.rb', line 19 def logging @logging end |
#returns ⇒ Object
Returns the value of attribute returns.
19 20 21 |
# File 'lib/logger_pipe/runner.rb', line 19 def returns @returns end |
#timeout(&block) ⇒ Object
Returns the value of attribute timeout.
18 19 20 |
# File 'lib/logger_pipe/runner.rb', line 18 def timeout @timeout end |
Instance Method Details
#execute ⇒ Object
29 30 31 32 33 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 |
# File 'lib/logger_pipe/runner.rb', line 29 def execute if @dry_run logger.info("dry run: #{cmd}") return nil end @buf = [] # systemをタイムアウトさせることはできないので、popenの戻り値を使っています。 # see http://docs.ruby-lang.org/ja/2.0.0/class/Timeout.html @com, @pid = nil, nil setup do |actual_cmd, log_enable| logger.info("executing: #{actual_cmd}") timeout do # popenにブロックを渡さないと$?がnilになってしまうので敢えてブロックで処理しています。 @com = IO.popen(actual_cmd) do |com| @com = com @pid = com.pid while line = com.gets @buf << line logger.debug(line.chomp) if log_enable end end if $?.exitstatus == 0 logger.info("\e[32mSUCCESS: %s\e[0m" % [actual_cmd]) return (returns == :none) ? nil : @buf.join else msg = "\e[31mFAILURE: %s\e[0m" % [actual_cmd] logger.error(msg) raise Failure.new(msg, @buf) end end end end |
#logging_subfile(f) ⇒ Object
129 130 131 132 |
# File 'lib/logger_pipe/runner.rb', line 129 def logging_subfile(f) f.open logger.info("--- begin stderr ---\n%s\n--- end stderr ---" % f.read) end |
#setup ⇒ Object
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 |
# File 'lib/logger_pipe/runner.rb', line 92 def setup raise ArgumentError, "Invalid option :returns #{returns.inspect}" unless SOURCES.include?(returns) raise ArgumentError, "Invalid option :logging #{logging.inspect}" unless SOURCES.include?(logging) if (returns == :both) && ([:stdout, :stderr].include?(logging)) raise ArgumentError, "Can' set logging: #{logging.inspect} with returns: #{returns.inspect}" elsif (returns == :none) || (logging == :none) || (returns == logging) actual_cmd = case logging when :none then case returns when :none then "#{cmd} 1>/dev/null 2>/dev/null" when :stdout then "#{cmd} 2>/dev/null" when :stderr then "#{cmd} 2>&1 1>/dev/null" when :both then "#{cmd} 2>&1" end when :stdout then "#{cmd} 2>/dev/null" when :stderr then "#{cmd} 2>&1 1>/dev/null" when :both then "#{cmd} 2>&1" end return block_given? ? yield(actual_cmd, logging != :none) : nil else Tempfile.open("logger_pipe.stderr.log") do |f| f.close actual_cmd = case returns when :stdout then "#{cmd} 2>#{f.path}" when :stderr then "#{cmd} 2>&1 1>#{f.path}" end begin return block_given? ? yield(actual_cmd, logging == :both) : nil ensure logging_subfile(f) end end end end |