Skip to content
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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions exe/dual_run
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
Copy link
Member

@JuanVqz JuanVqz Nov 29, 2023

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:

Running in parallel:

next results
BUNDLE_GEMFILE=Gemfile.next bundle exec rspec spec/models/
================================================================================

Randomized with seed 41263
...................

Finished in 0.25773 seconds (files took 4.02 seconds to load)
19 examples, 0 failures

Randomized with seed 41263




(waiting for current command to finish)
current results
BUNDLE_GEMFILE=Gemfile bundle exec rspec spec/models/
================================================================================

Randomized with seed 40666
...................

Finished in 0.23505 seconds (files took 3.96 seconds to load)
19 examples, 0 failures

Randomized with seed 40666

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
Loading