Sha256: 135b6a29219490715a04db24ca74f1ccd521358083f37d882cd09064fa41c702

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

class SafeFork
  def self.fork
    begin
      # remove our connection so it doesn't get cloned
      connection = ActiveRecord::Base.remove_connection if defined?(ActiveRecord)
      # fork a process
      child = Process.fork do
        begin
          # create a new connection and perform the action
          begin
          ActiveRecord::Base.establish_connection(connection.merge({:allow_concurrency => true})) if defined?(ActiveRecord)
          rescue ActiveRecord::AdapterNotSpecified
            # AR was defined but we didn't have a connection
          end
          yield
        ensure
          # make sure we remove the connection before we're done
          ActiveRecord::Base.remove_connection if defined?(ActiveRecord)
        end      
      end
    ensure
      # make sure we re-establish the connection before returning to the main instance
      begin
        ActiveRecord::Base.establish_connection(connection.merge({:allow_concurrency => true})) if defined?(ActiveRecord)
      rescue ActiveRecord::AdapterNotSpecified
        # AR was defined but we didn't have a connection
      end
    end
    return child
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hydra-0.16.3 lib/hydra/safe_fork.rb