Forked from danieldbower/fork_with_new_connection.rb
Last active
May 19, 2022 07:23
-
-
Save kaosf/2d13da1161ac5c96da66c6e83349158c to your computer and use it in GitHub Desktop.
Forking Processes In Ruby On Rails
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
# Logic for forking connections | |
# The forked process does not have access to static vars as far as I can discern, so I've done some stuff to check if the op threw an exception. | |
def fork_with_new_connection | |
# Store the ActiveRecord connection information | |
config = ActiveRecord::Base.remove_connection | |
pid = fork do | |
# tracking if the op failed for the Process exit | |
success = true | |
begin | |
ActiveRecord::Base.establish_connection(config) | |
# This is needed to re-initialize the random number generator after forking (if you want diff random numbers generated in the forks) | |
srand | |
# Run the closure passed to the fork_with_new_connection method | |
yield | |
rescue Exception => exception | |
puts ("Forked operation failed with exception: #{exception}") | |
# the op failed, so note it for the Process exit | |
success = false | |
ensure | |
ActiveRecord::Base.remove_connection | |
Process.exit! success | |
end | |
end | |
# Restore the ActiveRecord connection information | |
ActiveRecord::Base.establish_connection(config) | |
#return the process id | |
pid | |
end | |
# A static var to keep track of the number of failures | |
@@failed = 0 | |
@@forks = 20 | |
#forks @@forks processes | |
def concurrent_opps iteration | |
(1..@@forks).each do |i| | |
pid = fork_with_new_connection do | |
# simple way to keep track of progress | |
puts "Fork: #{i}" | |
#necessary to manage activerecord connections since we are forking | |
ActiveRecord::Base.connection.reconnect! | |
# | |
# The whole reason for creating the fork... do some stuff here | |
# | |
x = rand[25] | |
if 0==x then raise Exception, "it was 6, and I don't like 6's" end | |
end | |
puts "Process #{pid} completed" | |
end | |
#Wait for all processes to finish before proceeding - collect results as well | |
results = Process.waitall | |
results.each{ |result| | |
@@failed += result[1].exitstatus | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment