open4 0.7.0 → 0.8.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.
@@ -4,7 +4,7 @@ require 'thread'
4
4
 
5
5
  module Open4
6
6
  #--{{{
7
- VERSION = '0.7.0'
7
+ VERSION = '0.8.0'
8
8
  def self.version() VERSION end
9
9
 
10
10
  class Error < ::StandardError; end
@@ -76,8 +76,6 @@ module Open4
76
76
  module_function :popen4
77
77
  module_function :open4
78
78
 
79
-
80
-
81
79
  class SpawnError < Error
82
80
  #--{{{
83
81
  attr 'cmd'
@@ -103,16 +101,26 @@ module Open4
103
101
  #--{{{
104
102
  attr 'threads'
105
103
 
106
- def initialize
107
- @threads, @argv, @done, @running = [], [], Queue.new, false
104
+ def initialize cid
105
+ @cid, @threads, @argv, @done, @running = @cid, [], [], Queue.new, false
106
+ @killed = false
108
107
  end
109
108
 
110
109
  def add_thread *a, &b
111
110
  @running ? raise : (@argv << [a, b])
112
111
  end
113
112
 
113
+ #
114
+ # take down process more nicely
115
+ #
114
116
  def killall
115
- (@threads - [Thread.current]).each{|t| t.kill rescue nil} if @running
117
+ c = Thread.critical
118
+ return nil if @killed
119
+ Thread.critical = true
120
+ (@threads - [Thread.current]).each{|t| t.kill rescue nil}
121
+ @killed = true
122
+ ensure
123
+ Thread.critical = c
116
124
  end
117
125
 
118
126
  def run
@@ -185,41 +193,43 @@ module Open4
185
193
 
186
194
  def relay src, dst = nil, t = nil
187
195
  #--{{{
188
- if src.respond_to? :gets
189
- while buf = to(t){ src.gets }
190
- dst << buf if dst
191
- end
196
+ unless src.nil?
197
+ if src.respond_to? :gets
198
+ while buf = to(t){ src.gets }
199
+ dst << buf if dst
200
+ end
192
201
 
193
- elsif src.respond_to? :each
194
- q = Queue.new
195
- th = nil
202
+ elsif src.respond_to? :each
203
+ q = Queue.new
204
+ th = nil
196
205
 
197
- timer_set = lambda do |t|
198
- th = new_thread{ to(t){ q.pop } }
199
- end
206
+ timer_set = lambda do |t|
207
+ th = new_thread{ to(t){ q.pop } }
208
+ end
200
209
 
201
- timer_cancel = lambda do |t|
202
- th.kill if th rescue nil
203
- end
210
+ timer_cancel = lambda do |t|
211
+ th.kill if th rescue nil
212
+ end
204
213
 
205
- timer_set[t]
206
- begin
207
- src.each do |buf|
214
+ timer_set[t]
215
+ begin
216
+ src.each do |buf|
217
+ timer_cancel[t]
218
+ dst << buf if dst
219
+ timer_set[t]
220
+ end
221
+ ensure
208
222
  timer_cancel[t]
209
- dst << buf if dst
210
- timer_set[t]
211
223
  end
212
- ensure
213
- timer_cancel[t]
214
- end
215
224
 
216
- elsif src.respond_to? :read
217
- buf = to(t){ src.read }
218
- dst << buf if dst
225
+ elsif src.respond_to? :read
226
+ buf = to(t){ src.read }
227
+ dst << buf if dst
219
228
 
220
- else
221
- buf = to(t){ src.to_s }
222
- dst << buf if dst
229
+ else
230
+ buf = to(t){ src.to_s }
231
+ dst << buf if dst
232
+ end
223
233
  end
224
234
  #--}}}
225
235
  end
@@ -263,26 +273,26 @@ module Open4
263
273
  popen4(cmd) do |c, i, o, e|
264
274
  started = true
265
275
 
266
- %w( pid= << push update ).each do |msg|
276
+ %w( replace pid= << push update ).each do |msg|
267
277
  break(pid.send(msg, c)) if pid.respond_to? msg
268
278
  end
269
279
 
270
- te = ThreadEnsemble.new
280
+ te = ThreadEnsemble.new c
271
281
 
272
- te.add_thread(i, stdin) do |i, stdin|
273
- relay stdin, i, stdin_timeout if stdin
274
- i.close rescue nil
275
- end
282
+ te.add_thread(i, stdin) do |i, stdin|
283
+ relay stdin, i, stdin_timeout
284
+ i.close rescue nil
285
+ end
276
286
 
277
- te.add_thread(o, stdout) do |o, stdout|
278
- relay o, stdout, stdout_timeout if stdout
279
- end
287
+ te.add_thread(o, stdout) do |o, stdout|
288
+ relay o, stdout, stdout_timeout
289
+ end
280
290
 
281
- te.add_thread(e, stderr) do |o, stderr|
282
- relay e, stderr, stderr_timeout if stderr
283
- end
291
+ te.add_thread(e, stderr) do |o, stderr|
292
+ relay e, stderr, stderr_timeout
293
+ end
284
294
 
285
- te.run
295
+ te.run
286
296
  end
287
297
  end
288
298
  rescue
@@ -316,6 +326,44 @@ module Open4
316
326
  alias bg background
317
327
  module_function :background
318
328
  module_function :bg
329
+
330
+ def maim pid, opts = {}
331
+ #--{{{
332
+ getopt = getopts opts
333
+ sigs = getopt[ 'signals', %w(SIGTERM SIGQUIT SIGKILL) ]
334
+ suspend = getopt[ 'suspend', 4 ]
335
+ pid = Integer pid
336
+ existed = false
337
+ sigs.each do |sig|
338
+ begin
339
+ Process.kill sig, pid
340
+ existed = true
341
+ rescue Errno::ESRCH
342
+ return(existed ? nil : true)
343
+ end
344
+ return true unless alive? pid
345
+ sleep suspend
346
+ return true unless alive? pid
347
+ end
348
+ return(not alive?(pid))
349
+ #--}}}
350
+ end
351
+ module_function :maim
352
+
353
+ def alive pid
354
+ #--{{{
355
+ pid = Integer pid
356
+ begin
357
+ Process.kill 0, pid
358
+ true
359
+ rescue Errno::ESRCH
360
+ false
361
+ end
362
+ #--}}}
363
+ end
364
+ alias alive? alive
365
+ module_function :alive
366
+ module_function :'alive?'
319
367
  #--}}}
320
368
  end
321
369
 
@@ -4,7 +4,7 @@ require 'thread'
4
4
 
5
5
  module Open4
6
6
  #--{{{
7
- VERSION = '0.7.0'
7
+ VERSION = '0.8.0'
8
8
  def self.version() VERSION end
9
9
 
10
10
  class Error < ::StandardError; end
@@ -76,8 +76,6 @@ module Open4
76
76
  module_function :popen4
77
77
  module_function :open4
78
78
 
79
-
80
-
81
79
  class SpawnError < Error
82
80
  #--{{{
83
81
  attr 'cmd'
@@ -103,16 +101,26 @@ module Open4
103
101
  #--{{{
104
102
  attr 'threads'
105
103
 
106
- def initialize
107
- @threads, @argv, @done, @running = [], [], Queue.new, false
104
+ def initialize cid
105
+ @cid, @threads, @argv, @done, @running = @cid, [], [], Queue.new, false
106
+ @killed = false
108
107
  end
109
108
 
110
109
  def add_thread *a, &b
111
110
  @running ? raise : (@argv << [a, b])
112
111
  end
113
112
 
113
+ #
114
+ # take down process more nicely
115
+ #
114
116
  def killall
115
- (@threads - [Thread.current]).each{|t| t.kill rescue nil} if @running
117
+ c = Thread.critical
118
+ return nil if @killed
119
+ Thread.critical = true
120
+ (@threads - [Thread.current]).each{|t| t.kill rescue nil}
121
+ @killed = true
122
+ ensure
123
+ Thread.critical = c
116
124
  end
117
125
 
118
126
  def run
@@ -185,41 +193,43 @@ module Open4
185
193
 
186
194
  def relay src, dst = nil, t = nil
187
195
  #--{{{
188
- if src.respond_to? :gets
189
- while buf = to(t){ src.gets }
190
- dst << buf if dst
191
- end
196
+ unless src.nil?
197
+ if src.respond_to? :gets
198
+ while buf = to(t){ src.gets }
199
+ dst << buf if dst
200
+ end
192
201
 
193
- elsif src.respond_to? :each
194
- q = Queue.new
195
- th = nil
202
+ elsif src.respond_to? :each
203
+ q = Queue.new
204
+ th = nil
196
205
 
197
- timer_set = lambda do |t|
198
- th = new_thread{ to(t){ q.pop } }
199
- end
206
+ timer_set = lambda do |t|
207
+ th = new_thread{ to(t){ q.pop } }
208
+ end
200
209
 
201
- timer_cancel = lambda do |t|
202
- th.kill if th rescue nil
203
- end
210
+ timer_cancel = lambda do |t|
211
+ th.kill if th rescue nil
212
+ end
204
213
 
205
- timer_set[t]
206
- begin
207
- src.each do |buf|
214
+ timer_set[t]
215
+ begin
216
+ src.each do |buf|
217
+ timer_cancel[t]
218
+ dst << buf if dst
219
+ timer_set[t]
220
+ end
221
+ ensure
208
222
  timer_cancel[t]
209
- dst << buf if dst
210
- timer_set[t]
211
223
  end
212
- ensure
213
- timer_cancel[t]
214
- end
215
224
 
216
- elsif src.respond_to? :read
217
- buf = to(t){ src.read }
218
- dst << buf if dst
225
+ elsif src.respond_to? :read
226
+ buf = to(t){ src.read }
227
+ dst << buf if dst
219
228
 
220
- else
221
- buf = to(t){ src.to_s }
222
- dst << buf if dst
229
+ else
230
+ buf = to(t){ src.to_s }
231
+ dst << buf if dst
232
+ end
223
233
  end
224
234
  #--}}}
225
235
  end
@@ -263,26 +273,26 @@ module Open4
263
273
  popen4(cmd) do |c, i, o, e|
264
274
  started = true
265
275
 
266
- %w( pid= << push update ).each do |msg|
276
+ %w( replace pid= << push update ).each do |msg|
267
277
  break(pid.send(msg, c)) if pid.respond_to? msg
268
278
  end
269
279
 
270
- te = ThreadEnsemble.new
280
+ te = ThreadEnsemble.new c
271
281
 
272
- te.add_thread(i, stdin) do |i, stdin|
273
- relay stdin, i, stdin_timeout if stdin
274
- i.close rescue nil
275
- end
282
+ te.add_thread(i, stdin) do |i, stdin|
283
+ relay stdin, i, stdin_timeout
284
+ i.close rescue nil
285
+ end
276
286
 
277
- te.add_thread(o, stdout) do |o, stdout|
278
- relay o, stdout, stdout_timeout if stdout
279
- end
287
+ te.add_thread(o, stdout) do |o, stdout|
288
+ relay o, stdout, stdout_timeout
289
+ end
280
290
 
281
- te.add_thread(e, stderr) do |o, stderr|
282
- relay e, stderr, stderr_timeout if stderr
283
- end
291
+ te.add_thread(e, stderr) do |o, stderr|
292
+ relay e, stderr, stderr_timeout
293
+ end
284
294
 
285
- te.run
295
+ te.run
286
296
  end
287
297
  end
288
298
  rescue
@@ -316,6 +326,44 @@ module Open4
316
326
  alias bg background
317
327
  module_function :background
318
328
  module_function :bg
329
+
330
+ def maim pid, opts = {}
331
+ #--{{{
332
+ getopt = getopts opts
333
+ sigs = getopt[ 'signals', %w(SIGTERM SIGQUIT SIGKILL) ]
334
+ suspend = getopt[ 'suspend', 4 ]
335
+ pid = Integer pid
336
+ existed = false
337
+ sigs.each do |sig|
338
+ begin
339
+ Process.kill sig, pid
340
+ existed = true
341
+ rescue Errno::ESRCH
342
+ return(existed ? nil : true)
343
+ end
344
+ return true unless alive? pid
345
+ sleep suspend
346
+ return true unless alive? pid
347
+ end
348
+ return(not alive?(pid))
349
+ #--}}}
350
+ end
351
+ module_function :maim
352
+
353
+ def alive pid
354
+ #--{{{
355
+ pid = Integer pid
356
+ begin
357
+ Process.kill 0, pid
358
+ true
359
+ rescue Errno::ESRCH
360
+ false
361
+ end
362
+ #--}}}
363
+ end
364
+ alias alive? alive
365
+ module_function :alive
366
+ module_function :'alive?'
319
367
  #--}}}
320
368
  end
321
369
 
File without changes
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: open4
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.0
7
- date: 2006-09-01 00:00:00.000000 -06:00
6
+ version: 0.8.0
7
+ date: 2006-09-25 00:00:00.000000 -06:00
8
8
  summary: open4
9
9
  require_paths:
10
10
  - lib
@@ -34,6 +34,7 @@ files:
34
34
  - lib
35
35
  - README
36
36
  - gemspec.rb
37
+ - open4-0.8.0.gem
37
38
  - sample/block.rb
38
39
  - sample/simple.rb
39
40
  - sample/exception.rb
@@ -42,7 +43,7 @@ files:
42
43
  - sample/timeout.rb
43
44
  - sample/stdin_timeout.rb
44
45
  - lib/open4.rb
45
- - lib/open4-0.7.0.rb
46
+ - lib/open4-0.8.0.rb
46
47
  test_files: []
47
48
  rdoc_options: []
48
49
  extra_rdoc_files: []