justrun 1.0.4 → 1.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/justrun.rb +17 -12
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af93afa743b4e4c48ac538bdb14a3f4140bfa99d
4
- data.tar.gz: 773c9326572fb0ee13c454d0691ee6078c1aaa50
3
+ metadata.gz: f39bde292aa909e5c234486d10404e6f6d2967df
4
+ data.tar.gz: 92ea6aecc4804970a8d6d4c930df88c2712d4819
5
5
  SHA512:
6
- metadata.gz: 75f9e8bc72787006fccddb42e9adb05f4c77b8b97a3f13c7b8e2899a17f45df56a78f55219ebe8174a1a7cd0a8bbd85618cbb5aded129ce734fd5835bffd7c6e
7
- data.tar.gz: 95e9fa5de706ca94eefc3246c29d2e6fd66d4bf495fdd6afd3868375256d34238123b8b050bfa6c3cafa7f8e75c4b69b9697c5fad0204d35a60a53a432db4e28
6
+ metadata.gz: 9dcbf483f67f3b4355ad484c8a3a0c82463b41ac735d675f3345fa8d71fcc5767e98472587adb469d7db7ee7512518d03730f836cbe17a0fc9f782da488f9dc0
7
+ data.tar.gz: 40f6659d202e4b2fcbbf54d278b2b814cca2b58457c18eb0aeca505ec7b59082724e9645fa6acd212a5ea8d3a24769c9510536edc6d082c4b534f37378f17f99
@@ -3,7 +3,10 @@ require 'open3'
3
3
  #TODO: Run multiple commands at the same time
4
4
 
5
5
  class JustRun
6
- def self.command(command, block_size: 4096*4, buffer_output: false, chdir: Dir.pwd, init: ->(writer) {}, env: ENV, &block)
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
- until all_eof files do
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
- # expected
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
- break if written == 0
142
+ return true if written == 0
138
143
  end
139
144
  rescue IO::WaitWritable, Errno::EINTR
140
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: justrun
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Kaczmarek