frontkick 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/VERSION +1 -1
- data/lib/frontkick/command.rb +35 -10
- data/lib/frontkick/command_result.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01d81fe27d75745783f882710005259ad6c906be
|
4
|
+
data.tar.gz: faf7d294bc7bda0101193a16c5cb4b4df68eb803
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 515e97ebefd91655e3a0290e18c673a8582bc9bf32a8cdd7d4c1fcd7fe82adab6327bc97563c13b1cd0af411c7dc004e472da8160df9e41db6731174160849c7
|
7
|
+
data.tar.gz: 00251c8ad5cd01d07144bec5e7d8ed4be70c621924cee0b0ed2e9083fdfbeac2c0b3d7a957c46d3b54fdca4c7e2e58763fa02e6e0ddbda49b03a085e464f1772
|
data/CHANGELOG.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/frontkick/command.rb
CHANGED
@@ -1,22 +1,47 @@
|
|
1
1
|
require 'benchmark'
|
2
2
|
require 'open3'
|
3
3
|
|
4
|
-
module
|
4
|
+
module Frontkick
|
5
5
|
class Command
|
6
|
-
def self.exec(
|
7
|
-
stdout = nil
|
8
|
-
|
9
|
-
exit_code = nil
|
6
|
+
def self.exec(cmd, opts = {})
|
7
|
+
stdout, stderr, exit_code, duration = nil
|
8
|
+
stdin, out, err, wait_thr, pid = nil
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
cmd_array = cmd.kind_of?(Array) ? cmd : [cmd]
|
11
|
+
begin
|
12
|
+
timeout(opts[:timeout]) do # nil is for no timeout
|
13
|
+
duration = Benchmark.realtime do
|
14
|
+
stdin, out, err, wait_thr = Open3.popen3(*cmd_array)
|
15
|
+
pid = wait_thr.pid
|
16
|
+
stdout = out.read
|
17
|
+
stderr = err.read
|
18
|
+
exit_code = wait_thr.value.exitstatus
|
19
|
+
process_wait(pid)
|
20
|
+
end
|
16
21
|
end
|
22
|
+
rescue Timeout::Error => e
|
23
|
+
Process.kill('SIGINT', pid)
|
24
|
+
exit_code = wait_thr.value.exitstatus
|
25
|
+
process_wait(pid)
|
26
|
+
duration = opts[:timeout]
|
27
|
+
stdout = ""
|
28
|
+
stderr = "pid:#{pid}\tcommand:#{cmd_array.join(' ')} is timeout!"
|
29
|
+
ensure
|
30
|
+
stdin.close unless stdin.nil?
|
31
|
+
out.close unless out.nil?
|
32
|
+
err.close unless err.nil?
|
33
|
+
wait_thr.kill unless wait_thr.stop?
|
17
34
|
end
|
18
35
|
|
19
36
|
CommandResult.new(stdout, stderr, exit_code, duration)
|
20
37
|
end
|
38
|
+
|
39
|
+
def self.process_wait(pid)
|
40
|
+
begin
|
41
|
+
pid, status = Process.waitpid2(pid) # wait child processes finish
|
42
|
+
rescue Errno::ECHILD => e
|
43
|
+
# no child process
|
44
|
+
end
|
45
|
+
end
|
21
46
|
end
|
22
47
|
end
|