Sha256: f190c8256c7f89ed5984139256ea7dd0bdd9e235850f1bf5713b2c32091db9be

Contents?: true

Size: 1.14 KB

Versions: 42

Compression:

Stored size: 1.14 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

42 entries across 42 versions & 8 rubygems

Version Path
ngauthier-hydra-0.24.0 lib/hydra/safe_fork.rb
nulogy-hydra-0.26.0 lib/hydra/safe_fork.rb
arturop-hydra-0.25.0 lib/hydra/safe_fork.rb
arturop-hydra-0.24.0 lib/hydra/safe_fork.rb
hydra-0.24.0 lib/hydra/safe_fork.rb
nulogy-hydra-0.23.2.1 lib/hydra/safe_fork.rb
justinf-hydra-0.23.8 lib/hydra/safe_fork.rb
justinf-hydra-0.23.7 lib/hydra/safe_fork.rb
justinf-hydra-0.23.6 lib/hydra/safe_fork.rb
justinf-hydra-0.23.5 lib/hydra/safe_fork.rb
justinf-hydra-0.23.4 lib/hydra/safe_fork.rb
arturop-hydra-0.23.4 lib/hydra/safe_fork.rb
sskirby-hydra-0.23.3 lib/hydra/safe_fork.rb
hydra-0.23.3 lib/hydra/safe_fork.rb
causes-hydra-0.21.0 lib/hydra/safe_fork.rb
hydra-0.23.2 lib/hydra/safe_fork.rb
hydra-0.23.1 lib/hydra/safe_fork.rb
hydra-0.23.0 lib/hydra/safe_fork.rb
hydra-0.22.2 lib/hydra/safe_fork.rb
hydra-0.22.1 lib/hydra/safe_fork.rb