Sha256: 76780b464ef97b23d6336a7ba6d1d245e649c8f4035ade3aa67aa84e46d59866

Contents?: true

Size: 1.39 KB

Versions: 44

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module ForkTracker # :nodoc:
    module ModernCoreExt
      def _fork
        pid = super
        if pid == 0
          ForkTracker.check!
        end
        pid
      end
    end

    module CoreExt
      def fork(...)
        if block_given?
          super do
            ForkTracker.check!
            yield
          end
        else
          unless pid = super
            ForkTracker.check!
          end
          pid
        end
      end
    end

    module CoreExtPrivate
      include CoreExt
      private :fork
    end

    @pid = Process.pid
    @callbacks = []

    class << self
      def check!
        new_pid = Process.pid
        if @pid != new_pid
          @callbacks.each(&:call)
          @pid = new_pid
        end
      end

      def hook!
        if Process.respond_to?(:_fork) # Ruby 3.1+
          ::Process.singleton_class.prepend(ModernCoreExt)
        elsif Process.respond_to?(:fork)
          ::Object.prepend(CoreExtPrivate) if RUBY_VERSION < "3.0"
          ::Kernel.prepend(CoreExtPrivate)
          ::Kernel.singleton_class.prepend(CoreExt)
          ::Process.singleton_class.prepend(CoreExt)
        end
      end

      def after_fork(&block)
        @callbacks << block
        block
      end

      def unregister(callback)
        @callbacks.delete(callback)
      end
    end
  end
end

ActiveSupport::ForkTracker.hook!

Version data entries

44 entries across 41 versions & 8 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/fork_tracker.rb
activesupport-7.0.8.5 lib/active_support/fork_tracker.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/fork_tracker.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/fork_tracker.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/fork_tracker.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/fork_tracker.rb
activesupport-7.0.8.4 lib/active_support/fork_tracker.rb
activesupport-7.0.8.1 lib/active_support/fork_tracker.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/fork_tracker.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/fork_tracker.rb
activesupport-7.0.8 lib/active_support/fork_tracker.rb
activesupport-7.0.7.2 lib/active_support/fork_tracker.rb
activesupport-7.0.7.1 lib/active_support/fork_tracker.rb
activesupport-7.0.7 lib/active_support/fork_tracker.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/fork_tracker.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/fork_tracker.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/fork_tracker.rb
activesupport-7.0.6 lib/active_support/fork_tracker.rb
activesupport-7.0.5.1 lib/active_support/fork_tracker.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/fork_tracker.rb