Sha256: 6f7029c174d271cec165f99d4778e561779ab94453c1b35af14fec0b859ea97b

Contents?: true

Size: 1.7 KB

Versions: 16

Compression:

Stored size: 1.7 KB

Contents

# typed: false

module Datadog
  module Core
    module Utils
      # Helper methods for managing forking behavior
      module Forking
        def self.included(base)
          base.prepend(ClassExtensions) if base.is_a?(Class)
        end

        def self.extended(base)
          # Explicitly update PID here because there's a case where
          # the code path that lazily updates the PID may not be exercised
          # until after a fork occurs, thus causing the event to be missed.
          # By eagerly setting this, we avoid this scenario.
          base.update_fork_pid!
        end

        def after_fork!
          if forked?
            yield
            update_fork_pid!
            true
          else
            false
          end
        end

        def forked?
          Process.pid != fork_pid
        end

        def update_fork_pid!
          @fork_pid = Process.pid
        end

        def fork_pid
          @fork_pid ||= Process.pid
        end

        # Adds additional functionality for Classes that implement Forking
        module ClassExtensions
          # Addresses an edge case where forking before invoking #update_fork_pid! on the
          # object will cause forking to not be detected in the fork when it should have.
          #
          # This wrapper prevents this by initializing the fork PID when the object is created.
          if RUBY_VERSION >= '3'
            def initialize(*args, **kwargs, &block)
              super(*args, **kwargs, &block)
              update_fork_pid!
            end
          else
            def initialize(*args, &block)
              super(*args, &block)
              update_fork_pid!
            end
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ddtrace-1.9.0 lib/datadog/core/utils/forking.rb
ddtrace-1.8.0 lib/datadog/core/utils/forking.rb
ddtrace-1.7.0 lib/datadog/core/utils/forking.rb
ddtrace-1.6.1 lib/datadog/core/utils/forking.rb
ddtrace-1.6.0 lib/datadog/core/utils/forking.rb
ddtrace-1.5.2 lib/datadog/core/utils/forking.rb
ddtrace-1.5.1 lib/datadog/core/utils/forking.rb
ddtrace-1.5.0 lib/datadog/core/utils/forking.rb
ddtrace-1.4.2 lib/datadog/core/utils/forking.rb
ddtrace-1.4.1 lib/datadog/core/utils/forking.rb
ddtrace-1.4.0 lib/datadog/core/utils/forking.rb
ddtrace-1.3.0 lib/datadog/core/utils/forking.rb
ddtrace-1.2.0 lib/datadog/core/utils/forking.rb
ddtrace-1.1.0 lib/datadog/core/utils/forking.rb
ddtrace-1.0.0 lib/datadog/core/utils/forking.rb
ddtrace-1.0.0.beta2 lib/datadog/core/utils/forking.rb