justrun 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/justrun.rb +17 -12
- 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: f39bde292aa909e5c234486d10404e6f6d2967df
|
4
|
+
data.tar.gz: 92ea6aecc4804970a8d6d4c930df88c2712d4819
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dcbf483f67f3b4355ad484c8a3a0c82463b41ac735d675f3345fa8d71fcc5767e98472587adb469d7db7ee7512518d03730f836cbe17a0fc9f782da488f9dc0
|
7
|
+
data.tar.gz: 40f6659d202e4b2fcbbf54d278b2b814cca2b58457c18eb0aeca505ec7b59082724e9645fa6acd212a5ea8d3a24769c9510536edc6d082c4b534f37378f17f99
|
data/lib/justrun.rb
CHANGED
@@ -3,7 +3,10 @@ require 'open3'
|
|
3
3
|
#TODO: Run multiple commands at the same time
|
4
4
|
|
5
5
|
class JustRun
|
6
|
-
|
6
|
+
class CommandTimeout < Exception
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.command(command, timeout: 0, block_size: 4096*4, buffer_output: false, chdir: Dir.pwd, init: ->(writer) {}, env: ENV, &block)
|
7
10
|
ret_code = -1
|
8
11
|
|
9
12
|
buffers = {
|
@@ -12,10 +15,10 @@ class JustRun
|
|
12
15
|
all: []
|
13
16
|
}
|
14
17
|
|
18
|
+
beginning_time = Time.now
|
15
19
|
Open3.popen3 env, command, chdir: chdir do |stdin, stdout, stderr, wait_thr|
|
16
20
|
writer = JustRun::Writer.new stdin
|
17
21
|
init.call writer
|
18
|
-
|
19
22
|
line_handler = -> line, name {
|
20
23
|
if buffer_output
|
21
24
|
buffers[name].push line
|
@@ -33,8 +36,15 @@ class JustRun
|
|
33
36
|
stdout.fileno => :stdout,
|
34
37
|
stderr.fileno => :stderr
|
35
38
|
}
|
36
|
-
|
39
|
+
|
40
|
+
was_eof = []
|
41
|
+
until was_eof[stdout.fileno] && was_eof[stderr.fileno] do
|
37
42
|
ready = IO.select files, stdin.closed? ? [] : [stdin], [], 0.1
|
43
|
+
if timeout > 0 && (Time.now - beginning_time) > timeout
|
44
|
+
`kill -9 #{wait_thr.pid}` # note: Process.kill does not work
|
45
|
+
raise CommandTimeout, "Command: '#{command}' timed out with timeout #{timeout}s"
|
46
|
+
end
|
47
|
+
|
38
48
|
if ready
|
39
49
|
readable = ready[0]
|
40
50
|
readable.each do |f|
|
@@ -55,13 +65,11 @@ class JustRun
|
|
55
65
|
line_handler.call line, name
|
56
66
|
end
|
57
67
|
rescue EOFError => e
|
58
|
-
|
59
|
-
end
|
60
|
-
writable = ready[1]
|
61
|
-
writable.each do |stdin|
|
62
|
-
writer.process
|
68
|
+
was_eof[fileno] = true
|
63
69
|
end
|
64
70
|
end
|
71
|
+
writable = ready[1]
|
72
|
+
writable.each { |stdin| writer.process }
|
65
73
|
end
|
66
74
|
end
|
67
75
|
fileno_lines.each do |fileno, lines|
|
@@ -89,9 +97,6 @@ class JustRun
|
|
89
97
|
end
|
90
98
|
|
91
99
|
private
|
92
|
-
def self.all_eof(files)
|
93
|
-
files.find { |f| !f.eof }.nil?
|
94
|
-
end
|
95
100
|
|
96
101
|
class Writer
|
97
102
|
def initialize stdin
|
@@ -134,7 +139,7 @@ class JustRun
|
|
134
139
|
@on_empty.call
|
135
140
|
return
|
136
141
|
end
|
137
|
-
|
142
|
+
return true if written == 0
|
138
143
|
end
|
139
144
|
rescue IO::WaitWritable, Errno::EINTR
|
140
145
|
end
|