Class: SafeFork

Inherits:
Object
  • Object
show all
Defined in:
lib/hydra/safe_fork.rb

Class Method Summary collapse

Class Method Details

.forkObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hydra/safe_fork.rb', line 2

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