frontkick 0.0.1 → 0.0.2

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: f76198405d6525369aa2223db8121819a4f29441
4
- data.tar.gz: 5595af1cdcf7d519531b2d7d16e08962d527ebb7
3
+ metadata.gz: 01d81fe27d75745783f882710005259ad6c906be
4
+ data.tar.gz: faf7d294bc7bda0101193a16c5cb4b4df68eb803
5
5
  SHA512:
6
- metadata.gz: 6bb73fb14d7d3277b7355687d5ded5d892e5e6d6f2db4f85e647353acde583987634e3ba387e93cba2cbbdcdc7e9cf276cc9fc57e40609fc4975398b8c973c29
7
- data.tar.gz: d79066eba2c55dd71e31a8347d5c641338847c2db47025d3a1e0069032ab0979321a4bc38ee82f61a631181d1246f5b35f6d7a9692249714e70707cc27261f66
6
+ metadata.gz: 515e97ebefd91655e3a0290e18c673a8582bc9bf32a8cdd7d4c1fcd7fe82adab6327bc97563c13b1cd0af411c7dc004e472da8160df9e41db6731174160849c7
7
+ data.tar.gz: 00251c8ad5cd01d07144bec5e7d8ed4be70c621924cee0b0ed2e9083fdfbeac2c0b3d7a957c46d3b54fdca4c7e2e58763fa02e6e0ddbda49b03a085e464f1772
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.0.2 (2013/06/23)
2
+
3
+ Features:
4
+
5
+ - Support timeout option
6
+
1
7
  # 0.0.1 (2013/06/23)
2
8
 
3
9
  First Version
10
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,22 +1,47 @@
1
1
  require 'benchmark'
2
2
  require 'open3'
3
3
 
4
- module FrontKick
4
+ module Frontkick
5
5
  class Command
6
- def self.exec(*cmd)
7
- stdout = nil
8
- stderr = nil
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
- duration = Benchmark.realtime do
12
- Open3.popen3(*cmd) do |stdin, out, err, wait_thr|
13
- stdout = out.read
14
- stderr = err.read
15
- exit_code = wait_thr.value.exitstatus
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
@@ -1,4 +1,4 @@
1
- module FrontKick
1
+ module Frontkick
2
2
  class CommandResult
3
3
  attr_accessor :stdout, :stderr, :duration
4
4
 
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.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo