frontkick 0.4.9 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16a532fb426a342a5850b7b64badbcc69e66e718
4
- data.tar.gz: d0e33fb6cab10d114c247f53d6b9ea309bf831f7
3
+ metadata.gz: c1ec1035f37f85a6c2df0efac9c9a2bf94f924aa
4
+ data.tar.gz: f86e5798517525996d28f8e9ea37752864771b50
5
5
  SHA512:
6
- metadata.gz: 777ce58833f2764f2115bf012ffea28d9cb8d99fa3d596678a1c7664e1fb05503b37513fc281207813b36425878b6cce0c370b0d3d1ef9575450f3c9d9f82a4e
7
- data.tar.gz: 05092c48d5d8c3972e0d50e966458ca45d895efc14f7ffcf4e1b6e1b0b66eb1f54dc03fdc5d94ec00820546b7ab5335b72e1b752666f4d9495425a1a0f2495fb
6
+ metadata.gz: 15f13d612326c77c06c274c0a21be7d80c5da51018a1060f8092ead9170387079cf99bc77447acc7aab34dd51a125b72cd0bf3dc46118aa7f682e6c54cf3c6f3
7
+ data.tar.gz: 13eb2a6ddc5fd04aba31e499428c17ba982e5a0ac6c80a80f6c668bf8cfb3cacdf7a1b056e98bb5d1488b1b0d527b08435680f252331bfc806dcc2b4fa97b1ca
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.5.0 (2016/01/22)
2
+
3
+ Enhancements:
4
+
5
+ - Add :out and :err options to redirects STDOUT and STDERR
6
+
1
7
  # 0.4.9 (2016/01/22)
2
8
 
3
9
  Enhancements:
data/README.md CHANGED
@@ -67,12 +67,17 @@ NOTE: Shoud use `[]` form, otherwirse `sh -c 'sleep 100'` is ran, and frotkick k
67
67
 
68
68
  Other options such as :chdir are treated as options of `Kernel.#spawn`. See http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-spawn for available options.
69
69
 
70
- ### Hint: Redirect stderr to stdout
70
+ ### Redirect Options
71
71
 
72
- Frontkick itself does not aid anything, but you can do as
72
+ Frontkick.exec(["ls /something_not_found"], :out => 'stdout.txt', :err => 'stderr.txt')
73
73
 
74
- result = Frontkick.exec(["ls /something_not_found 2>&1"])
75
- puts result.stdout #=> ls: /something_not_found: No such file or directory
74
+ This redirects STDOUT and STDERR into files. In this case, result.stdout, and result.stderr are the given filename.
75
+
76
+ out = File.open('stdout.txt', 'w').tap {|fp| fp.sync = true }
77
+ err = File.open('stderr.txt', 'w').tap {|fp| fp.sync = true }
78
+ Frontkick.exec(["ls /something_not_found"], :out => out, :err => err)
79
+
80
+ You can also give IO objects. In this case, result.stdout, and result.stderr are the given IO objects.
76
81
 
77
82
  ## Contributing
78
83
 
@@ -0,0 +1,8 @@
1
+ require 'frontkick'
2
+
3
+ out = '/tmp/frontkick_stdout.txt'
4
+ err = '/tmp/frontkick_stderr.txt'
5
+ result = Frontkick.exec(["ls /something_not_found"], :out => out, :err => err)
6
+ puts "write to #{result.stdout} and #{result.stderr}"
7
+ puts File.read(result.stdout)
8
+ puts File.read(result.stderr)
@@ -7,8 +7,30 @@ module Frontkick
7
7
  def self.exec(cmd, opts = {})
8
8
  opts[:timeout_kill] = true unless opts.has_key?(:timeout_kill) # default: true
9
9
 
10
- stdout, stderr, exit_code, duration = nil
11
- stdin, out, err, wait_thr, pid = nil
10
+ exit_code, duration = nil
11
+ stdin, stdout, stderr, wait_thr, pid = nil
12
+
13
+ if opts[:out]
14
+ if opts[:out].is_a?(String)
15
+ out = File.open(opts[:out], 'w')
16
+ out.sync = true
17
+ else
18
+ out = opts[:out] # IO
19
+ end
20
+ else
21
+ out = StringIO.new
22
+ end
23
+
24
+ if opts[:err]
25
+ if opts[:err].is_a?(String)
26
+ err = File.open(opts[:err], 'w')
27
+ err.sync = true
28
+ else
29
+ err = opts[:err] # IO
30
+ end
31
+ else
32
+ err = StringIO.new
33
+ end
12
34
 
13
35
  cmd_array = cmd.kind_of?(Array) ? cmd : [cmd]
14
36
  command = "#{cmd_array.first} #{Shellwords.shelljoin(cmd_array[1..-1])}"
@@ -23,9 +45,23 @@ module Frontkick
23
45
  begin
24
46
  ::Timeout.timeout(opts[:timeout], Frontkick::TimeoutLocal) do # nil is for no timeout
25
47
  duration = Benchmark.realtime do
26
- stdin, out, err, wait_thr = Open3.popen3(*cmd_array, spawn_opts)
27
- out_reader = Thread.new { out.read }
28
- err_reader = Thread.new { err.read }
48
+ stdin, stdout, stderr, wait_thr = Open3.popen3(*cmd_array, spawn_opts)
49
+ out_thread = Thread.new {
50
+ begin
51
+ while true
52
+ out.write stdout.readpartial(4096)
53
+ end
54
+ rescue EOFError
55
+ end
56
+ }
57
+ err_thread = Thread.new {
58
+ begin
59
+ while true
60
+ err.write stderr.readpartial(4096)
61
+ end
62
+ rescue EOFError
63
+ end
64
+ }
29
65
  stdin.close
30
66
  pid = wait_thr.pid
31
67
 
@@ -33,8 +69,8 @@ module Frontkick
33
69
  trap_signal(pid)
34
70
  end
35
71
 
36
- stdout = out_reader.value
37
- stderr = err_reader.value
72
+ out_thread.join
73
+ err_thread.join
38
74
  exit_code = wait_thr.value.exitstatus
39
75
  process_wait(pid)
40
76
  end
@@ -52,9 +88,19 @@ module Frontkick
52
88
  err.close if err and !err.closed?
53
89
  wait_thr.kill if wait_thr and !wait_thr.stop?
54
90
  lock_fd.flock(File::LOCK_UN) if lock_fd
91
+ if opts[:out] and opts[:out].is_a?(String)
92
+ out.close rescue nil
93
+ end
94
+ if opts[:err] and opts[:err].is_a?(String)
95
+ err.close rescue nil
96
+ end
55
97
  end
56
-
57
- Result.new(:stdout => stdout, :stderr => stderr, :exit_code => exit_code, :duration => duration)
98
+
99
+ Result.new(
100
+ :stdout => opts[:out] ? opts[:out] : out.string,
101
+ :stderr => opts[:err] ? opts[:err] : err.string,
102
+ :exit_code => exit_code, :duration => duration
103
+ )
58
104
  end
59
105
 
60
106
  # private
@@ -6,7 +6,7 @@ module Frontkick
6
6
  alias :exitstatus :exit_code # for compatibility with Open3 Process::Status
7
7
  alias :exitstatus= :exit_code= # for compatibility with Open3 Process::Status
8
8
 
9
- def initialize(params)
9
+ def initialize(params = {})
10
10
  @stdout = params[:stdout] || ""
11
11
  @stderr = params[:stderr] || ""
12
12
  @exit_code = params[:exit_code] || 0
@@ -1,3 +1,3 @@
1
1
  module Frontkick
2
- VERSION = "0.4.9"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frontkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
@@ -69,6 +69,7 @@ files:
69
69
  - Rakefile
70
70
  - example/dry_run.rb
71
71
  - example/kill_child.rb
72
+ - example/out_err.rb
72
73
  - example/run.rb
73
74
  - example/spawn_opts.rb
74
75
  - experiment/capture3_no_deadlock.rb