Module: MemprofilerPprof::Atfork
- Defined in:
- lib/ruby_memprofiler_pprof/atfork.rb
Defined Under Namespace
Classes: Handler
Class Method Summary collapse
Class Method Details
.at_fork(stage = :prepare, &block) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/ruby_memprofiler_pprof/atfork.rb', line 29 def at_fork(stage = :prepare, &block) handler = Handler.new.tap do |h| h.block = block h.stage = stage end at_fork_handlers << handler handler end |
.at_fork_handlers ⇒ Object
25 26 27 |
# File 'lib/ruby_memprofiler_pprof/atfork.rb', line 25 def at_fork_handlers @@at_fork_handlers ||= [] end |
.fork ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby_memprofiler_pprof/atfork.rb', line 38 def fork # If a block is provided, it must be wrapped to trigger callbacks. child_block = if block_given? proc do # Trigger :child callback at_fork_handlers.select { |h| h.stage == :child }.each(&:call) # Invoke original block yield end end # Trigger :prepare callback at_fork_handlers.select { |h| h.stage == :prepare }.each(&:call) # Start fork # If a block is provided, use the wrapped version. result = child_block.nil? ? super : super(&child_block) # Trigger correct callbacks depending on whether we're in the parent or child. # If we're in the fork, result = nil: trigger child callbacks. # If we're in the parent, result = fork PID: trigger parent callbacks. if result.nil? # Trigger :child callback at_fork_handlers.select { |h| h.stage == :child }.each(&:call) else # Trigger :parent callback at_fork_handlers.select { |h| h.stage == :parent }.each(&:call) end # rubocop:enable Style/IfInsideElse # Return PID from #fork result end |