Sha256: 0dcf0edfa95b0e3861abaeb298aff98bd8f6a13fb5121fe688fa92524310aaa3

Contents?: true

Size: 1.26 KB

Versions: 32

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module ForkTracker # :nodoc:
    module CoreExt
      def fork(*)
        if block_given?
          super do
            ForkTracker.check!
            yield
          end
        else
          unless pid = super
            ForkTracker.check!
          end
          pid
        end
      end
      ruby2_keywords(:fork) if respond_to?(:ruby2_keywords, true)
    end

    module CoreExtPrivate
      include CoreExt

      private
        def fork(*)
          super
        end
        ruby2_keywords(:fork) if respond_to?(:ruby2_keywords, true)
    end

    @pid = Process.pid
    @callbacks = []

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

      def hook!
        if Process.respond_to?(:fork)
          ::Object.prepend(CoreExtPrivate)
          ::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

32 entries across 32 versions & 6 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/fork_tracker.rb
activesupport-6.1.7.9 lib/active_support/fork_tracker.rb
activesupport-6.1.7.8 lib/active_support/fork_tracker.rb
activesupport-6.1.7.7 lib/active_support/fork_tracker.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/fork_tracker.rb
activesupport-6.1.7.6 lib/active_support/fork_tracker.rb
activesupport-6.1.7.5 lib/active_support/fork_tracker.rb
activesupport-6.1.7.4 lib/active_support/fork_tracker.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/fork_tracker.rb
activesupport-6.1.7.3 lib/active_support/fork_tracker.rb
activesupport-6.1.7.2 lib/active_support/fork_tracker.rb
activesupport-6.1.7.1 lib/active_support/fork_tracker.rb
activesupport-6.1.7 lib/active_support/fork_tracker.rb
activesupport-6.1.6.1 lib/active_support/fork_tracker.rb
activesupport-6.1.6 lib/active_support/fork_tracker.rb
activesupport-6.1.5.1 lib/active_support/fork_tracker.rb
activesupport-6.1.5 lib/active_support/fork_tracker.rb
activesupport-6.1.4.7 lib/active_support/fork_tracker.rb
activesupport-6.1.4.6 lib/active_support/fork_tracker.rb
activesupport-6.1.4.5 lib/active_support/fork_tracker.rb