Skip to content

Commit

Permalink
working with threads
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Mar 26, 2016
1 parent 8c07a83 commit 91ac9af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions lib/ember_cli/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ module EmberCli
class Runner
def initialize(out:, err:, env: {}, options: {})
@env = env
@out_streams = Array(out)
@err_streams = Array(err)
@output_streams = Array(out)
@error_streams = Array(err)
@options = options
@threads = []
end

def run(command)
Open3.popen3(env, command, options) do |stdin, stdout, stderr, process|
stdin.close

redirect(stdout, pipes: out_streams)
redirect(stderr, pipes: err_streams)
threads << redirect_stream_in_thread(stdout, write_to: output_streams)
threads << redirect_stream_in_thread(stderr, write_to: error_streams)

threads.each(&:join)
process.value
end
end
Expand All @@ -30,17 +32,17 @@ def run!(command)

protected

attr_reader :env, :err_streams, :options, :out_streams
attr_reader :env, :error_streams, :options, :output_streams, :threads

private

def redirect(stream, pipes:)
pipes.each do |pipe|
# Thread.new do
# Thread.current.abort_on_exception = true
#
IO.copy_stream(stream, pipe)
# end
def redirect_stream_in_thread(stream, write_to:)
Thread.new do
Thread.current.abort_on_exception = true

while line = stream.gets
write_to.each { |redirection_stream| redirection_stream.puts(line) }
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ember_cli/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def spawn(command)
def runner
Runner.new(
options: { chdir: paths.root.to_s },
out: [$stdout, paths.log],
out: [$stdout, paths.log.open("a")],
err: [$stderr],
env: env,
)
Expand Down

0 comments on commit 91ac9af

Please sign in to comment.