Sha256: 1906d9d08357942be78a1d0e0389dc9b7c4ebf113cf7ceb493725dcd40927087

Contents?: true

Size: 769 Bytes

Versions: 14

Compression:

Stored size: 769 Bytes

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
          ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
          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
      ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
    end
    return child
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hydra-0.15.1 lib/hydra/safe_fork.rb
hydra-0.15.0 lib/hydra/safe_fork.rb
hydra-0.14.1 lib/hydra/safe_fork.rb
hydra-0.14.0 lib/hydra/safe_fork.rb
hydra-0.13.0 lib/hydra/safe_fork.rb
hydra-0.12.0 lib/hydra/safe_fork.rb
hydra-0.11.0 lib/hydra/safe_fork.rb
hydra-0.10.3 lib/hydra/safe_fork.rb
hydra-0.10.2 lib/hydra/safe_fork.rb
hydra-0.10.1 lib/hydra/safe_fork.rb
hydra-0.10.0 lib/hydra/safe_fork.rb
hydra-0.9.0 lib/hydra/safe_fork.rb
hydra-0.8.0 lib/hydra/safe_fork.rb
hydra-0.7.2 lib/hydra/safe_fork.rb