Module: RailsParallel::Forks

Included in:
Runner::Child, Runner::Parent, Schema
Defined in:
lib/rails_parallel/forks.rb

Instance Method Summary collapse

Instance Method Details

#before_exitObject



35
36
37
# File 'lib/rails_parallel/forks.rb', line 35

def before_exit
  # cleanup here (in children)
end

#check_status(stat) ⇒ Object



31
32
33
# File 'lib/rails_parallel/forks.rb', line 31

def check_status(stat)
  raise "error: #{stat.inspect}" unless stat.success?
end

#fork_and_runObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_parallel/forks.rb', line 3

def fork_and_run
  ActiveRecord::Base.connection.disconnect! if ActiveRecord::Base.connected?

  fork do
    begin
      yield
      Kernel.exit!(0)
    rescue Interrupt, SignalException
      Kernel.exit!(1)
    rescue Exception => e
      puts "Error: #{e}"
      puts(*e.backtrace.map {|t| "\t#{t}"})
      before_exit
      Kernel.exit!(1)
    end
  end
end

#wait_any(nonblock = false) ⇒ Object



27
28
29
# File 'lib/rails_parallel/forks.rb', line 27

def wait_any(nonblock = false)
  wait_for(-1, nonblock)
end

#wait_for(pid, nonblock = false) ⇒ Object



21
22
23
24
25
# File 'lib/rails_parallel/forks.rb', line 21

def wait_for(pid, nonblock = false)
  pid = Process.waitpid(pid, nonblock ? Process::WNOHANG : 0)
  check_status($?) if pid
  pid
end