Sha256: 12b06509230a45667b7efafee77c4c227aa4a9744c0ea7f1a1f80cc0118731b9

Contents?: true

Size: 1.61 KB

Versions: 20

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module ForkTracker # :nodoc:
    module ModernCoreExt
      def _fork
        pid = super
        if pid == 0
          ForkTracker.after_fork_callback
        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 after_fork_callback
        new_pid = Process.pid
        if @pid != new_pid
          @callbacks.each(&:call)
          @pid = new_pid
        end
      end

      if Process.respond_to?(:_fork) # Ruby 3.1+
        def check!
          # We trust the `_fork` callback
        end
      else
        alias_method :check!, :after_fork_callback
      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

20 entries across 20 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/fork_tracker.rb
activesupport-7.1.5.1 lib/active_support/fork_tracker.rb
activesupport-7.1.5 lib/active_support/fork_tracker.rb
activesupport-7.1.4.2 lib/active_support/fork_tracker.rb
activesupport-7.1.4.1 lib/active_support/fork_tracker.rb
activesupport-7.1.4 lib/active_support/fork_tracker.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/fork_tracker.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/fork_tracker.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/fork_tracker.rb
activesupport-7.1.3.4 lib/active_support/fork_tracker.rb
activesupport-7.1.3.2 lib/active_support/fork_tracker.rb
activesupport-7.1.3.1 lib/active_support/fork_tracker.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/fork_tracker.rb
activesupport-7.1.3 lib/active_support/fork_tracker.rb
activesupport-7.1.2 lib/active_support/fork_tracker.rb
activesupport-7.1.1 lib/active_support/fork_tracker.rb
activesupport-7.1.0 lib/active_support/fork_tracker.rb
activesupport-7.1.0.rc2 lib/active_support/fork_tracker.rb
activesupport-7.1.0.rc1 lib/active_support/fork_tracker.rb
activesupport-7.1.0.beta1 lib/active_support/fork_tracker.rb