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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +9 -4
- data/example/out_err.rb +8 -0
- data/lib/frontkick/command.rb +55 -9
- data/lib/frontkick/result.rb +1 -1
- data/lib/frontkick/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1ec1035f37f85a6c2df0efac9c9a2bf94f924aa
|
4
|
+
data.tar.gz: f86e5798517525996d28f8e9ea37752864771b50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15f13d612326c77c06c274c0a21be7d80c5da51018a1060f8092ead9170387079cf99bc77447acc7aab34dd51a125b72cd0bf3dc46118aa7f682e6c54cf3c6f3
|
7
|
+
data.tar.gz: 13eb2a6ddc5fd04aba31e499428c17ba982e5a0ac6c80a80f6c668bf8cfb3cacdf7a1b056e98bb5d1488b1b0d527b08435680f252331bfc806dcc2b4fa97b1ca
|
data/CHANGELOG.md
CHANGED
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
|
-
###
|
70
|
+
### Redirect Options
|
71
71
|
|
72
|
-
Frontkick
|
72
|
+
Frontkick.exec(["ls /something_not_found"], :out => 'stdout.txt', :err => 'stderr.txt')
|
73
73
|
|
74
|
-
|
75
|
-
|
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
|
|
data/example/out_err.rb
ADDED
@@ -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)
|
data/lib/frontkick/command.rb
CHANGED
@@ -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
|
-
|
11
|
-
stdin,
|
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,
|
27
|
-
|
28
|
-
|
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
|
-
|
37
|
-
|
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(
|
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
|
data/lib/frontkick/result.rb
CHANGED
@@ -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
|
data/lib/frontkick/version.rb
CHANGED
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
|
+
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
|