Sha256: 6ced53c1f5e9a1ba2c5a3160dbbfe77618172ea5334fd2a1d7ff20a2d3b887be

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

class SafeFork
  def self.fork
    begin
      # remove our connection so it doesn't get cloned
      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 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 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

3 entries across 3 versions & 1 rubygems

Version Path
hydra-0.16.2 lib/hydra/safe_fork.rb
hydra-0.16.1 lib/hydra/safe_fork.rb
hydra-0.16.0 lib/hydra/safe_fork.rb