config/initializers/active_record_extensions.rb
# https://gist.github.com/danieldbower/842562
# 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.
gist.github.com/danieldbower/842562
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.
gist . github . com / danieldbower / 842562 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
      config = ActiveRecord::Base.remove_connection
      pid = fork do
        success = true
        begin
          ActiveRecord::Base.establish_connection(config)
          srand
          yield
        rescue => exception
          logger.debug2  ('Forked operation failed with exception: ' + exception)
          success = false
        ensure
          ActiveRecord::Base.remove_connection
          Process.exit! success
        end
      end
      ActiveRecord::Base.establish_connection(config)
      pid
    end
def fork_with_new_connection config = ActiveRecord :: Base . remove_connection pid = fork do success = true begin ActiveRecord :: Base . establish_connection ( config ) srand yield rescue => exception logger . debug2 ( 'Forked operation failed with exception: ' + exception ) success = false ensure ActiveRecord :: Base . remove_connection Process . exit! success end end ActiveRecord :: Base . establish_connection ( config ) pid end
https://gist.github.com/danieldbower/842562
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.
https : // gist . github . com / danieldbower / 842562 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 .
# Store the ActiveRecord connection information # tracking if the op failed for the Process exit # This is needed to re-initialize the random number generator after forking (if you want diff random numbers generated in the forks) # Run the closure passed to the fork_with_new_connection method # the op failed, so note it for the Process exit # Restore the ActiveRecord connection information #return the process id