forked from clio/ten_years_rails
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dual_run command #112
Closed
Closed
Add dual_run command #112
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "pty" | ||
|
||
command = ARGV.join(" ") | ||
|
||
if command == "" | ||
puts "No command specified" | ||
exit 1 | ||
end | ||
|
||
puts "Running in parallel:" | ||
puts "BUNDLE_GEMFILE=Gemfile.next #{command}" | ||
puts command | ||
puts "" | ||
|
||
current_output = "" | ||
current_status = nil | ||
should_exit = false | ||
|
||
# Run the "current" version in a thread. Capture it's output to print at the end | ||
# Using PTY to capture the colorized output | ||
current_command = Thread.new do | ||
# stdout_err is the mixed output of stdout and std_err | ||
PTY.spawn(command) do |stdout_err, stdin, pid| | ||
char = stdout_err.getc | ||
while !should_exit && char | ||
current_output << char | ||
char = stdout_err.getc | ||
end | ||
rescue Errno::EIO # always raised when PTY runs out of input | ||
ensure | ||
if should_exit | ||
# kill process with a SIGINT signal | ||
Process.kill 2, pid | ||
else | ||
Process.waitpid pid # Wait for PTY to complete before continuing | ||
current_status = $?.exitstatus | ||
end | ||
end | ||
end | ||
|
||
next_status = nil | ||
|
||
# Run the "next" version in another thread | ||
next_command = Thread.new do | ||
puts "next results" | ||
puts "=" * 80 | ||
system("BUNDLE_GEMFILE=Gemfile.next #{command}") | ||
next_status = $?.exitstatus | ||
end | ||
|
||
# Handle Ctrl+C | ||
Signal.trap("INT") do | ||
# handle pressing Ctrl+C twice to kill the commands without waiting | ||
if should_exit | ||
Thread.kill(next_command) | ||
Thread.kill(current_command) | ||
end | ||
|
||
should_exit = true | ||
end | ||
|
||
# Wait for the "next" command to finish first | ||
next_command.join | ||
|
||
puts "" | ||
puts "" | ||
puts "" | ||
|
||
# Then wait for the "current" command to print its output | ||
puts "(waiting for current command to finish)" | ||
current_command.join | ||
puts "current results" | ||
puts "=" * 80 | ||
puts current_output | ||
|
||
# check exit statuses, if both are successful exit with 0 | ||
if current_status == 0 && next_status == 0 | ||
exit 0 | ||
elsif should_exit | ||
puts "Terminated by user" | ||
exit 1 | ||
else | ||
puts "Current command exited with: #{current_status}" | ||
puts "Next command exited with: #{next_status}" | ||
exit 1 | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arielj Probably we don't need to print the command twice, it was printed in the line before, what about to print it on each block.
Like so: