background_process 1.0 → 1.1
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.
data/README.textile
CHANGED
@@ -4,13 +4,15 @@ This is like popen4, but provides several convenience methods for interacting
|
|
4
4
|
with the process. It only works on UNIX and Ruby implementations that support
|
5
5
|
fork and native UNIX I/O streams.
|
6
6
|
|
7
|
+
"Click here for Complete Documentation":http://rdoc.info/projects/timcharper/background_process
|
8
|
+
|
7
9
|
Example:
|
8
10
|
|
9
11
|
<pre>
|
10
12
|
process = BackgroundProcess.run("sh -c 'sleep 1; exit 1'")
|
11
13
|
process.running? # => true
|
12
14
|
process.wait # => #<Process::Status: pid=34774,exited(1)>
|
13
|
-
process.running? #=>
|
15
|
+
process.running? #=> false
|
14
16
|
process.exitstatus # => 1
|
15
17
|
</pre>
|
16
18
|
|
@@ -4,17 +4,18 @@ module BackgroundProcess::IOHelpers
|
|
4
4
|
begin
|
5
5
|
Timeout::timeout(timeout) do
|
6
6
|
# Something that should be interrupted if it takes too much time...
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
until streams.empty?
|
8
|
+
active_streams, _, _ = Kernel.select(streams, nil, nil, 1)
|
9
|
+
active_streams.each do |s|
|
10
|
+
(streams -= [s]; next) if s.eof?
|
10
11
|
content = s.gets
|
11
12
|
if result = (block.arity == 1 ? yield(content) : yield(s, content))
|
12
13
|
return result
|
13
14
|
end
|
14
|
-
end if
|
15
|
+
end if active_streams
|
15
16
|
end
|
16
17
|
end
|
17
|
-
|
18
|
+
nil
|
18
19
|
rescue Timeout::Error
|
19
20
|
nil
|
20
21
|
end
|
@@ -45,7 +45,7 @@ describe BackgroundProcess do
|
|
45
45
|
|
46
46
|
describe "#exitstatus" do
|
47
47
|
it "returns the exit status of a process after it exits." do
|
48
|
-
process = BackgroundProcess.run("bash -c 'sleep 1; exit 1'")
|
48
|
+
process = BackgroundProcess.run("bash -c 'sleep 0.1; exit 1'")
|
49
49
|
process.exitstatus.should == 1
|
50
50
|
process.exitstatus.should == 1
|
51
51
|
end
|
@@ -55,14 +55,14 @@ describe BackgroundProcess do
|
|
55
55
|
it "waits for a process with timeout" do
|
56
56
|
process = BackgroundProcess.run("sleep 3")
|
57
57
|
started_waiting = Time.now
|
58
|
-
process.wait(0.
|
59
|
-
(Time.now - started_waiting).should be_close(0.
|
58
|
+
process.wait(0.1).should be_false
|
59
|
+
(Time.now - started_waiting).should be_close(0.1, 0.05)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
describe "#detect" do
|
64
64
|
it "calls the provided block for every line outputted, and returns the first non-false value" do
|
65
|
-
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.
|
65
|
+
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.05; do a=$(($a + 1)); echo $a; done'")
|
66
66
|
result = process.detect do |line|
|
67
67
|
"golden" if line.strip == "3"
|
68
68
|
end
|
@@ -71,7 +71,7 @@ describe BackgroundProcess do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it "yields the stream if two parameters are provided on the block" do
|
74
|
-
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.
|
74
|
+
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.05; do a=$(($a + 1)); echo $a 1>&2; done'")
|
75
75
|
result = process.detect(:both, 1) do |stream, line|
|
76
76
|
"golden" if stream == process.stderr && line.strip == "3"
|
77
77
|
end
|
@@ -81,7 +81,7 @@ describe BackgroundProcess do
|
|
81
81
|
|
82
82
|
it "aborts if the provided timeout is reached" do
|
83
83
|
process = BackgroundProcess.run("sleep 2")
|
84
|
-
result = process.detect(:both, 0.
|
84
|
+
result = process.detect(:both, 0.05) do |stream, line|
|
85
85
|
true
|
86
86
|
end
|
87
87
|
result.should be_nil
|
@@ -89,7 +89,7 @@ describe BackgroundProcess do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "monitors the specified stream" do
|
92
|
-
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.
|
92
|
+
process = BackgroundProcess.run("bash -c 'a=0; while sleep 0.05; do a=$(($a + 1)); echo $a; echo $a 1>&2; done'")
|
93
93
|
output = []
|
94
94
|
process.detect(:stdout) do |line|
|
95
95
|
output << line.to_i
|
@@ -105,10 +105,25 @@ describe BackgroundProcess do
|
|
105
105
|
end
|
106
106
|
|
107
107
|
it "never yields if nothing occurs on specified streams" do
|
108
|
-
process = BackgroundProcess.run("bash -c '
|
108
|
+
process = BackgroundProcess.run("bash -c 'echo hi'")
|
109
109
|
process.detect(:stderr, 1) do |line|
|
110
110
|
raise(Spec::Expectations::ExpectationNotMetError, "expected to not yield the block")
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
it "quits when the process does" do
|
115
|
+
process = BackgroundProcess.run("sleep 0.3")
|
116
|
+
started_waiting = Time.now
|
117
|
+
result = process.detect(:both, 1) do |line|
|
118
|
+
line.should_not be_nil
|
119
|
+
end
|
120
|
+
result.should be_nil
|
121
|
+
(Time.now - started_waiting).should < 0.4
|
122
|
+
end
|
123
|
+
|
124
|
+
it "yields the very last bit of the process output" do
|
125
|
+
process = BackgroundProcess.run("echo hello; printf hi")
|
126
|
+
process.detect(:both, 1) { |line| line == "hi" }.should == true
|
127
|
+
end
|
113
128
|
end
|
114
129
|
end
|
metadata
CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
version: "1.
|
7
|
+
- 1
|
8
|
+
version: "1.1"
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Tim Harper
|
@@ -13,7 +13,7 @@ autorequire:
|
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
15
|
|
16
|
-
date: 2010-04-
|
16
|
+
date: 2010-04-22 00:00:00 -06:00
|
17
17
|
default_executable:
|
18
18
|
dependencies: []
|
19
19
|
|