forked from travis-ci/travis-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathem_stdout.rb
52 lines (42 loc) · 893 Bytes
/
em_stdout.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'rubygems'
require 'em/stdout'
EventMachine.module_eval do
def self.split_stdout(&block)
stdout = nil
EM.next_tick do
read, write = IO.pipe
stdout = STDOUT.clone
EM.attach(read, Splitter) { |c|
c.stdout = stdout
c.callback = block
}
STDOUT.reopen(write)
end
sleep(0.01) until stdout
stdout
end
class Splitter < EventMachine::Connection
attr_accessor :stdout, :callback
def receive_data(data)
callback.call(data)
end
def unbind
STDOUT.reopen(stdout)
stdout.puts "unbound"
end
end
end
STDOUT.sync = true
EM.run do
EM.defer do
stdout = EM.split_stdout do |data|
stdout.puts '--: ' + data.inspect
sleep(1)
end
stdout.puts 'starting'
10.times { puts 'output ... '; sleep(0.2) }
stdout.puts 'stopping'
EM.stop
end
end
puts 'fin'