Created
December 6, 2018 14:33
-
-
Save pvalena/39cf453368e29b64d158fc738a5d8d37 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
# | |
# $ ./ruby-paralell echo 2 test <<EOLX | |
# first | |
# second | |
# third | |
# EOL | |
# [2192]+ echo first test | |
# [2193]+ echo second test | |
# first test | |
# second test | |
# [2194]+ echo third test | |
# third test | |
require 'shellwords' | |
# Kill spawned processes on failure | |
trap("SIGINT") { exit! } | |
# "rsync -a --del" | |
command_begin = Shellwords.escape ARGV.shift | |
## source-dir <= stdin | |
process_max_count = ARGV.shift.to_i | |
# "target-host:/dir/" | |
command_end = Shellwords.escape ARGV.shift | |
###################################### | |
def failed? status | |
unless status.success? | |
puts "[#{status.pid}] Failed!" | |
return true | |
end | |
end | |
def check_exited processes | |
failure_occured = nil | |
processes.reject! do |pid| | |
if exited = Process.waitpid(pid, Process::WNOHANG) | |
failure_occured = failed?($?) or failure_occured | |
end | |
exited | |
end | |
if failure_occured | |
wait_for_all processes | |
abort | |
end | |
end | |
def wait_for_all processes | |
processes.each do |pid| | |
Process.wait pid | |
failed? $? | |
end | |
processes.clear | |
end | |
###################################### | |
processes = Array.new | |
# "source-directory" | |
ARGF.each do |variable_arg| | |
variable_arg = Shellwords.escape variable_arg.chomp | |
command = [command_begin, variable_arg, command_end] | |
.join(' ') | |
while processes.count >= process_max_count | |
sleep 0.1 | |
check_exited processes | |
end | |
processes << spawn(command) | |
puts "[#{processes.last}]+ #{command}" | |
end | |
wait_for_all processes | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment